SIGQUIT,通过快速、连续的调用处理第二个信号
SIGQUIT, Handling the Second signal with quick, successive calls
我试图忽略第一个 SIGQUIT
到 child 线程...
具体来说,在每次调用后写入控制台并在第二次调用时退出。
我已经尝试过此代码的变体;即每个类似的堆栈溢出线程分成两个函数。不幸的是,对于 child 的处理程序,parent 循环似乎是 'too quick'。
SIGQUIT
的处理程序:
int goes = 0;
void install_handler(){
write(1, "SIGQUIT\n", 8);
if (goes == 1) exit(0);
goes += 1;
}
只是 driver:
{
if((child_pid = fork()) < 0) exit(1);
if (child_pid == 0 ) {
signal(SIGQUIT, install_handler);
while(1){}
} else {
sleep(1);
for (int i = 0; i < 3; i++) {
write(1, "KILLING\n", 8);
kill(child_pid, SIGQUIT);
// sleep(1);
}
}
return 0;
}
我希望输出为 ~
KILLING
SIGQUIT
KILLING
SIGQUIT
KILLING
...
输出(在 parent 循环中没有休眠)是
KILLING
KILLING
KILLING
SIGQUIT
SIGQUIT
这不是我要达到的顺序。什么是确保在 sigquit 的每个信号上我们可以保证 "SIGQUIT" 在不修改 main()
方法的情况下写入控制台的有效方法?
你的问题有一个有趣的答案。
但首先,虽然很烦人,但我不允许发表评论,所以我希望我没有犯任何错误。
int goes = 0;
int child_pid = 0;
void install_handler(){
if (child_pid == 0) exit(0); //to exit the program once done
write(1, "SIGQUIT\n", 8);
kill(child_pid, SIGQUIT);
write(1, "KILLING\n", 8);
//you can put it after the if depending on the output
if (goes == 1) {
kill(0, SIGQUIT);
exit(0);
} //this way the program will end himself at the end
// i guess the fact that goes was indented is a mistake
goes += 1;
}
如您所见,我将 kill 放入处理程序中,这样 kill 将不会被发送,直到收到另一个 kill。
(注意我在这里是如何调用 pid 的吗?)
{
if((child_pid = fork()) < 0) exit(1);
if (child_pid == 0 ) {
while(1){
signal(SIGQUIT, install_handler);
}
} else {
sleep(1);
kill(child_pid, SIGQUIT);
write(1, "KILLING\n", 8);
for (int i = 0; i < 3; i++) {
signal(SIGQUIT, install_handler);
wait(); //you might even not need the wait!
// sleep(1);
}
}
return 0;
}
我强烈建议使用 sigaction 而不是 signal,它更精确。
我必须说我不确定我的答案,因为我在这台计算机上没有 c 但通常我使用 sigaction 并等待(而不是睡眠)
你也不需要调用它 child_pid 因为它是而且不是子 pid,因为它是两个进程
soooo...我希望我有用!
我试图忽略第一个 SIGQUIT
到 child 线程...
具体来说,在每次调用后写入控制台并在第二次调用时退出。
我已经尝试过此代码的变体;即每个类似的堆栈溢出线程分成两个函数。不幸的是,对于 child 的处理程序,parent 循环似乎是 'too quick'。
SIGQUIT
的处理程序:
int goes = 0;
void install_handler(){
write(1, "SIGQUIT\n", 8);
if (goes == 1) exit(0);
goes += 1;
}
只是 driver:
{
if((child_pid = fork()) < 0) exit(1);
if (child_pid == 0 ) {
signal(SIGQUIT, install_handler);
while(1){}
} else {
sleep(1);
for (int i = 0; i < 3; i++) {
write(1, "KILLING\n", 8);
kill(child_pid, SIGQUIT);
// sleep(1);
}
}
return 0;
}
我希望输出为 ~
KILLING
SIGQUIT
KILLING
SIGQUIT
KILLING
...
输出(在 parent 循环中没有休眠)是
KILLING
KILLING
KILLING
SIGQUIT
SIGQUIT
这不是我要达到的顺序。什么是确保在 sigquit 的每个信号上我们可以保证 "SIGQUIT" 在不修改 main()
方法的情况下写入控制台的有效方法?
你的问题有一个有趣的答案。 但首先,虽然很烦人,但我不允许发表评论,所以我希望我没有犯任何错误。
int goes = 0;
int child_pid = 0;
void install_handler(){
if (child_pid == 0) exit(0); //to exit the program once done
write(1, "SIGQUIT\n", 8);
kill(child_pid, SIGQUIT);
write(1, "KILLING\n", 8);
//you can put it after the if depending on the output
if (goes == 1) {
kill(0, SIGQUIT);
exit(0);
} //this way the program will end himself at the end
// i guess the fact that goes was indented is a mistake
goes += 1;
}
如您所见,我将 kill 放入处理程序中,这样 kill 将不会被发送,直到收到另一个 kill。 (注意我在这里是如何调用 pid 的吗?)
{
if((child_pid = fork()) < 0) exit(1);
if (child_pid == 0 ) {
while(1){
signal(SIGQUIT, install_handler);
}
} else {
sleep(1);
kill(child_pid, SIGQUIT);
write(1, "KILLING\n", 8);
for (int i = 0; i < 3; i++) {
signal(SIGQUIT, install_handler);
wait(); //you might even not need the wait!
// sleep(1);
}
}
return 0;
}
我强烈建议使用 sigaction 而不是 signal,它更精确。 我必须说我不确定我的答案,因为我在这台计算机上没有 c 但通常我使用 sigaction 并等待(而不是睡眠) 你也不需要调用它 child_pid 因为它是而且不是子 pid,因为它是两个进程
soooo...我希望我有用!