![]() |
Linux c alarm signal cannot receive
cannot receive SIGALRM
#include <stdio.h> #include <stdlib.h> #include <signal.h> #include <time.h> #include <strings.h> void alarmhandler(int signum){ printf("get signal %d\n",signum); } int main() { struct sigaction sa; bzero(&sa, sizeof sa); sa.sa_handler = alarmhandler; sigaction(SIGINT, &sa, 0); sigaction(SIGALRM, &sa, 0); printf("%d\n",alarm(2)); sleep(10000); } ubuntu: Linux k-vbox-ubuntu 3.0.0-15-generic #26-Ubuntu SMP Fri Jan 20 17:23:00 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux |
Works for me:
Code:
#include <stdio.h>alarm(2) returned 0 got signal 14 |
First, printf() is not async-signal-safe.
See man 7 signal, section Async-signal-safe functions for the list of functions you can use in a signal handler. All other functions may or may not work. In particular, printf() et al. can sometimes silently fail, mangle, or discard data. (You can use low-level I/O from unistd.h, though; just not the stdio.h interface.) Second, alarm() (see man 2 alarm) and sleep() (see man 3 sleep) are mutually exclusive. Both alarm() and sleep() are implemented using the same mechanism, the alarm signal, in most POSIX systems -- including Linux. The same applies to usleep() (see man 3 usleep) as well. You can use nanosleep() (see man 2 nanosleep) to sleep while having an alarm, because nanosleep() will not interfere with the alarm signal (or any other signals). Hope this helps, |
In my linux-box sleep(3) calls nanosleep(2)
|
Quote:
While this does mean that on Linux and BSDs sleep() will actually not interfere with signals, including SIGALRM used by alarm(), it is only because the C library uses nanosleep() behind your back on these architectures. Do not expect sleep() and alarm() to work simultaneously on other architectures. I used to have an account on a Tru64 Unix box, and its documentation states alarm() is deactivated by a sleep() call. I seem to recall a test program to check this, from years ago; perhaps it was on that machine. Also, the relevant man pages explicitly mention that POSIX says using alarm() and sleep() at the same time yields undefined results. The rare cases where I've used a sleep in my real programs (with alarm() or any signals) I've used nanosleep() explicitly. In almost all cases there are much better ways to wait/block than sleep()/usleep()/nanosleep(); sleeping is only really useful when testing. So, while NevemTeve is certainly right, I stand by my two points. My latter point does not affect the execution of the code on knightmare1980's Ubuntu box, but using sleep() and alarm() at the same time is undefined by all the relevant standards. Better use nanosleep() explicitly instead: design your code, applying current knowledge, instead of just throwing together something that seems to work. Please. |
Quote:
I always use sleep and would like to learn a better way to wait/block :) (I read this because I have a problem with sleep and alarm and I am looking for a solution. After reading your post I will try nanosleep(), but I am interested in other ways to wait!) |
| All times are GMT -5. The time now is 12:14 AM. |