我试图编写一个在父进程和子进程之间发送信号的程序。 这就是我到目前为止所做的:
void parent_sig(int signo){
if(signo == SIGINT){
printf("*** Parent received SIGINT ***\n");
}else if(signo == SIGTSTP){
//first time
signal(SIGUSR1, c_sig_usr);
//second time
printf("*** Parent received SIGTSTP ***\n");
exit(0);
}else if(signo == SIGUSR1){
printf("*** Parent received SIGUSR1 ***\n");
sleep(3);
signal(SIGUSR1, c_sig_usr);
}else{
pause();
}
}
void child_sig(int signo){
if(signo == SIGINT){
printf("*** Child received SIGINT ***\n");
}else if(signo == SIGTSTP){
printf("*** Child received SIGTSTP ***\n");
}else if(signo == SIGUSR1){
printf("*** Child received SIGUSR1 ***\n");
sleep(3);
signal(SIGUSR1, p_sig_usr);
}else{
pause();
}
}
int main(void)
{
pid_t child, parent;
parent = getpid();
struct sigaction p_sig;
p_sig.sa_handler = &parent_sig;
if((child = fork()) < 0) {
perror("**ERROR**: failed to fork process");
exit(EXIT_FAILURE);
}else if (child == 0){
struct sigaction c_sig;
c_sig.sa_handler = &child_sig;
}else{
}
return 0;
}
我想要
我需要更改/添加此代码才能使其正常工作?
答案 0 :(得分:0)
signal()是用于配置在接收到特定信号时应调用函数的调用。
kill()是您用来发送
信号的电话