1

In the highest ranked answer of question:

If computers start counting at 0, why does the init process have a pid of 1?

it was stated, that each process has a PPID (parent).

However I read (will provide link later), that there are a lot of processes which have no parents.

Could anybody put the contradictory statements into a reasonable context?

2

Processes always have a parent process. However, what process becomes the new parent when an existing process dies is not necessarily PID 1. Here's how Linux does it:

/*
 * When we die, we re-parent all our children, and try to:
 * 1. give them to another thread in our thread group, if such a member exists
 * 2. give it to the first ancestor process which prctl'd itself as a
 *    child_subreaper for its children (like a service manager)
 * 3. give it to the init process (PID 1) in our pid namespace
 */
static struct task_struct *find_new_reaper(struct task_struct *father,
                       struct task_struct *child_reaper)
{
    struct task_struct *thread, *reaper;

    thread = find_alive_thread(father);
    if (thread)
        return thread;

    if (father->signal->has_child_subreaper) {
        unsigned int ns_level = task_pid(father)->level;
        /*
         * Find the first ->is_child_subreaper ancestor in our pid_ns.
         * We can't check reaper != child_reaper to ensure we do not
         * cross the namespaces, the exiting parent could be injected
         * by setns() + fork().
         * We check pid->level, this is slightly more efficient than
         * task_active_pid_ns(reaper) != task_active_pid_ns(father).
         */
        for (reaper = father->real_parent;
             task_pid(reaper)->level == ns_level;
             reaper = reaper->real_parent) {
            if (reaper == &init_task)
                break;
            if (!reaper->signal->is_child_subreaper)
                continue;
            thread = find_alive_thread(reaper);
            if (thread)
                return thread;
        }
    }

    return child_reaper;
}
1

When a process's Parent dies, the process can be said to "have no parent". When this happens, PPID is set to 1, the PID of init.

Every process returns a $STATUS value on exit. The Parent may do something with this value, but it MUST free the memory $STATUS is stored in, and release the process data in the kernel.

  • ty, just to clarify: when a process has a parent and a grandparent and the parent gets killed, does thee new parent will become INIT or the grandparent? – sharkant May 26 '17 at 16:46
  • init. This is why the traditional way to create a daemon is fork-fork-exec. – Philip Kendall May 26 '17 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.