473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

time returning -1

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?
Nov 14 '05 #1
3 1464


Servé Lau wrote:
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?


time() returns -1 "if the calendar time is not available."
There are many reasons the calendar time might be unavailable:
no timer in the system, timer reading outside the time_t range,
timer temporarily unavailable, ... The Standard does not try
to enumerate all the failure modes.

Does the loop belong inside time()? I think not. If the
time is unavailable, there's no guarantee that it will become
available within a reasonable, er, time. Maybe the hardware
timer has "locked up" somehow and will not recover until it's
been power-cycled and reset. Maybe the other process that's
interfering with your time() calls will keep on interfering
with them until somebody kills it. Would you rather receive
a failure indication, or just have your program freeze inside
the time() function?

--
Er*********@sun.com

Nov 14 '05 #2
"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.
Nov 14 '05 #3
On Wed, 20 Apr 2005 12:57:19 -0400, Eric Sosman wrote:


Servé Lau wrote:
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?
time() returns -1 "if the calendar time is not available."
There are many reasons the calendar time might be unavailable:
no timer in the system, timer reading outside the time_t range,
timer temporarily unavailable, ... The Standard does not try
to enumerate all the failure modes.

Does the loop belong inside time()? I think not.


It depends, and requires implementation-specific knowledge. But of course
as time() is part of the implementation that's not a problem. If the
implementation knows that this bus locking is the source of the problem
and is a transient phenomenon it would make sense for it to loop in
time(). Programs that call time() typically don't deal with transient
failure of that function, if they test for failure at all. So if the
implementation can make "typical" programs continue to work correctly then
it probably should.

OTOH the implementor may have already considered this and decided that
it is not viable.
If the
time is unavailable, there's no guarantee that it will become
available within a reasonable, er, time.
The time() implementation could implement a timeout(!). But maybe it did
and has timed out. :-)
Maybe the hardware
timer has "locked up" somehow and will not recover until it's
been power-cycled and reset.
In which case the problem isn't transient and it wouldn't be useful for
the calling program to keep trying.
Maybe the other process that's
interfering with your time() calls will keep on interfering
with them until somebody kills it. Would you rather receive
a failure indication, or just have your program freeze inside
the time() function?


That would depend on various factors. A process that caused such
interference would likely be considered faulty. Again if the behaviour of
time() makes it necessary to create a calling loop in the user code it
isn't helping. If a system can get into a state where a bus is locked
indefinitely then the system is probably not usable from that point on.

Lawrence
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Funnyweb | last post by:
I have just notices that the date() function is not returning the correct date/time on my "server". I am running apache2 on my winxp pro laptop. My system clock is set to the correct date,...
8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
7
by: Patrick Kowalzick | last post by:
Dear all, I just wondered if it is possible to count the number of classes created via a template class at compile time. To show what I mean I post an example, which is not working but carries...
38
by: vashwath | last post by:
Might be off topic but I don't know where to post this question.Hope some body clears my doubt. The coding standard of the project which I am working on say's not to use malloc.When I asked my...
7
by: Brett Edman | last post by:
I created a UTC clock using this: UTCTime = MyTime.ToUniversalTime() Now that we've turned the clocks ahead 1 hour for daylight savings time, the clock is reporting the wrong UTC time. It is...
18
by: Sven | last post by:
Hi, I found a strange behaviour when using the time() function from time.h. Sometimes when it is called, it does not show the correct time in seconds, but an initial value. This time seem to be...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
8
by: bill | last post by:
It appears that the only parameter that can be passed to time() is 0. No other number works (because converting from int to time_t*). If that's true, why does it even accept a parameter? I've...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
1
by: akdemirc | last post by:
Hi, My question is about retrieving single records based on a time column, i mean the result set should not include duplicate rows for a unique time value as an example: A B C ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.