Linux – Internal organization (with regard to family relations) of processes in Linux

kernellinuxprocess

As I understand it, process descriptors are stored in a doubly linked list data structure. But fork can be used to 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?

Best Answer

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.

Related Question