2017-04-15 67 views
0

假設我有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

回答

0

怎麼樣在主進程的信號處理程序中手動轉發信號。也許你可以提供一些代碼片段來澄清你所處的情況。

void signalHandler(int signum) 
{ 
    kill(child_pid,signum); 
    //other stuff 
} 
相關問題