"Servé Lau" <bl****@bleat.com> writes:
I encountered a situation today where time(NULL) returned -1. This was
because at the point of the call the function could not read from the bus
because it was locked by another process.
I got the advice to use while((t = time(NULL)) == -1) { ; }
I always thought that time only returned -1 when their was no timer
functionality in the hardware and that it should return a valid value for
all other cases. Is that correct? Should the loop above be moved into the
time function?
Eric Sosman's reply was good Also, a tight loop like that is likely to
be a bad idea on a multi-process system; your program will gobble CPU
time at the expense of other processes until the time becomes
available. It might even prevent the other process from releasing its
lock on the bus.
It probably makes sense for something like the loop to be inside the
time() function *if* it can determine that the failure is a temporary
one. Ideally, there should be some way for time() to go to sleep
until the information becomes available, allowing other processes to
run.
This is all extremely system-specific, of course. The C standard
doesn't deal with multiple processes, and as far as it's concerned the
time() function either succeeds or fails.
You might consider putting a short delay into your loop (in some
system-specific way), and perhaps bailing out with an error indication
if time() fails too many times in a row.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.