简体   繁体   中英

How to kill parent process before the completion of the child?

I need to parent process ended earlier than the child process. This is the code:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
main()
{
    extern int errno;
    extern char* sys_errlist[];
    pid_t pid;
    int rv;
    switch(pid=fork()) {
        case -1:
            perror("fork");
            exit(1);
        case 0:
            printf(" CHILD: This is child-process!\n");
            printf(" CHILD: My PID -- %d\n", getpid());
            printf(" CHILD: My parent PID -- %d\n", getppid());
            sleep(3);
            printf(" CHILD: My new parent PID -- %d\n", getppid());//I think, that getppid() return 1 
            printf(" CHILD: Exit!\n");
            exit(rv);
        default:
            printf("PARENT: This is parent-process!\n");
            printf("PARENT: My PID -- %d\n", getpid());
            printf("PARENT: My child PID %d\n",pid);
            printf("PARENT: Exit!\n");
    }
}

But I'm not sure that it is correct. The output of this program:

PARENT: This is parent-process!
PARENT: My PID -- 943
PARENT: My PID -- 943
PARENT: My child PID -- 944
PARENT: Exit!
CHILD: This is child-process!
CHILD: My PID -- 944
CHILD: My parent PID -- 1
CHILD: My new parent PID -- 1
CHILD: Exit!

Thanks! PS. I'm sorry for my English

The output looks fine. The parent process appears to be exiting; the child process is not affected by the parent dying and continues to print its output too.

You can see that the child's parent changed when the parent exited; orphans are inherited by process ID 1, which is simply sitting there waiting for children to die. PID 1 is classically the init process (though on Mac OS X, for example, it is /sbin/launchd ).

It looks like your parent process exited before the child even started sleeping. This is possible because your parent doesn't do anything that takes a significant amount of time after creating the child. It just prints and exits. So the child saw PPID 1 even before the sleep.

If you add a sleep(1) to the end of the parent, there will be a much better chance that the child will print 2 different PPIDs.

If that doesn't work, you'll have to do some actual synchronization to get your processes running the steps in the right order. Sleeps aren't great for that because you can't control how much time the other stuff takes (including time that your processes aren't running at all because something else took the CPU).

Perhaps what you seem to lack is some form of synchronisation between the two processes that can be acheived for instance via signals, pipes, etc ... you choose

To ensure the order of execution:

  • the parent after the fork() print its stuff, tell the child to wake up and exit immeditly after
  • the child wait before it's waked up and then print its own stuff on his turn.

sleep() is not a synchronization primitive (but can be handy for simple stuff)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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