以下处理为进程是代码片断:误差信号在linux
#include<sys/types.h>
#include<stdio.h>
#include<signal.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
sig_atomic_t child1 ;
sig_atomic_t child2 ;
void child1_handler(int sig_num)
{
int ret ;
if(sig_num == SIGUSR2)
{
printf("\n Recieved sigusr2 for child1\n");
ret = kill(getppid() , SIGUSR2) ;
if(ret != 0)
kill(getpid() , SIGTERM);
}
else if(sig_num == SIGUSR1)
{
/* child 1 does something */
printf("\n Recieved sigusr1 for child1\n");
printf("\n child 1 is doing \n");
kill(child2 , SIGUSR2);
}
}
void child2_handler(int sig_num)
{
if(sig_num == SIGUSR2)
{
/* child2 does somethign */
printf("\n Recieved sigusr2 for child2\n");
printf("\n child2 is doing \n");
kill(child1 , SIGUSR2);
kill(getpid() , SIGTERM);
}
}
void parent_handler(int sig_num)
{
int ret ;
if(sig_num == SIGUSR2)
{
printf("\n Recieved sigusr2 for parent\n");
ret = kill(child1 , SIGUSR1) ;
if(ret != 0)
{
/* parent does something */
printf("\n Parent does something \n");
exit(0);
}
}
}
int main()
{
struct sigaction sa_parent , sa_child1 , sa_child2 ;
pid_t temp_id1 , temp_id2 ;
int temp ;
memset(&sa_parent , 0 , sizeof(sa_parent)) ;
memset(&sa_child1 , 0 , sizeof(sa_child1)) ;
memset(&sa_child2 , 0 , sizeof(sa_child2)) ;
/* parent */
printf(" \n Inside parent \n");
sa_parent.sa_handler = &parent_handler ;
sigaction(SIGUSR2 , &sa_parent , NULL);
temp_id1 = fork() ;
if(temp_id1 == 0)
{
child1 = getpid() ;
/* child1 */
printf("\n inside child1 \n");
sa_child1.sa_handler = &child1_handler ;
sigaction(SIGUSR1 , &sa_child1 , NULL) ;
sigaction(SIGUSR2 , &sa_child1 , NULL) ;
temp_id2 = fork() ;
if(temp_id2 == 0)
{
child2 = getpid() ;
/* child2 */
printf("\n inside child2 \n");
sa_child2.sa_handler = &child2_handler ;
sigaction(SIGUSR2 , &sa_child2 , NULL);
kill(child1 , SIGUSR2);
}
wait(&temp);
}
wait(&temp);
return 0 ;
}
我期待输出到描绘了第一
child1 is doing
child2 is doing
parent does something
然而所产生的输出是如下面给出的。 。
Inside parent
inside child1
inside child2
Recieved sigusr2 for child1
Recieved sigusr2 for parent
Recieved sigusr1 for child1
child 1 is doing
Recieved sigusr2 for child1
User defined signal 1
[ what is going wrong ? ]
请忽略“接收到的标志”和“内部...”行,因为它们是为了标记loca代码中的tions。
为什么你的代码以明显随机的方式缩进? – caf 2010-11-16 23:41:30
你可能会考虑使用sigemptyset()而不是memset()。 memset()不保证排除所有信号。这是一个附注,与你的问题不太相关,但我想我会指出。 – 2010-11-17 00:11:29