How can I interrupt sleep() using a signal handler?
+1
A:
From sleep function man page:
"The sleep() function suspends execution of the calling thread until either seconds seconds have elapsed or a signal is delivered to the thread and its action is to invoke a signal-catching function or to terminate the thread or process. System activity may lengthen the sleep by an indeterminate amount."
So, if a signal is send to the application, its execution is resumed from the instruction after the sleep call.
kist
2010-07-01 11:42:28
And by sending a signal, it means using alarm()?
sleep
2010-07-01 11:45:08
Yep. To send the signal for an external application you can use the kill(process_id, sig) function, if you are in a *unix OS.
kist
2010-07-01 11:55:20
@sleep: No, don't use `alarm`. Its manpage states: "sleep(3) may be implemented using SIGALRM; mixing calls to alarm() and sleep(3) is a bad idea." To send a signal, simply use `raise(3)`, `kill(2)`, `killpg(2)`, `pthread_kill(3)` or `tgkill(2)`. Use one of the user-defined signals `SIGUSR1` or `SIGUSR2`. See also `signal(7)`.
Philipp
2010-07-01 12:01:35