简体   繁体   English

SIGTSTP处理程序会终止父进程和子进程

[英]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.

相关问题 子进程的SIGTSTP信号处理程序 - SIGTSTP signal handler for child process 确保父进程在子进程之前终止 - Ensuring that parent process terminates before child 在C语言中,如果父进程终止,那么子进程会自动终止吗? - In C, if a parent process terminates, then will the child process automatically terminate? 使用SIGTSTP挂起子进程后,shell没有响应 - After suspending child process with SIGTSTP, shell not responding 如何正确向子进程发送 SIGTSTP 信号? - How to correctly send SIGTSTP signal to a child process? 为 SIGTSTP 信号配置处理程序以显示消息但不终止当前进程 - configuring handler for SIGTSTP signal to display a message but not terminate the current process 使用SIGTERM对子进程调用kill会终止父进程,但使用SIGKILL调用它会使父进程保持活动状态 - Calling kill on a child process with SIGTERM terminates parent process, but calling it with SIGKILL keeps the parent alive 父进程创建子进程并且父进程和子进程运行相同程序的不同代码的程序 - A program where parent process creates a child process and both parent and child run same program different code 杀死派生的子进程会杀死SIGCHLD处理程序中的父进程 - Killing forked child process kills parent in SIGCHLD handler 僵尸进程的父进程终止后会发生什么? - What happens after the parent of zombie process terminates?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM