I am trying to write simple program that uses real-time signals in Linux. But I encountered strange behaviour, first the code:
#include<signal.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include"errhandling.h"
#include<string.h>
#include<errno.h>
#include<unistd.h>
void baz(int sig,siginfo_t* info,void *context)
{
if (sig==SIGUSR1)
printf("SIGUSR1 %d\n",info->si_value.sival_int);
else if(sig==SIGRTMIN)
printf("SIGRTMIN %d\n",info->si_value.sival_int);
else
printf("SIGRTMIN+1 %d\n",info->si_value.sival_int);
return ;
}
void sig_output()
{
sigset_t set;
sigprocmask(0,NULL,&set);
printf("blokowane sa: ");
if (sigismember(&set,SIGUSR1))
printf("SIGUSR1 ");
if(sigismember(&set,SIGUSR2))
printf(" SIGUSR2");
printf("\n");
return ;
}
int received=0;
int usr2=0;
void foo(int sig)
{
return ;
}
void usr1_handler(int sig)
{
printf("usr1_handler\n");
//++received;
}
void usr2_handler(int sig)
{
usr2=1;
}
int main(int argc,char **argv)
{
int i=0;
pid_t pid=getppid();
struct sigaction a;
struct sigaction a2;
a.sa_flags=SA_SIGINFO;
sigset_t set;
sigemptyset(&set);
//sigaddset(&set,SIGRTMAX);
sigemptyset(&(a.sa_mask));
sigemptyset(&(a2.sa_mask));
a.sa_sigaction=baz;
sigaction(SIGRTMIN,&a,NULL);
a2.sa_handler=usr1_handler;
sigaction(SIGRTMIN+1,&a2,NULL);
//sigprocmask(SIG_SET,&(a.sa_mask),NULL);
while(!usr2)
{
printf("while\n");
sigsuspend(&set);
}
//pause()
printf("after while\n");
return EXIT_SUCCESS;
}
when I run this program and it enters this loop with sigsuspend and I send to this program SIGRTMIN everything goes ok - handler executes and it waits for another signal, but when I send it SIGRTMIN+1 I get segmentation fault.
It seems that for real-time signals I need to use this extended handler with 3 arguments, but why? Is it specified somewhere? I run this program on my friend OpenSUSE 12.1 and I don't get segmentation fault for SIGRTMIN+1, but on my Xubuntu 11.10, when I send SIGRTMIN+1 I am getting segmentation fault. Is it problem with my system? Or is it implementation dependent?
Don't use printf() and friends from inside a signal handler. Don't be stubborn... Here's a substitute.
UPDATE: this appears to work:
It seems you are missing to assign a handler to
a2.sa_sigaction
.In general it is not good idea to refer to signals by a raw integer values, as the definition for the various signals might be platform specific.
Update:
Make sure thestruct sigaction
structures are initialised properly by for examplememset()
ing them to 0.