【问题标题】:why the child process doesn't resume after I send SIGCONT to it? [duplicate]为什么在我向它发送 SIGCONT 后子进程没有恢复? [复制]
【发布时间】:2020-09-11 15:24:09
【问题描述】:

我是 C 和流程的新手,如果我的问题听起来很愚蠢,我很抱歉。以下是我的代码:

int main()
{   
    pid_t pid; 
    
    if ((pid = fork()) == 0){
        pause();
        printf("child process restarts!\n");
        exit(0);
    }

    kill(pid, SIGCONT);
    exit(0);
}

所以在父进程中,我确实向子进程发送了SIGCONT,所以被pause 挂起的子进程应该恢复,但是我没有看到任何输出,这意味着子进程没有重新启动?那么如何恢复子进程呢?

【问题讨论】:

  • pause 函数有什么作用?
  • from man:“pause() 导致调用进程(或线程)休眠,直到发出终止进程或导致调用信号捕获函数的信号。”。我认为SIGCONT没有默认的信号捕获功能
  • 这能回答你的问题吗? -SIGCONT does not continue paused process?

标签: c linux process


【解决方案1】:

就像man pause中说的,你需要抓住SIGCONT

   pause() causes the calling process (or thread) to sleep until a
   signal is delivered that either terminates the process or causes the
   invocation of a signal-catching function.

在父进程中添加 sleep(1) 以确保在调用 kill() 时子进程已完全初始化,您将获得所需的输出:

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

static void catch( int sig ) { }

int main()
{
    pid_t pid;

    if ((pid = fork()) == 0){
        signal( SIGCONT, catch );
        pause();
        printf("child process restarts!\n");
        exit(0);
    }

    sleep( 1 );
    kill(pid, SIGCONT);
    exit(0);
}

由于SIGCONTSIGSTOPSIGTSTP 的特殊含义,您可能希望将另一个信号与pause() 一起使用,例如SIGUSR1

【讨论】:

    猜你喜欢
    • 2022-06-15
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    相关资源
    最近更新 更多