linux - sleep() of parent process interrupted by c

2019-07-24 15:41发布

When a child process is fork() ed , then the parent process can wait() for the child process to complete . Suppose , just for experimenting , instead of wait() ing , if we make the parent process sleep() , then why does not it work ?

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

int main()
{
    pid_t child_id ;

    child_id = fork() ;

    if (child_id == 0)
    {
        printf("\nChild process");
        printf("\nChild process exiting");
    }
    else
    {
        printf("\nParent process");
        sleep(10);
        printf("\nParent process exiting");
    }
}

I guess SIGCHLD signal is causing the the parent process to wake from sleep() . But why , it is a child process , they have different address space and resources , then how can it interfere in the matters of parent process ?

标签: c linux sigchld
2条回答
兄弟一词,经得起流年.
2楼-- · 2019-07-24 16:12

Beware differences between systems. With this minor adaptation of your code, running on Mac OS X 10.9, the child dying does not affect the sleep(10) in the parent:

Parent process

Child process
Child process exiting 1384590368

Parent process exiting 1384590378

As you can see, the parent exited 10 seconds or so later than the child did.

#include <stdio.h>
#include <unistd.h>
#include <time.h>

int main(void)
{
    pid_t child_id;

    child_id = fork();

    if (child_id == 0)
    {
        printf("\nChild process");
        printf("\nChild process exiting %ld\n", (long)time(0));
    }
    else
    {
        printf("\nParent process\n");
        sleep(10);
        printf("\nParent process exiting %ld\n", (long)time(0));
    }
}

I got the same behaviour on a VM running an ancient version of Linux (2.6.16.60 kernel from 2008); the parent died 10 seconds after the child did.

So, if the behaviour you are asking "why does it not work?" is 'the parent exits immediately child dies', then your code does not prove that it does exit on either of two systems. I can't categorically say the parent doesn't die promptly on your system, but it would be unexpected.

You might find this program useful for studying the behaviour of SIGCHLD signals:

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

static siginfo_t sig_info;
static volatile sig_atomic_t sig_num;
static void *sig_ctxt;

static void catcher(int signum, siginfo_t *info, void *vp)
{
    sig_num = signum;
    sig_info = *info;
    sig_ctxt = vp;
}

static void set_handler(int signum)
{
    struct sigaction sa;
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = catcher;
    sigemptyset(&sa.sa_mask);

    if (sigaction(signum, &sa, 0) != 0)
    {
        int errnum = errno;
        fprintf(stderr, "Failed to set signal handler (%d: %s)\n", errnum, strerror(errnum));
        exit(1);
    }
}

static void prt_interrupt(FILE *fp)
{
    if (sig_num != 0)
    {
        fprintf(fp, "Signal %d from PID %d\n", sig_info.si_signo, (int)sig_info.si_pid);
        sig_num = 0;
    }
}

static void five_kids(void)
{
    for (int i = 0; i < 5; i++)
    {
        pid_t pid = fork();
        if (pid < 0)
            break;
        else if (pid == 0)
        {
            printf("PID %d - exiting with status %d\n", (int)getpid(), i);
            exit(i);
        }
        else
        {
            int status = 0;
            pid_t corpse = wait(&status);
            printf("Child: %d; Corpse: %d; Status = 0x%.4X\n", pid, corpse, (status & 0xFFFF));
            prt_interrupt(stdout);
            fflush(0);
        }
    }
}

int main(void)
{
    printf("SIGCHLD set to SIG_DFL\n");
    signal(SIGCHLD, SIG_DFL);
    five_kids();

    printf("SIGCHLD set to SIG_IGN\n");
    signal(SIGCHLD, SIG_IGN);
    five_kids();

    printf("SIGCHLD set to catcher()\n");
    set_handler(SIGCHLD);
    five_kids();

    return(0);
}

On Mac OS X 10.9 again, it produced:

SIGCHLD set to SIG_DFL
PID 52345 - exiting with status 0
Child: 52345; Corpse: 52345; Status = 0x0000
PID 52346 - exiting with status 1
Child: 52346; Corpse: 52346; Status = 0x0100
PID 52347 - exiting with status 2
Child: 52347; Corpse: 52347; Status = 0x0200
PID 52348 - exiting with status 3
Child: 52348; Corpse: 52348; Status = 0x0300
PID 52349 - exiting with status 4
Child: 52349; Corpse: 52349; Status = 0x0400
SIGCHLD set to SIG_IGN
PID 52350 - exiting with status 0
Child: 52350; Corpse: -1; Status = 0x0000
PID 52351 - exiting with status 1
Child: 52351; Corpse: -1; Status = 0x0000
PID 52352 - exiting with status 2
Child: 52352; Corpse: -1; Status = 0x0000
PID 52353 - exiting with status 3
Child: 52353; Corpse: -1; Status = 0x0000
PID 52354 - exiting with status 4
Child: 52354; Corpse: -1; Status = 0x0000
SIGCHLD set to catcher()
PID 52355 - exiting with status 0
Child: 52355; Corpse: -1; Status = 0x0000
Signal 20 from PID 52355
Child: 52356; Corpse: 52355; Status = 0x0000
PID 52356 - exiting with status 1
Child: 52357; Corpse: -1; Status = 0x0000
PID 52357 - exiting with status 2
Signal 20 from PID 52356
Child: 52358; Corpse: 52357; Status = 0x0200
Signal 20 from PID 52357
PID 52358 - exiting with status 3
Child: 52359; Corpse: 52356; Status = 0x0100
PID 52359 - exiting with status 4

The behaviour on Linux is similar — not quite identical:

SIGCHLD set to SIG_DFL
PID 14645 - exiting with status 0
Child: 14645; Corpse: 14645; Status = 0x0000
PID 14646 - exiting with status 1
Child: 14646; Corpse: 14646; Status = 0x0100
PID 14647 - exiting with status 2
Child: 14647; Corpse: 14647; Status = 0x0200
PID 14648 - exiting with status 3
Child: 14648; Corpse: 14648; Status = 0x0300
PID 14649 - exiting with status 4
Child: 14649; Corpse: 14649; Status = 0x0400
SIGCHLD set to SIG_IGN
PID 14650 - exiting with status 0
Child: 14650; Corpse: -1; Status = 0x0000
PID 14651 - exiting with status 1
Child: 14651; Corpse: -1; Status = 0x0000
PID 14652 - exiting with status 2
Child: 14652; Corpse: -1; Status = 0x0000
PID 14653 - exiting with status 3
Child: 14653; Corpse: -1; Status = 0x0000
PID 14654 - exiting with status 4
Child: 14654; Corpse: -1; Status = 0x0000
SIGCHLD set to catcher()
PID 14655 - exiting with status 0
Child: 14655; Corpse: 14655; Status = 0x0000
Signal 17 from PID 14655
PID 14656 - exiting with status 1
Child: 14656; Corpse: 14656; Status = 0x0100
Signal 17 from PID 14656
PID 14657 - exiting with status 2
Child: 14657; Corpse: 14657; Status = 0x0200
Signal 17 from PID 14657
PID 14658 - exiting with status 3
Child: 14658; Corpse: 14658; Status = 0x0300
Signal 17 from PID 14658
PID 14659 - exiting with status 4
Child: 14659; Corpse: 14659; Status = 0x0400
Signal 17 from PID 14659
查看更多
狗以群分
3楼-- · 2019-07-24 16:25

Please read carefully the man pages of fork(2), execve(2), wait(2) ...

The wait syscall do much more than just passively waiting for a child process. It cleans internal kernel state to avoid zombie processes.

Use also strace(1) e.g. as strace -f on your program.

And take several hours to read a good book like e.g. Advanced Linux Programming. Understanding processes need many hours, and we have not that much time to teach it to you. Please read books and continue experimenting like you do! Also, take some time to read the source code of free software (like the source code of some shell -e.g. bash or sash)

BTW, you program is wrong on another point: you should always test for failure of fork (so always handle the three possible returns of fork: ==0 in child process, >0 in parent process, <0 on failure). Consider using ulimit (which calls setrlimit(2) inside the shell) to trigger such an error condition for testing purposes.

查看更多
登录 后发表回答