I tried to stop and resume the child process using the signal handler function, my code is as follows, but the result does not seem to achieve what I want, why?
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include<sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
}while(0)
#define MAXLINE 100
pid_t global_pid = -1; // global
void stop(int signo)
{
printf("catch the signo: %d, which is the 0xcc interruption\n",signo);
kill(global_pid,SIGSTOP);
printf("resume child process\n");
kill(global_pid,SIGCONT);
}
int main()
{
printf("before fork, pid = %d\n",getpid());
signal(5,(void(*)(int))stop);
global_pid = fork();
if(pid == -1)
{
ERR_EXIT("fork error\n");
}
if(pid > 0)
{
printf("This is parent pid = % d child pid = %d\n",getpid(),pid);
sleep(5);
}else if(pid == 0)
{
printf("This is child pid = %d parent pid = %d\n",getpid(),getppid());
asm volatile("int3");
printf("The child continued.\n");
}
return 0;
}
The result as follow. The child process was not successfully resumed. Can someone tell me what to do?
before fork, pid = 128943
This is parent pid = 128943 child pid = 128944
This is child pid = 128944 parent pid = 128943
catch the signo: 5, which is the 0xcc interruption
[1]+ Stopped **
The child catches the signal. In the child, global_pid is zero, so you are sending the signals to the process group.