简体   繁体   English

子进程的SIGTSTP信号处理程序

[英]SIGTSTP signal handler for child process

So I'm trying to implement a signal handler for the SIGTSTP signal in a child process. 因此,我正在尝试在子进程中为SIGTSTP信号实现信号处理程序。

Basically what I'm trying to achieve is this: 基本上我想要达到的是:

  1. Start child Process 开始子进程
  2. Make the parent wait on the child process 让父级等待子进程
  3. call Sleep on the Child process for x seconds. 在“子进程”上调用“睡眠”达x秒。
  4. Before the sleep finishes execution, I want to send a Ctrl+Z signal. 在睡眠完成执行之前,我想发送Ctrl + Z信号。 This signal should stop the child process, but resume the parent process. 该信号应停止子进程,但恢复父进程。 The parent process should then know the process id of the stopped process. 然后,父进程应该知道已停止进程的进程ID。

I run it using the command: ./testsig sleep 10 我使用以下命令运行它:./testsig sleep 10

This is my code so far: 到目前为止,这是我的代码:

#include<stdlib.h>
#include<stdio.h>
#include<signal.h>
#include<string.h>



volatile sig_atomic_t last_proc_stopped;
volatile sig_atomic_t parent_proc_id;

 void handle_stp(int signum)
  {
    if(getpid()==parent_proc_id)
    {
        kill(parent_proc_id,SIGCONT);
        signal(SIGTSTP,handle_stp);
    }
    else
    {
        last_proc_stopped=getpid();
      kill(parent_proc_id,SIGCONT);
    }   
  }



  void main(int argc, char *argv[])
  {

    int childid=0,status;

    signal(SIGTSTP,SIG_IGN);

    parent_proc_id=getpid();


    childid=fork();

    if(childid>=0)
    {

      if(childid==0)//child
      {


        signal(SIGTSTP,handle_stp);
        strcpy(argv[0],argv[1]);
        strcpy(argv[1],argv[2]);
        argv[2]=NULL;
        printf("Passing %s %s %s\n",argv[0],argv[1],argv[2]);
        execvp(argv[0],argv);
      }

      else
      {
        wait(&status);

        printf("Last Proc Stopped:%d\n",last_proc_stopped);

      }
    }

    else
    {
      printf("fork failed\n");
    }
  }

Currently, it seems like ctrl+Z has some kind of effect (but definitely not the one I want!) 目前,ctrl + Z似乎具有某种效果(但绝对不是我想要的效果!)

When I hit ctrl+Z in the middle of the child executing sleep, the cursor continues to blink for the remainder of the (in my case 10) seconds, but control does not reach the parent process. 当我在执行睡眠的孩子中间按下ctrl + Z时,光标会继续闪烁剩余的时间(在我的情况下为10秒),但是控件没有到达父进程。

Without hitting ctrl+Z, control returns to the parent as expected. 在不按Ctrl + Z的情况下,控件将按预期方式返回到父级。

What am I doing wrong? 我究竟做错了什么?

I have seen this answer as well, but I'm not really able to understand it: 我也已经看到了这个答案,但是我真的不能理解它:

After suspending child process with SIGTSTP, shell not responding 使用SIGTSTP挂起子进程后,shell没有响应

You have two processes: 您有两个过程:

  • The parent, which ignores the signal, 忽略信号的父母

  • The child, which sets a handler, then execs another process - this will clear the code of the signal handler from the memory (the executed program will be loaded in place of the calling process), therefore will also clear the signal settings as well. 设置了处理程序的子进程将执行另一个进程-这将从内存中清除信号处理程序的代码(执行的程序将代替调用进程而加载),因此也将清除信号设置。 So, your signal handler function will never be called. 因此,您的信号处理函数将永远不会被调用。 Is it possible to signal handler to survive after “exec”? 是否可以向处理程序发出信号,使其在执行“ exec”之后继续生存?

What you could do to achieve your goal? 您可以做什么来实现自己的目标?

  • The parent should ignore the signal, 父母应该忽略这个信号,

  • The child should leave the default signal handling (which stops it), 子级应保留默认的信号处理功能(这将停止处理),

  • The parent should use waitpid() to see if the child process exited or was stopped, and act accordingly (this involves actually killing the stopped child process). 父级应使用waitpid()来查看子进程是否已退出或已停止,并采取相应措施(这实际上是杀死已停止的子进程)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM