#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
pid_t pid;
void send_signal(int signum){
kill(pid, signum);
}
void init_signals(){
signal(SIGINT, send_signal);
signal(SIGTSTP, send_signal);
}
int main(){
init_signals();
pid = fork();
if(pid > 0){
//Parent Process
printf("PARENT: %d\n", getpid());
waitpid(pid, NULL, WUNTRACED);
printf("Parent out of wait, i think this is what you are expecting\n");
} else {
struct sigaction act = {{0}};
act.sa_handler = send_signal;
act.sa_flags = SA_RESETHAND;
sigaction(SIGTSTP, &act, NULL);
sigaction(SIGINT, &act, NULL);
perror("sigaction ");
printf("CHILD: %d\n", getpid());
// Child Process
while(1){
usleep(300000);
}
}
return 0;
}
我认为上面的代码可以达到您的目的。让我解释一下。
在您的代码 [How to redirect signal to child process from parent process? 中,您已经处理了信号,并且从处理程序上下文发送了相同的信号。当您按下 Ctrl + c 或 Ctrl + z 时,父母和孩子都会收到信号。现在根据处理程序代码
void send_signal(int signum) {
kill(pid, signum);
}
当处理程序将在父上下文中执行时pid 将等于child's pid,因此它将向子上下文发送信号但是当处理程序在子上下文中运行时pid 值将是0,因此它将信号发送给整个进程组,即父进程和子进程。这使您的代码可以无限次递归地运行处理程序。因此,您没有得到想要的结果。
我已经修改了两件事以获得预期的结果。
子上下文
在子上下文中,在进入信号处理程序时将信号动作恢复为默认值,以便当子级第二次接收到信号时,可以执行信号默认动作。
父上下文
使用 waitpid() 代替 wait()。
pid_t waitpid(pid_t pid, int *status, int options);
The waitpid() system call suspends execution of the calling process until a child specified by pid argument has changed state. By default, waitpid() waits only for terminated children, but this behavior is modifiable via the options argument.
`WUNTRACED` also return if a child has stopped
由于WUNTRACED父进程将在子进程停止或终止时返回。
我希望它对你有用,请问我是否没有。