gpt4 book ai didi

linux - 如何将 C 中的信号从父进程传播到自己进程组中的子进程?

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:34 24 4
gpt4 key购买 nike

假设我有 10 个子进程,它们在 exec 之前通过 setpgid(0,0) 移动到它们自己的进程组。 (每个 child 也有自己的进程组中的 child 。)我的前台进程收到 ctrl-c SIGINT 信号,我想将它传播到所有子进程(所有子进程都在不同的组中)。如何做到这一点?

希望快速草稿能更好地解释我的问题。

void handler(int signal) {
// resend SIGINT to all childs but childs are in different pgid
}

int main(int argc, char* argv[]){

struct sigaction sa;
sa.sa_handler = &handler;

sigaction(SIGINT, &sa, NULL);

pid_t pid[SIZE];

int i = 0;
// if argv is ge 2;
for (;i < SIZE; i++) // SIZE is number of the elements in argv
{
pid[i] = fork();
if(pid[i] == 0)
{
setpgid(0,0);
// execv self here but with one less element in argv;
}
}
while(1){}; // infinity loop (waits for ctrl-c from foreground process)

// prints to the terminal pid and pgid
// waits here for all childs to finish and then close self
}

最佳答案

如何在主进程的信号处理程序中手动转发信号。也许您可以提供一些代码片段来阐明您所处的情况。

void signalHandler(int signum)
{
kill(child_pid,signum);
//other stuff
}

关于linux - 如何将 C 中的信号从父进程传播到自己进程组中的子进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43426684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com