【问题标题】:How to propagate signal in C from parent to child which are in own process group?如何将 C 中的信号从父进程组中的子进程传播到子进程?
【发布时间】:2017-04-15 13:33:29
【问题描述】:

假设我有 10 个子进程,它们在 exec 之前通过 setpgid(0,0) 移动到它们自己的进程组。 (每个孩子也有同样在他们自己的过程组中的孩子。) 我的前台进程获得 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
}

【问题讨论】:

  • 让每个父母给它的每个孩子发送一个SIGINT

标签: linux process signals sigint


【解决方案1】:

如何在主进程的信号处理程序中手动转发信号。也许你可以提供一些代码 sn-p 来澄清你所处的情况。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多