如何使用信号处理程序停止和恢复子进程?

How can I use signal handlers to stop and resume a child process?

我尝试使用信号处理函数停止和恢复子进程,我的代码如下,但结果似乎没有达到我想要的效果,为什么?

#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;
}

结果如下。子进程未成功恢复。谁能告诉我该怎么做?

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 **

child 捕捉到信号。在 child 中,global_pid 为零,因此您正在向进程组发送信号。