LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Linux c alarm signal cannot receive (http://www.linuxquestions.org/questions/programming-9/linux-c-alarm-signal-cannot-receive-928109/)

knightmare1980 02-07-2012 09:46 AM

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

NevemTeve 02-07-2012 10:20 AM

Works for me:
Code:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <strings.h>
#include <unistd.h>

static void alarmhandler (int signum)
{
    char buff [512];
    size_t len;

    len= sprintf (buff, "got signal %d\n", signum);
    write (2, buff, len);
}

int main (void)
{
    struct sigaction sa;

    bzero(&sa, sizeof sa);
    sa.sa_handler = alarmhandler;
    sigaction (SIGINT, &sa, 0);
    sigaction (SIGALRM, &sa, 0);
    printf ("alarm(2) returned %d\n",alarm(2));
    sleep (10000);
    return 0;
}

$ ./alarmtest
alarm(2) returned 0
got signal 14

Nominal Animal 02-07-2012 10:31 AM

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,

NevemTeve 02-07-2012 11:00 AM

In my linux-box sleep(3) calls nanosleep(2)

Nominal Animal 02-07-2012 02:11 PM

Quote:

Originally Posted by NevemTeve (Post 4596119)
In my linux-box sleep(3) calls nanosleep(2)

You're right. glibc implements sleep(3) using nanosleep(2); in fact, it's the only sleep syscall (well, along with clock_nanosleep()) the Linux kernel provides. Apparently, the *BSDs also use nanosleep().

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.

occam25 11-16-2012 03:10 AM

Quote:

Originally Posted by Nominal Animal (Post 4596309)
There are much better ways to wait/block than sleep()/usleep()/nanosleep(); sleeping is only really useful when testing.

Sorry for re-open an old thread, but could you speak a bit more about that? what are the better ways you talk about? Why sleeping is only for testing?

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 02:10 PM.