473,473 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sleep( ) misbehaving

I am trying to write a little program that goes to sleep for 5 seconds.
However, if the user presses Ctrl-C, it interrupts the sleep and prints the
unslept amount.

For example:

unix> ./sleeper
sleeping for 5 seconds
unslept amount is 2 (User hits Ctrl-C after 3 seconds)
unix>

What's wrong with the code below?
-------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

unsigned int unslept;

void handler (int sig)
{
printf("unslept amount is %d",unslept);
exit(0);
}

int main()
{
signal(SIGINT,handler);
printf("sleeping for 5 seconds\n");
unslept = sleep(5);
return 0;
}
Dec 8 '05 #1
10 3512
syed wrote:
I am trying to write a little program that goes to sleep for 5 seconds.
However, if the user presses Ctrl-C, it interrupts the sleep and prints the
unslept amount.

<snip>
You're looking for comp.unix.programmer. sleep() is a nonstandard function.
Also see http://www.ungerhu.com/jxh/clc.welcome.txt.

S.
Dec 8 '05 #2
syed a écrit :
I am trying to write a little program that goes to sleep for 5 seconds.
However, if the user presses Ctrl-C, it interrupts the sleep and prints the
unslept amount.

For example:

unix> ./sleeper
sleeping for 5 seconds
unslept amount is 2 (User hits Ctrl-C after 3 seconds)
unix>

What's wrong with the code below?
-------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

unsigned int unslept;

void handler (int sig)
{
printf("unslept amount is %d",unslept);
exit(0);
}

int main()
{
signal(SIGINT,handler);
printf("sleeping for 5 seconds\n");
unslept = sleep(5);
return 0;
}


After 3 seconds you sent the program a signal (SIGINT) with
the Ctrl+C keys. You got into your handler procedure
and exited.

What else do you want?

Signals are asynchronous in C.

Programs doing something can at any moment be redirected to a signal
handler.

This is normal behavior.

jacob
Dec 8 '05 #3
"jacob navia" <ja***@jacob.remcomp.fr> wrote:
After 3 seconds you sent the program a signal (SIGINT) with
the Ctrl+C keys. You got into your handler procedure
and exited.

What else do you want?


I want it to print the unslept time correctly. My code always prints ZERO
for unslept time.

unix> ./sleeper
sleeping for 5 seconds
unslept amount is 0 (This line prints when user
hits Ctrl-C, unslept amount is always zero)
unix>

I want it to print the acutal time left to sleep. For example if the user
hits Ctrl-C after three seconds, then it should print 2 for unslept time:

unix> ./sleeper
sleeping for 5 seconds
unslept amount is 2 (User hits Ctrl-C after 3
seconds)
unix>

----
Thanks.
Syed
Dec 8 '05 #4

"jacob navia" <ja***@jacob.remcomp.fr> wrote in message
news:43***********************@news.wanadoo.fr...
syed a écrit :
I am trying to write a little program that goes to sleep for 5 seconds.
However, if the user presses Ctrl-C, it interrupts the sleep and prints
the unslept amount.

For example:

unix> ./sleeper
sleeping for 5 seconds
unslept amount is 2 (User hits Ctrl-C after 3 seconds)
unix>

What's wrong with the code below?


unslept variable won't be set until AFTER the sleep() returns.
Move the printf() out of the signal handler (shouldn't do i/o in
there anyway) and place it after the call where it is set.
shitcan the exit() from your handler and just return sig;
That should do what you want.
Dec 8 '05 #5
syed wrote:
I want it to print the unslept time correctly. My code always prints ZERO
for unslept time.


that's because you never let sleep finish.

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void handler (int sig)
{
}

int main()
{
unsigned int unslept;
signal(SIGINT,handler);
printf("sleeping for 5 seconds\n");
unslept = sleep(5);
if (unslept)
printf("time not slept %d\n" unslept);
return 0;
}

Dec 8 '05 #6
syed wrote:

"jacob navia" <ja***@jacob.remcomp.fr> wrote:
After 3 seconds you sent the program a signal (SIGINT) with
the Ctrl+C keys. You got into your handler procedure
and exited.

What else do you want?


I want it to print the unslept time correctly. My code always prints ZERO
for unslept time.


Well, it would have been nice if you said so in the first place, rather
than showing a sample of the "correct" output.

[...]

As has been pointed out, sleep() is not standard C, so is off-topic here.
While the man page on _my_ Unix system says:

If the sleep() function returns due to the delivery of a signal,
the value returned will be the unslept amount in seconds.

there is nothing as far as C itself is concerned that says that this must
be the return value. Check the man pages for your particular system, and
if it says it should "work" as you expect, then you need to ask a group
that is appropriate for your system-specific question.

On the other hand, note that the signal handler will be called _before_
the interrupted sleep() call returns.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Dec 9 '05 #7
"syed" <su*******@gmail.com> writes:
I am trying to write a little program that goes to sleep for 5 seconds.
However, if the user presses Ctrl-C, it interrupts the sleep and prints the
unslept amount.

For example:

unix> ./sleeper
sleeping for 5 seconds
unslept amount is 2 (User hits Ctrl-C after 3 seconds)
unix>

What's wrong with the code below?
-------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

unsigned int unslept;

void handler (int sig)
{
printf("unslept amount is %d",unslept);
exit(0);
}

int main()
{
signal(SIGINT,handler);
printf("sleeping for 5 seconds\n");
unslept = sleep(5);
return 0;
}


sleep() is off-topic, but signals are part of the C standard.

Try this:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

unsigned int unslept = 42;

void handler (int sig)
{
printf("unslept amount is %d\n",unslept);
exit(0);
}

int main()
{
signal(SIGINT,handler);
printf("sleeping for 5 seconds\n");
unslept = sleep(5);
return 0;
}

(I've added a newline to the message printed by handler(), and I've
initialized unslept to 42.)

If the call to sleep() is interrupted by a SIGINT, it never returns,
so the assignment "unslept = sleep(5);" is never executed. The
variable retains its previous value, which is 0 in your original
program, 42 in my version.

--
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.
Dec 9 '05 #8
Keith Thompson <ks***@mib.org> writes:
[snip]
sleep() is off-topic, but signals are part of the C standard.

Try this:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

unsigned int unslept = 42;

void handler (int sig)
{
printf("unslept amount is %d\n",unslept);
exit(0);
}

int main()
{
signal(SIGINT,handler);
printf("sleeping for 5 seconds\n");
unslept = sleep(5);
return 0;
}

(I've added a newline to the message printed by handler(), and I've
initialized unslept to 42.)

If the call to sleep() is interrupted by a SIGINT, it never returns,
so the assignment "unslept = sleep(5);" is never executed. The
variable retains its previous value, which is 0 in your original
program, 42 in my version.


One more thing: the reason sleep() never returns is that you do an
exit(0) in the signal handler. If you delete that line, you'll find
that the sleep() call does complete, and the variable unslept is set
to whatever value it returns.

What that value might be depends on what the sleep() function does,
something not covered by the C standard.

Also, you should use "%u", not "%d", to print an unsigned int value.

--
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.
Dec 9 '05 #9
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
sleep() is off-topic, but signals are part of the C standard. Try this: #include <stdio.h>
#include <stdlib.h>
#include <signal.h> unsigned int unslept = 42; void handler (int sig)
{
printf("unslept amount is %d\n",unslept);
exit(0);
}


The C89 standard says that except for signals occuring from
abort() or raise(), you have UB if the signal handler calls
any library function other than signal() or refers to any static
variable that isn't volatile sig_atomic_t .

Your example code refers to a static variable that isn't sig_atomic_t
and calls two library functions other than signal...
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Dec 9 '05 #10
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
sleep() is off-topic, but signals are part of the C standard.

Try this:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

unsigned int unslept = 42;

void handler (int sig)
{
printf("unslept amount is %d\n",unslept);
exit(0);
}


The C89 standard says that except for signals occuring from
abort() or raise(), you have UB if the signal handler calls
any library function other than signal() or refers to any static
variable that isn't volatile sig_atomic_t .

Your example code refers to a static variable that isn't sig_atomic_t
and calls two library functions other than signal...


D'oh!

(It almost certainly happens to "work" in the environment the OP is
using, but of course that's beside the point.)

BTW, the OP posted to comp.unix.programmer and got some good answers
there.

--
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.
Dec 9 '05 #11

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

Similar topics

5
by: Anks | last post by:
hi I am displaying a digital clock in an applet.I am using Thread.sleep (1000) for counting every second.This works fine some times but some times my clock just slows down or give an...
7
by: Colin Brown | last post by:
Python 2.3.3 Under Windows NT: Negative Int or Real -> sleeps for a long time Under Linux: Returns IOError: Invalid argument I consider both of these unfriendly results and expected the...
5
by: Dave | last post by:
For the life of me, I thought there was a sleep() function in the Standard Library (or something similarly named), but I can't find it. Is it non-existent or, if not, what header is it in? ...
21
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
1
by: u473 | last post by:
Running Totals by date misbehaving I applied to the letter the sample code given on http://support.microsoft.com/?kbid=290136 but it seems the running total breaks when the day number is less...
8
by: Cider123 | last post by:
I ran into a situation where my Window Service had to process 100,000+ files, when I first noticed I needed to tweak various routines. Everything runs fine, but here's what I ran into: In the...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
1
by: Matt Brown - identify | last post by:
Hello, I, as well as suspecting many others, am having an issue when I attempt to enter sleep mode in Vista. If any program is connected to the internet (AOL Instant Messenger for instance),...
13
by: Charles Zhang | last post by:
Sleep() function Sleep at lease 1 millisecond, there is a way to make a thread to sleep less than a millisecond? One way I know of is using performance counter which is not really sleep ( loop and...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.