O'Reilly logo

Hands-On System Programming with Linux by Kaiwan N Billimoria

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sleeping correctly

Yes, even sleeping requires sufficient knowledge to perform correctly!  Often, your process has to enter a sleep state. We have all probably learned to use the sleep(3) API to do so:

#include <unistd.h>unsigned int sleep(unsigned int seconds);

As a simple example, let's say that the process must work this way (pseudo code follows):

<...>func_a();sleep(10);func_b();<...>

It's quite clear: the process must sleep for 10 seconds; the code shown should work. Is there a problem?

Well, yes, signals: what if the process enters the sleep, but three seconds into the sleep a signal arrives? The default behavior (meaning, unless signals are masked) is to handle the signal, and you would imagine, go back to sleep for the remaining time ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required