简体   繁体   English

暂停时向子进程发送信号

[英]Sending signal to child process on pause

My aim is to pause child process after its creation.我的目标是在创建子进程后暂停子进程。 Then I want to interrupt pause and I do it by a signal that I send from the parent process via kill() .然后我想中断暂停,并通过从父进程通过kill()发送的信号来完成。 The main question is where I should put pause() in this situation.主要问题是在这种情况下我应该把pause()放在哪里。

This is what I've got so far:这是我到目前为止所得到的:

int main()
{
    int pid;

    if ((pid = fork()) < 0) //error occured
    {
        perror("fork");
        exit(1);
    }

    if (pid == 0) //___Child___
    {
        signal(SIGHUP, sighup);
        printf("Tryna work with pause():\n");
        printf("Before pause:\n");
        pause();
        printf("After pause.\n");

        for(;;) //infinite loop
            ;
    }

    else //___Parent___
    { /* pid hold id of child */
        printf("\nPID=%d\n\n",pid);
        printf("\nPARENT: sending SIGHUP\n\n");
        kill(pid, SIGHUP);

        sleep(3); //wait 3 secs
    }
}

void sighup()
{
    signal(SIGHUP, sighup) //reset signal;
    printf("In handler...");
}

Call it in Linux terminal like main 5053 and the output is:在 Linux 终端中调用它,如main 5053和 output 是:

PID=1939

PARENT: sending SIGHUP

The only thing to change is move kill after the sleep to give child process time to run.唯一需要改变的是在sleep后移动kill给子进程运行时间。

{
    ...
    printf("\nPARENT: sending SIGHUP\n\n");
    kill(pid, SIGHUP);

    sleep(3); //wait 3 secs
    ...
}

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

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