SIGTSTP signal handler for a child process

So, I'm trying to implement a signal handler for a SIGTSTP signal in a child process.

I am basically trying to achieve this:

  • Start child process
  • Make parent wait for child process
  • Call Sleep on the child process for x seconds.
  • Before completing the completion of sleep, I want to send a signal Ctrl + Z. This signal should stop the child process, but resume the parent process to process. Then the parent process must know the process id has stopped.

I run it using the command: ./ testsig sleep 10

This is my code:

#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 that ctrl + Z has some kind of effect (but definitely not the one I want!)

ctrl + Z , , ( 10) , .

Ctrl + Z , .

?

, :

SIGTSTP

+4
1

:

?

+4

All Articles