[英]SIGTSTP Handler terminates both parent and child process
My Goal: I have a simple c program that should overwrite the default SIGTSTP handler with my own, and send a SIGTSTP only to the child process. 我的目标:我有一个简单的c程序,该程序应使用我自己的程序覆盖默认的SIGTSTP处理程序,并仅将SIGTSTP发送给子进程。
My Issue: The kill call within the SIGTSTP handler stops the parent process, and exits my program (not just the child). 我的问题: SIGTSTP处理程序中的kill调用停止了父进程,并退出了我的程序(不仅仅是子进程)。 What am I doing wrong?
我究竟做错了什么?
Edit: This problem seems to only happen when I compile and run my code using the following make command: gcc -o program *.c -lreadline && ./program
. 编辑:仅当我使用以下make命令编译并运行代码时,才会出现此问题:
gcc -o program *.c -lreadline && ./program
。 It seems (the make
process?) is terminated because my output contains the following line upon ctrl-z: gcc -o sandbox *.c -lreadline && ./sandbox
Is there a way to both get my program to have the desired functionality and use make? 似乎(
make
process?)已终止,因为我的输出在ctrl-z上包含以下行: gcc -o sandbox *.c -lreadline && ./sandbox
是否有办法使我的程序具有所需的功能,并且使用make?
My Code: 我的代码:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <readline/readline.h>
int child;
void handler();
static void SIGTSTP_Handler()
{
if (child != 0) {
kill(child, SIGTSTP);
}
}
int main(void)
{
signal(SIGTSTP, SIGTSTP_Handler);
child = fork();
if (child == 0) {
setpgid(0, getpid());
printf("CHILD's PID ::: [ %d ]\n", getpid());
printf("CHILD's GROUP ::: %d\n", getpgrp());
execlp("sleep", "sleep", "30", NULL);
}
else {
setpgid(child, child);
int status;
printf("CHILD's PID (From Parent Perspective) ::: [ %d ]\n", child);
printf("PARENT's PID ::: %d\n", getpid());
printf("PARENT's GROUP ::: %d\n", getpgrp());
waitpid(child, &status, WUNTRACED | WCONTINUED);
}
while (1);
}
The issue was caused because the make
command that I started my program using was terminated by ctrl-z
. 引起该问题的原因是,我启动程序所使用的
make
命令已由ctrl-z
终止。 To fix the problem you can either: 要解决此问题,您可以:
OLD Problematic Make Command: 有问题的旧命令:
gcc -o program *.c && ./program
Potential Solutions: 潜在解决方案:
(1) Remove the && ./program
line from the make command (1)从make命令中删除
&& ./program
行
(2) Compile and run your program without using make (2)不使用make编译并运行程序
I am unsure if there is anyway to still use start the program using make if you are hoping to keep your main program running in the case of a SIGTSTP
signal 我不确定是否仍然可以使用make启动程序,如果您希望在
SIGTSTP
信号的情况下保持主程序运行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.