PepeHands
PepeHands

Reputation: 1406

re-parenting stopped process

How does re-parenting of stopped process heppens? Why does stopped process just terminates after re-parenting?

More precisely, suppose I have a code like this

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h> 
#include <sys/syscall.h>
#include <stdio.h>


int main(void) {
    pid_t child;

    child = fork();

    if (child == 0) {
        int t = 0;
        while (true) {
            printf("%d. I'm %d, my parent is %d\n", t++, getpid(), getppid());
            sleep(1);
        }
    } else {
        printf("I'm the parent. My pid is %d\n", getpid());
        printf("Starting to wait for 30 seconds\n");
        sleep(30);
        printf("Done waiting, aborting\n");
    }
}

When I run this code the child process works and parent process just sleeps. After 30 seconds passed the parent process terminates and the child process now becomes a child of init and just continues running. Everything is normal.

But if I run this code and in first 30 seconds of it's execution I also run

kill -SIGSTOP <child_pid>

Then the child process stops (T state in ps xaf) and the parent process sleeps. After 30 second passed the parent process returns from sleep and just terminates (as it reached the end of main) but the child process instead of being re-parented to init in stopped state just terminates. I don't see it in ps xaf and if run lastcomm I see this output:

a.out             F  X equi     pts/5      0.00 secs Wed Mar 16 17:44

Why is this happening that stopped process dies after re-parenting? Is it possible in linux to re-parrent stopped process?

Upvotes: 4

Views: 99

Answers (1)

nzc
nzc

Reputation: 1876

From http://www.gnu.org/software/libc/manual/html_node/Job-Control-Signals.html

When a process in an orphaned process group (see Orphaned Process Groups) receives a SIGTSTP, SIGTTIN, or SIGTTOU signal and does not handle it, the process does not stop. Stopping the process would probably not be very useful, since there is no shell program that will notice it stop and allow the user to continue it. What happens instead depends on the operating system you are using. Some systems may do nothing; others may deliver another signal instead, such as SIGKILL or SIGHUP. On GNU/Hurd systems, the process dies with SIGKILL; this avoids the problem of many stopped, orphaned processes lying around the system.

See also: What's the difference between SIGSTOP and SIGTSTP?

Upvotes: 3

Related Questions