LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-07-2012, 09:46 AM   #1
knightmare1980
LQ Newbie
 
Registered: Feb 2012
Posts: 1

Rep: Reputation: Disabled
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
 
Old 02-07-2012, 10:20 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,828

Rep: Reputation: 507Reputation: 507Reputation: 507Reputation: 507Reputation: 507Reputation: 507
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
 
Old 02-07-2012, 10:31 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
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,

Last edited by Nominal Animal; 02-07-2012 at 10:33 AM.
 
1 members found this post helpful.
Old 02-07-2012, 11:00 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,828

Rep: Reputation: 507Reputation: 507Reputation: 507Reputation: 507Reputation: 507Reputation: 507
In my linux-box sleep(3) calls nanosleep(2)
 
Old 02-07-2012, 02:11 PM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by NevemTeve View Post
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.
 
2 members found this post helpful.
Old 11-16-2012, 03:10 AM   #6
occam25
LQ Newbie
 
Registered: Feb 2010
Posts: 7

Rep: Reputation: 0
Quote:
Originally Posted by Nominal Animal View Post
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!)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to receive the signal multiple times in user space without terminating it? pobitro Linux - Embedded & Single-board computer 0 06-19-2011 02:25 PM
Obtaining periodic AP receive signal strength measurements wood@addanc Linux - Wireless Networking 2 06-24-2007 11:43 AM
Gtk, glib alarm(time) signal? kalleanka Programming 4 07-09-2006 02:23 AM


All times are GMT -5. The time now is 03:46 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration