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

问题描述:

假设我有10个子进程在exec之前通过setpgid(0,0)移动到它们自己的进程组。 (每个孩子也有孩子,这些孩子也在他们自己的进程组中。) 我的前台进程获取ctrl-c SIGINT信号,我想将它传播到所有子进程(所有孩子都在不同的组中)。怎么做?如何将C中的信号从父进程传播到自己进程组中的子进程?

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

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 
} 
+0

让每个父母给每个孩子发送一个SIGINT? – alk

怎么样在主进程的信号处理程序中手动转发信号。也许你可以提供一些代码片段来澄清你所处的情况。

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