#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
void new_op(int,siginfo_t*,void*);
int main(int argc,char**argv)
{
struct sigaction act;
int sig;
sig=atoi(argv[1]);
sigemptyset(&act.sa_mask);
act.sa_flags=SA_SIGINFO;
act.sa_sigaction=new_op;
if(sigaction(sig,&act,NULL) < 0)
{
printf("install sigal error\n");
}
while(1)
{
sleep(2);
printf("wait for the signal\n");
}
}
void new_op(int signum,siginfo_t *info,void *myact)
{
printf("receive signal %d", signum);
sleep(5);
}
说明,命令行参数为信号值,后台运行sigreceive signo &,可获得该进程的ID,假设为pid,然后再另一终端上运行kill -s signo pid验证信号的发送接收及处理。同时,可验证信号的排队问题。
这上面其实是一个帖子的内容,网址在这
http://www.ibm.com/developerworks/cn/linux/l-ipc/part2/index2.html
我想问的是,我的信号编号都是小于SIGRTMIN的,也就是不可靠信号,按理说,应该是发送多次,但只会响应一次的。但是我实验的结果是发送了三次,响应了2次,不知大虾能否解释一下,感激不尽!!!大虾 是在没有很多分了。。。。。
实验结果如下:
其中一个终端上的
[user@MagicLinux APUE]$ ./a.out 3 &
[1] 24336
[user@MagicLinux APUE]$ wait for the signal
wait for the signal
wait for the signal
wait for the signal
wait for the signal
wait for the signal
receive signal 3receive signal 3wait for the signal
wait for the signal
wait for the signal
wait for the signal
wait for the signal
另外一个终端
[user@MagicLinux ~]$ kill -s 3 24336
[user@MagicLinux ~]$ kill -s 3 24336
[user@MagicLinux ~]$ kill -s 3 24336
[user@MagicLinux ~]$ kill 24336
[user@MagicLinux ~]$