Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 2: Process Management and Scheduling


{
return pid_task(find_pid_ns(nr, ns), type);
}

Some simpler auxiliary functions build on the most generalfind_task_by_pid_type_ns:

❑ find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)finds atask_structinstance
given a numerical PID and the namespace of the task.
❑ find_task_by_vpid(pid_t vnr)finds a task by its local numerical PID.
❑ find_task_by_pid(pid_t nr)finds a task by its global numerical PID.

find_task_by_pidis required at many points in the kernel sources because a large number of process-
specific operations (e.g., sending a signal usingkill) identify their target process by means of its PID.

Generating UniquePIDs


In addition to managing PIDs, the kernel is also responsible for providing a mechanism to generate
unique PIDs that have not yet been assigned. In this case, the differences between the various PID
types can be ignored because unique numbers need only be generated for PIDs in the classicalUnix
sense. All other identifiers can be derived from the PID, as we will see when discussingforkandclone
below. In the sections that follow, the term PID once again refers to the classicalUnixprocess identifier
(PIDTYPE_PID).

To keep track of which PIDs have been allocated and which are still free, the kernel uses a large bitmap
in which each PID is identified by a bit. The value of the PID is obtained from the position of the bit in
the bitmap.

Allocating a free PID is then restricted essentially to looking for the first bit in the bitmap whose value is
0; this bit is then set to 1. Conversely, freeing a PIDcan be implemented by ‘‘toggling‘‘ the corresponding
bit from 1 to 0. These operations are implemented using

kernel/pid.c
static int alloc_pidmap(struct pid_namespace *pid_ns)

to reserve a PID, and

kernel/pid.c
static fastcall void free_pidmap(struct pid_namespace *pid_ns, int pid)

to free a PID. How they are implemented does not concern us here, but naturally, they must work on a
per-namespace basis.

When a new process is created, it may be visible in multiple namespaces. For each of them a local PID
must be generated. This is handled inalloc_pid:

kernel/pid.c
struct pid *alloc_pid(struct pid_namespace *ns)
{
struct pid *pid;
enum pid_type type;
int i, nr;
Free download pdf