我正試圖編寫簡單的程序,在Linux中使用實時信號。但是,我遇到了奇怪的現象,首先代碼:Linux和實時信號
#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;
}
當我運行這個程序,它進入這個循環與sigsuspend我送這個節目SIGRTMIN一切正常的話 - 處理程序執行,並等待另一個信號,但當我發送SIGRTMIN + 1時,出現分段錯誤。
看來,實時信號我需要使用這個擴展的處理程序與3個參數,但爲什麼?它在某處指定了嗎?我在我的朋友OpenSUSE 12.1上運行這個程序,並且我沒有爲SIGRTMIN + 1得到分段錯誤,但是在我的Xubuntu 11.10上,當我發送SIGRTMIN + 1時,出現分段錯誤。我的系統有問題嗎?還是它依賴於實現?
第1課:在信號處理程序不使用printf()函數。 – wildplasser 2012-04-18 12:19:04
我知道這一點 - 這只是出於沮喪,因爲我想獲得一些信息 - 如果他進入我的信號處理程序。 – Andna 2012-04-18 12:21:11
用vsnprintf()把它替換成本地緩衝區和write(),你可能就沒事了。 – wildplasser 2012-04-18 12:22:35