Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

As I understand it, process descriptors are stored in a doubly linked list data structure. But a fork can create multiple children for the same process, so that makes me think that there is a tree structure, because multiple processes will point to one parent process. Which is correct? Are process descriptors different from processes?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Your confusion stems from mixing two things: (1) keeping the process descriptors organized, and (2) the parent/child relationship.

You don't need the parent/child relationship to decide which process to run next, or (in general) which process to deliver a signal to. So, the Linux task_struct (which I found in linux/sched.h for the 3.11.5 kernel source) has:

struct task_struct __rcu *real_parent; /* real parent process */
struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */
/*
 * children/sibling forms the list of my natural children
 */

struct list_head children;  /* list of my children */
struct list_head sibling;   /* linkage in my parent's children list */

You're correct, a tree struct exists for the child/parent relationship, but it seems to be concealed in another list, and a pointer to the parent.

The famed doubly-linked list isn't obvious in the 3.11.5 struct task_struct structure definition. If I read the code correctly, the uncommented struct element struct list_head tasks; is the "organizing" doubly-linked list, but I could be wrong.

share|improve this answer
    
This makes me understand a little. That the tasks are organised in tree like structure(but parent points to a list of it's children list_head),, but process descriptors are in doubly linked list(this i read in a book), because it represents which process to run next. struct list_head children; -points to list of children. struct list_head sibling; -points to list of siblings. Is there a pointer to it's own parent in the process descriptor? like struct task_struct *parent; –  SmaugTheDragon Oct 16 '13 at 9:04

fork() creates a new process by copying the process descriptor. Hence the two processes do share (at least initially) some data, but as soon as one process starts changing it, copy-on-write mechanism make sure the change is localized only to the process that actually made it. It is the standard mechanism for process spawning on UNIX.

This of course creates a rather natural parent-children relation between processes, yet this is independent of the internal representation in the kernel. Process descriptors can be implemented as a linked list, tree, hash table or any other (more or less) suitable structure. All that one really needs is place in the kernel process descriptor that points to the parent process (and possibly child processes as well). Whether it is or isn't used as a key part of the structure is a design decision. One of the many things that come into play when deciding such a thing is for example what happens once the parent process exits - on UNIX the init process adopts orphaned processes (with all of their children processes).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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