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!)

ccc23 03-18-2015 08:00 AM

futex(0xc20f0, FUTEX_WAIT, 2, NULL) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
 
Hi, I a using Imx 53 Processor module & linux 3.12.13.

I am getting the futex error & program gets hang .

I am using signal alarm & settings are given below.

void alarm_handler()
{
printf(" \n alarm generated %d ",sigtmflag);
get_current_time();
getdata();
do_mytask();

if(sigtmflag==2)
{
printf(" \n alarm val %d ",sigtmflag);
sigtmflag=0;
logflag=1;
do_write_onserialport();
}
sigtmflag++;

}

void main()
{

struct itimerval delay;

// int i=0;
signal(SIGALRM,alarm_handler);

delay.it_value.tv_sec = 0;
delay.it_value.tv_usec = 400000;
delay.it_interval.tv_sec = 0;
delay.it_interval.tv_usec = 400000;

ret = setitimer(ITIMER_REAL, &delay, NULL);
if(ret)
{
perror("setitimer");
//return -1;
}

while(1)
{

do_something();
}

}

the program will hang up for infinite time & futex error comes.

please help for how to set the restart time when this error comes & how to solve this error.

These are some strace logs:

close(7) = 0
write(1, "\n", 1) = 1
open("/sys/class/gpio/export", O_WRONLY) = 7
write(7, "111", 3) = 3
close(7) = 0
open("/sys/class/gpio/gpio111/value", O_WRONLY) = 7
write(7, "1", 1) = 1
close(7) = 0
open("/sys/class/gpio/unexport", O_WRONLY) = 7
write(7, "111", 3) = 3
close(7) = 0
open("/sys/class/gpio/export", O_WRONLY) = 7
write(7, "4", 1) = 1
close(7) = 0
open("/sys/class/gpio/gpio4/value", O_WRONLY) = 7
write(7, "0", 1) = 1
close(7) = 0
open("/sys/class/gpio/unexport", O_WRONLY) = 7
write(7, "4", 1) = 1
close(7) = 0
write(1, " MAIN START ********aat 1000.03"..., 64) = 64
write(1, "\n", 1) = 1
--- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---
write(1, " status value hex in main 40 \n", 30) = 30
gettimeofday({946685166, 936965}, NULL) = 0
futex(0xc20f0, FUTEX_WAIT, 2, NULL) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
--- SIGINT {si_signo=SIGINT, si_code=SI_KERNEL} ---
+++ killed by SIGINT +++

NevemTeve 03-18-2015 08:26 AM

Please use [code] and [/code] tags.
Please don't put actual code into a signal-handler. In a signal-handler, setting a (volatile!) variable and/or writing a pipe is enough, the rest should be done in the main program.
Also don't use printf in signal-handler (sprintf+write is ok).

ccc23 04-16-2015 01:32 AM

Hi,

I am using a signal handler and a threads thread1 & thred2 running continuously .

In my signal handler I have just set the flag & in thread checking the flag to take action on it accordingly.

Also to display the status I have a display driver is running in main (Not in threads) .
After some time of execution I am getting the error given below .
open("/dev/klcd", O_WRONLY|O_NONBLOCK) = 6
write(1, "KLCD IOCTL Option: Print With Sp"..., 50) = 50
ioctl(6, 0x37, 0x7e8a2bb8) = 0
close(6) = 0
write(1, "KLCD User level Test Program \n", 30) = 30
write(1, "\n", 1) = 1
write(1, " val val in main fun 1503.803101"..., 64) = 64
write(1, "ioctl command debug: 7 \n", 24) = 24
open("/dev/klcd", O_WRONLY|O_NONBLOCK) = 6
write(1, "KLCD IOCTL Option: Print With Sp"..., 50) = 50
ioctl(6, 0x37 <unfinished ...>
+++ killed by SIGSEGV +++

I thing this is because my Kernel space driver gets interrupted by signal alarm ,is there any way to solve this error.

I am using Linux kernel 3.12 .

NevemTeve 04-16-2015 03:10 AM

Please don't say you want to write kernel device-drivers.

ccc23 04-16-2015 03:57 AM

Hi,

No, I have just inserted lcd display driver module .
and the error (ioctl(6, 0x37 <unfinished ...>)comes when it does ioctl operation and when it is doing the task which is in the kernel module I have inserted.

Is there any idea why it is getting interrupted & how to solve it.

NevemTeve 04-16-2015 04:21 AM

http://web.eecs.umich.edu/~sugih/pointers/gdbQS.html
http://undergraduate.csse.uwa.edu.au...gdb-intro.html

rtmistler 04-17-2015 01:27 PM

@ccc23 please be aware that resurrecting an old thread with a similar but different question is not the best choice here. If you felt that some of the older information was useful, please instead choose to refer back to this thread via a link as opposed to re-raising the thread.

ccc23 04-18-2015 01:41 AM

futex(0xc20f0, FUTEX_WAIT, 2, NULL) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
 
hi,

Sorry for reopening old thread,this futex error solved by replacing actual code by setting a flag in signal handler & removed all printf statement from handler function.

Thanks NevemTeve ,replaced printf by write .


All times are GMT -5. The time now is 09:31 AM.