Chapter 2: Process Management and Scheduling
Since all task structures that share an identifier are kept on a list headed bytasks, a list element is
required instruct task_struct:
<sched.h>
struct task_struct {
...
/* PID/PID hash table linkage. */
struct pid_link pids[PIDTYPE_MAX];
...
};
The auxiliary data structurepid_linkpermits linking of task structures on the lists headed from
struct pid:
<pid.h>
struct pid_link
{
struct hlist_node node;
struct pid *pid;
};
pidpoints to apidinstance to which the task belongs, andnodeis used as list element.
A hash table is used to find thepidinstance that belongs to a numeric PID value in a given
namespace:
kernel/pid.c
static struct hlist_head *pid_hash;
hlist_headis a kernel standard data element used to create doubly linked hash lists (Appendix C
describes the structure of such lists and introduces several auxiliary functions for processing them).
pid_hashis used as an array ofhlist_heads. The number of elements is determined by the RAM con-
figuration of the machine and lies between 2^4 =16 and 2^12 =4, 096.pidhash_initcomputes the apt size
and allocates the required storage.
Suppose that a new instance ofstruct pidhas been allocated and set up for a given ID typetype.Itis
attached to a task structure as follows:
kernel/pid.c
int fastcall attach_pid(struct task_struct *task, enum pid_type type,
struct pid *pid)
{
struct pid_link *link;
link = &task->pids[type];
link->pid = pid;
hlist_add_head_rcu(&link->node, &pid->tasks[type]);
return 0;
}
A connection is made in both directions: The task structure can access thepidinstance via
task_struct->pids[type]->pid. Starting from thepidinstance, the task can be found by iterating over