【发布时间】:2017-05-08 02:22:23
【问题描述】:
我有一个启动另一个程序(B) 的程序(A)。
我想要的是每次B接收到信号A时都会将此信号发送给B和B的所有子进程。我真的不知道如何在这里实现一些东西:
1)。如何确定该信号已发送到 B?
2)。如何将此信号保存在变量中?
3)。我如何循环直到 B 还活着?
int main() {
pid_t pid = fork();
int32_t num = 0;
if (pid == 0) {
static char *argv[] = {"main", NULL};
execv(argv[0], argv); //start program B
}
else{
while(/*B is alive*/){
//if program B receives signal
//I want to send this signal to B and all child processes,
//cause B doesn't handle any signals
if (/*B receives signal*/){
//save this signal to num.
kill(pid, num); //???
//send signal to parent
//useless cause it was already send to B?
fp = popen((("pgrep -P ") + string(num)).c_str(), "r");
//pgrep all child processes
std::vector<int> children;
while (fgets(buf, 128, fp) != NULL) //getting child pid
children.push_back(stoi(string(buf)));
for(auto a : children)
kill(a, num); //send signal to child
}
}
}
return 0;
}
【问题讨论】:
标签: c++ linux process signals signal-handling