1

When should I use sleep() and a reconfiguration of SIG_ALRM?

For example, I'm thinking of scheduling some task at some specific time. I could spawn a thread with an sleep() call inside and when sleep() returns, do some task, or I could specify a handler for SIG_ALRM and do the task inside the alarm interrupt. Do they take the same CPU usage and time? (besides the thread).

I've done some "tests" looking at the processes with ps command, showing me a CPU % and a CPU TIME of 0, but I'm wondering if I'm missing something or I'm looking at the wrong data.

BTW, I'm using Linux.

umläute
  • 28,885
  • 9
  • 68
  • 122
vicaba
  • 2,836
  • 1
  • 27
  • 45

1 Answers1

2

Note that what you do in a signal handler is very limited. You can only call certain POSIX functions and most of the C library is not allowed. Certainly not any C functions that might allocate or free memory or do I/O (you can use some POSIX I/O calls).

The sleeping thread might be the easiest way for you to go. If you use nanosleep it won't cause a signal at all, so you won't need to mess with handlers and such.

If your program is doing work, a common pattern is to have a central work loop, and in that loop you can check the time periodically to see if you should run your delayed job. Or you can skip checking the time and check a flag variable instead which your SIG_ALARM handler will set. Setting a sig_atomic_t variable is one of the things a signal handler is allowed to do.

CPU usage for a sleeping task is zero. It goes into the kernel as a timer event and is woken up to run when the timer expires.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • I've finally found answers out there (but no official doc.) that agree with your reply and the way I thought sleep worked (because I didn't believe that sleep was making a CPU usage after all, I think it's like a pause(); in a infinite loop that only can be waken up with a SIG_ALARM, so the OS scheduler is not giving any CPU time to the thread when sleeping). But I'd really appreciate if you can point me to official references that verify that for a GNU Linux. I'm trying to convince other people that sleep don't take any CPU time and usage and I'm sure they will want something official. – vicaba Dec 12 '14 at 10:31
  • 1
    Look, it's like this. The OS is a resource manager, and one of the manages resources is execution on a core. If a thread asks to not execute for some interval, (ie. calls sleep), the OS will not give it any cycles until the interval is over. Anything else would be insane. Don't worry about it. A sleeping thread is dead code and stack and, for a long sleep, may even get completely paged out. The only stuff used is a the Thread Control Block struct in the kernel and a pointer to it on the queue of timer events. No CPU cycles at all. – Martin James Dec 13 '14 at 20:30
  • 2
    Asking for 'official documentation' on this issue is like looking through a car manual to see if the car will run with no fuel in the tank. There will be no guidance in the manual for such a question. – Martin James Dec 13 '14 at 20:34