Sei sulla pagina 1di 13

Linux Kernel Process Management

The process is one of the fundamental abstractions in Unix operating systems1. A process is a 
program (object code stored on some media) in execution. Processes are, however, more than just 
the executing program code (often called the text section in Unix). They also include a set of 
resources such as open files and pending signals, internal kernel data, processor state, an address 
space, one or more threads of execution, and a data section containing global variables. Processes, 
in effect, are the living result of running program code.
Threads of execution, often shortened to threads, are the objects of activity within the process. Each 
thread includes a unique program counter, process stack, and set of processor registers. The kernel 
schedules individual threads, not processes. In traditional Unix systems, each process consists of 
one thread. In modern systems, however, multithreaded programs—those that consist of more than 
one thread—are common. As you will see later, Linux has a unique implementation of threads: It 
does not differentiate between threads and processes. To Linux, a thread is just a special kind of 
process.
On modern operating systems, processes provide two virtualizations: a virtualized processor and 
virtual memory. The virtual processor gives the process the illusion that it alone monopolizes the 
system, despite possibly sharing the processor among dozens of other processes. Chapter 4, 
"Process Scheduling," discusses this virtualization. Virtual memory lets the process allocate and 
manage memory as if it alone owned all the memory in the system. Virtual memory is covered in 
Chapter 11, "Memory Management." Interestingly, note that threads share the virtual memory 
abstraction while each receives its own virtualized processor.
A program itself is not a process; a process is an active program and related resources. Indeed, two 
or more processes can exist that are executing the same program. In fact, two or more processes can 
exist that share various resources, such as open files or an address space.
A process begins its life when, not surprisingly, it is created. In Linux, this occurs by means of the 
fork() system call, which creates a new process by duplicating an existing one. The process that 
calls fork() is the parent, whereas the new process is the child. The parent resumes execution and 
the child starts execution at the same place, where the call returns. The fork() system call returns 
from the kernel twice: once in the parent process and again in the newborn child.
Often, immediately after a fork it is desirable to execute a new, different, program. The exec*()
family of function calls is used to create a new address space and load a new program into it. In 
modern Linux kernels, fork() is actually implemented via the clone() system call, which is 
discussed in a following section.
Finally, a program exits via the exit() system call. This function terminates the process and frees 
all its resources. A parent process can inquire about the status of a terminated child via the 
wait4()2 system call, which enables a process to wait for the termination of a specific process. 
When a process exits, it is placed into a special zombie state that is used to represent terminated 
processes until the parent calls wait() or waitpid().
Another name for a process is a task. The Linux kernel internally refers to processes as tasks. In this 
book, I will use the terms interchangeably, although when I say task I am generally referring to a 
process from the kernel's point of view.
Process Descriptor and the Task Structure
The kernel stores the list of processes in a circular doubly linked list called the task list3. Each 
element in the task list is a process descriptor of the type struct task_struct, which is 
defined in <linux/sched.h>. The process descriptor contains all the information about a 
specific process.
The task_struct is a relatively large data structure, at around 1.7 kilobytes on a 32­bit machine. 
This size, however, is quite small considering that the structure contains all the information that the 
kernel has and needs about a process. The process descriptor contains the data that describes the 
executing program—open files, the process's address space, pending signals, the process's state, and 
much more (see Figure 3.1).
 

Figure 3.1 The process descriptor and task list.

Allocating the Process Descriptor
The task_struct structure is allocated via the slab allocator to provide object reuse and cache 
coloring (see Chapter 11, "Memory Management"). Prior to the 2.6 kernel series, struct
task_struct was stored at the end of the kernel stack of each process. This allowed 
architectures with few registers, such as x86, to calculate the location of the process descriptor via 
the stack pointer without using an extra register to store the location. With the process descriptor 
now dynamically created via the slab allocator, a new structure, struct thread_info, was 
created that again lives at the bottom of the stack (for stacks that grow down) and at the top of the 
stack (for stacks that grow up)4. See Figure 3.2.
The new structure also makes it rather easy to calculate offsets of its values for use in assembly 
code.
The thread_info structure is defined on x86 in <asm/thread_info.h> as
struct thread_info {
struct task_struct *task;
struct exec_domain *exec_domain;
unsigned long flags;
unsigned long status;
__u32 cpu;
__s32 preempt_count;
mm_segment_t addr_limit;
struct restart_block restart_block;
unsigned long previous_esp;
__u8 supervisor_stack[0];
};

Each task's thread_info structure is allocated at the end of its stack. The task element of the 
structure is a pointer to the task's actual task_struct.

 
Figure 3.2 The process descriptor and kernel stack.

Storing the Process Descriptor
The system identifies processes by a unique process identification value or PID. The PID is a 
numerical value that is represented by the opaque type5 pid_t, which is typically an int. Because 
of backward compatibility with earlier Unix and Linux versions, however, the default maximum 
value is only 32,768 (that of a short int), although the value can optionally be increased to the 
full range afforded the type. The kernel stores this value as pid inside each process descriptor.
This maximum value is important because it is essentially the maximum number of processes that 
may exist concurrently on the system. Although 32,768 might be sufficient for a desktop system, 
large servers may require many more processes. The lower the value, the sooner the values will 
wrap around, destroying the useful notion that higher values indicate later run processes than lower 
values. If the system is willing to break compatibility with old applications, the administrator may 
increase the maximum value via /proc/sys/kernel/pid_max.
Inside the kernel, tasks are typically referenced directly by a pointer to their task_struct
structure. In fact, most kernel code that deals with processes works directly with struct
task_struct. Consequently, it is very useful to be able to quickly look up the process descriptor 
of the currently executing task, which is done via the current macro. This macro must be 
separately implemented by each architecture. Some architectures save a pointer to the 
task_struct structure of the currently running process in a register, allowing for efficient 
access. Other architectures, such as x86 (which has few registers to waste), make use of the fact that 
struct thread_info is stored on the kernel stack to calculate the location of thread_info
and subsequently the task_struct.
On x86, current is calculated by masking out the 13 least significant bits of the stack pointer to 
obtain the thread_info structure. This is done by the current_thread_info() function. 
The assembly is shown here:
movl $-8192, %eax
andl %esp, %eax

This assumes that the stack size is 8KB. When 4KB stacks are enabled, 4096 is used in lieu of 8192.
Finally, current dereferences the task member of thread_info to return the 
task_struct:
current_thread_info()->task;

Contrast this approach with that taken by PowerPC (IBM's modern RISC­based microprocessor), 
which stores the current task_struct in a register. Thus, current on PPC merely returns the 
value stored in the register r2. PPC can take this approach because, unlike x86, it has plenty of 
registers. Because accessing the process descriptor is a common and important job, the PPC kernel 
developers deem using a register worthy for the task. 

Process State
The state field of the process descriptor describes the current condition of the process (see Figure 
3.3). Each process on the system is in exactly one of five different states. This value is represented 
by one of five flags:
• TASK_RUNNING—The process is runnable; it is either currently running or on a runqueue 
waiting to run (runqueues are discussed in Chapter 4, "Scheduling"). This is the only 
possible state for a process executing in user­space; it can also apply to a process in kernel­
space that is actively running.
• TASK_INTERRUPTIBLE—The process is sleeping (that is, it is blocked), waiting for 
some condition to exist. When this condition exists, the kernel sets the process's state to 
TASK_RUNNING. The process also awakes prematurely and becomes runnable if it receives 
a signal.
• TASK_UNINTERRUPTIBLE—This state is identical to TASK_INTERRUPTIBLE
except that it does not wake up and become runnable if it receives a signal. This is used in 
situations where the process must wait without interruption or when the event is expected to 
occur quite quickly. Because the task does not respond to signals in this state, 
TASK_UNINTERRUPTIBLE is less often used than TASK_INTERRUPTIBLE6.
• TASK_ZOMBIE—The task has terminated, but its parent has not yet issued a wait4()
system call. The task's process descriptor must remain in case the parent wants to access it. If 
the parent calls wait4(), the process descriptor is deallocated.
• TASK_STOPPED—Process execution has stopped; the task is not running nor is it eligible 
to run. This occurs if the task receives the SIGSTOP, SIGTSTP, SIGTTIN, or SIGTTOU
signal or if it receives any signal while it is being debugged.
 

Figure 3.3 Flow chart of process states.

Manipulating the Current Process State
Kernel code often needs to change a process's state. The preferred mechanism is using 
set_task_state(task, state); /* set task 'task' to state 'state' */

This function sets the given task to the given state. If applicable, it also provides a memory barrier 
to force ordering on other processors (this is only needed on SMP systems). Otherwise, it is 
equivalent to
task->state = state;

The method set_current_state(state) is synonymous to 
set_task_state(current, state).

Process Context
One of the most important parts of a process is the executing program code. This code is read in 
from an executable file and executed within the program's address space. Normal program execution 
occurs in user­space. When a program executes a system call (see Chapter 5, "System Calls") or 
triggers an exception, it enters kernel­space. At this point, the kernel is said to be "executing on 
behalf of the process" and is in process context. When in process context, the current macro is 
valid7. Upon exiting the kernel, the process resumes execution in user­space, unless a higher­priority 
process has become runnable in the interim, in which case the scheduler is invoked to select the 
higher priority process.
System calls and exception handlers are well­defined interfaces into the kernel. A process can begin 
executing in kernel­space only through one of these interfaces—all access to the kernel is through 
these interfaces.

The Process Family Tree
A distinct hierarchy exists between processes in Unix systems, and Linux is no exception. All 
processes are descendents of the init process, whose PID is one. The kernel starts init in the 
last step of the boot process. The init process, in turn, reads the system initscripts and executes 
more programs, eventually completing the boot process.
Every process on the system has exactly one parent. Likewise, every process has zero or more 
children. Processes that are all direct children of the same parent are called siblings. The 
relationship between processes is stored in the process descriptor. Each task_struct has a 
pointer to the parent's task_struct, named parent, and a list of children, named children. 
Consequently, given the current process, it is possible to obtain the process descriptor of its parent 
with the following code:
struct task_struct *my_parent = current->parent;

Similarly, it is possible to iterate over a process's children with
struct task_struct *task;
struct list_head *list;

list_for_each(list, &current->children) {
task = list_entry(list, struct task_struct, sibling);
/* task now points to one of current's children */
}

The init task's process descriptor is statically allocated as init_task. A good example of the 
relationship between all processes is the fact that this code will always succeed:
struct task_struct *task;
for (task = current; task != &init_task; task = task->parent)
;
/* task now points to init */

In fact, you can follow the process hierarchy from any one process in the system to any other. 
Oftentimes, however, it is desirable simply to iterate over all processes in the system. This is easy 
because the task list is a circular doubly linked list. To obtain the next task in the list, given any 
valid task, use:
list_entry(task->tasks.next, struct task_struct, tasks)

Obtaining the previous works the same way:
list_entry(task->tasks.prev, struct task_struct, tasks)

These two routines are provided by the macros next_task(task) and prev_task(task), 
respectively. Finally, the macro for_each_process(task) is provided, which iterates over the 
entire task list. On each iteration, task points to the next task in the list:
struct task_struct *task;

for_each_process(task) {
/* this pointlessly prints the name and PID of each task */
printk("%s[%d]\n", task->comm, task->pid);
}

Note: It can be expensive to iterate over every task in a system with many processes; code should 
have good reason (and no alternative) before doing so.

Experiments with the Linux Kernel: Process 
Segments
1. Introduction 
Traditionally, a Unix process is divided into segments. The standard segments are code segment, 
data segment, BSS (block started by symbol), and stack segment.
The code segment contains the binary code of the program which is running as the process (a 
"process" is a program in execution). The data segment contains the initialized global variables and 
data structures. The BSS segment contains the uninitialized global data structures and finally, the 
stack segment contains the local variables, return addresses, etc. for the particular process.
Under Linux, a process can execute in two modes ­ user mode and kernel mode. A process usually 
executes in user mode, but can switch to kernel mode by making system calls. When a process 
makes a system call, the kernel takes control and does the requested service on behalf of the 
process. The process is said to be running in kernel mode during this time. When a process is 
running in user mode, it is said to be "in userland" and when it is running in kernel mode it is said to 
be "in kernel space". We will first have a look at how the process segments are dealt with in 
userland and then take a look at the bookkeeping on process segments done in kernel space.
2. Userland's view of the segments 
The code segment consists of the code ­ the actual executable program. The code of all the 
functions we write in the program resides in this segment. The addresses of the functions will give 
us an idea where the code segment is. If we have a function foo() and let x be the address of foo
(x = &foo;). we know that x will point within the code segment.
The Data segment consists of the initialized global variables of a program. The Operating system 
needs to know what values are used to initialize the global variables. The initialized variables are 
kept in the data segment. To get the address of the data segment we declare a global variable and 
then print out its address. This address must be inside the data segment.
The BSS consists of the uninitialized global variables of a process. To get an address which occurs 
inside the BSS, we declare an uninitialized global variable, then print its address.
The automatic variables (or local variables) will be allocated on the stack, so printing out the 
addresses of local variables will provide us with the addresses within the stack segment.

3. A C program 
Let's have a look at the following C program:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5
6 int our_init_data = 30;
7 int our_noinit_data;
8
9 void our_prints(void)
10 {
11 int our_local_data = 1;
12 printf("\nPid of the process is = %d", getpid());
13 printf("\nAddresses which fall into:");
14 printf("\n 1) Data segment = %p",
15 &our_init_data);
16 printf("\n 2) BSS segment = %p",
17 &our_noinit_data);
18 printf("\n 3) Code segment = %p",
19 &our_prints);
20 printf("\n 4) Stack segment = %p\n",
21 &our_local_data);
22
23 while(1);
24 }
25
26 int main()
27 {
28 our_prints();
29 return 0;
30 }

We can see that lines 6 and 7 declare two global variables. One is initialized and one is uninitialized. 
Per the previous discussion, the initialized variable will fall into the data segment and the 
uninitialized variable will fall into the BSS segment. Lines 14­17 print the addresses of the 
variables. 
We also know that the address of the function our_prints will fall into the code segment, so that 
if we print the address of this function, we will get a value which falls into the code segment. This is 
done in lines 18­19.
Finally we print the address of a local variable. This automatic variable's address will be within the 
stack segment.

4. Execution of a userland program 
When we execute a userland program, similar to the one given above, what happens is that the shell 
will fork() and exec() the new program. The exec() code inside the kernel will figure out 
what format the binary is in (ELF, a.out, etc.) and will call the corresponding handler for that 
format. For example when an ELF format file is loaded, the function load_elf_binary() from 
fs/binfmt_elf.c takes care of initializing the kernel data structures for the particular process. 
Details of this portion of loading will not be dealt with here, as that in itself is a topic for another 
article :­) The point here is that the code which loads the executable into the kernel fills in the 
kernel data structures.

5. Memory­related data structures in the kernel 
In the Linux kernel, every process has an associated struct task_struct. The definition of 
this struct is in the header file include/linux/sched.h. The following snippet is from the 
2.6.10 Linux kernel source code (only the needed fields and a few nearby fields are shown):
struct task_struct {
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
struct thread_info *thread_info;
atomic_t usage;
...
...
...
struct mm_struct *mm, *active_mm;
...
...
...
pid_t pid;
...
...
...
char comm[16];
...
...
};

Three members of the data structure are relevant to us: 
1. pid contains the Process ID of the process. 
2. comm holds the name of the process. 
3. The mm_struct within the task_struct is the key to all memory management 
activities related to the process. 
The mm_struct is defined in include/linux/sched.h as: 
struct mm_struct {
struct vm_area_struct * mmap; /* list of VMAs */
struct rb_root mm_rb;
struct vm_area_struct * mmap_cache; /* last find_vma result */
...
...
...
unsigned long start_code, end_code, start_data, end_data;
unsigned long start_brk, brk, start_stack;
...
...
...
};

Here the first member of importance is the mmap. The mmap contains the pointer to the list of 
VMAs (Virtual Memory Areas) related to this process. Full usage of the process address space 
occurs very rarely. The sparse regions used are denoted by VMAs. So each VMA will contain 
information about a single region. The VMAs are stored in struct vm_area_struct defined 
in linux/mm.h: 
struct vm_area_struct {
struct mm_struct * vm_mm; /* The address space we belong to. */
unsigned long vm_start; /* Our start address within vm_mm. */
unsigned long vm_end; /* The first byte after our end address
within vm_mm. */
....
....
....
/* linked list of VM areas per task, sorted by address */
struct vm_area_struct *vm_next;
....
....
}

6. Kernel's view of the segments 
The kernel keeps track of the segments which have been allocated to a particular process using the 
above structures. For each segment, the kernel allocates a VMA. It keeps track of these segments in 
the mm_struct structures.
The kernel tracks the data segment using two variables: start_data and end_data. The code 
segment boundaries are in the start_code and end_code variables. The stack segment is 
covered by the single variable start_stack. There is no special variable to keep track of the 
BSS segment — the VMA corresponding to the BSS accounts for it.

7. A kernel module 
Let's have a look at the code for a kernel module:
1 #include <linux/init.h>
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/sched.h>
5 #include <linux/mm.h>
6
7 static int pid_mem = 1;
8
9 static void print_mem(struct task_struct *task)
10 {
11 struct mm_struct *mm;
12 struct vm_area_struct *vma;
13 int count = 0;
14 mm = task->mm;
15 printk("\nThis mm_struct has %d vmas.\n", mm->map_count);
16 for (vma = mm->mmap ; vma ; vma = vma->vm_next) {
17 printk ("\nVma number %d: \n", ++count);
18 printk(" Starts at 0x%lx, Ends at 0x%lx\n",
19 vma->vm_start, vma->vm_end);
20 }
21 printk("\nCode Segment start = 0x%lx, end = 0x%lx \n"
22 "Data Segment start = 0x%lx, end = 0x%lx\n"
23 "Stack Segment start = 0x%lx\n",
24 mm->start_code, mm->end_code,
25 mm->start_data, mm->end_data,
26 mm->start_stack);
27 }
28
29 static int mm_exp_load(void){
30 struct task_struct *task;
31 printk("\nGot the process id to look up as %d.\n", pid_mem);
32 for_each_process(task) {
33 if ( task->pid == pid_mem) {
34 printk("%s[%d]\n", task->comm, task->pid);
35 print_mem(task);
36 }
37 }
38 return 0;
39 }
40
41 static void mm_exp_unload(void)
42 {
43 printk("\nPrint segment information module exiting.\n");
44 }
45
46 module_init(mm_exp_load);
47 module_exit(mm_exp_unload);
48 module_param(pid_mem, int, 0);
49
50 MODULE_AUTHOR ("Krishnakumar. R, rkrishnakumar@gmail.com");
51 MODULE_DESCRIPTION ("Print segment information");
52 MODULE_LICENSE("GPL");

The module accepts the pid of the process, which it should dissect, as its parameter (line 48). The 
module will go through the list of processes in the kernel (32­37), and when it finds the required 
pid, it will call the function 'print_mem' function which will print the details from the memory 
management related data structures of the kernel.

8. Let us get into execution mode 
I ran the C program given in the earlier section and, while it was still running, loaded the kernel 
module with the pid of the process. Please note that the program was compiledstatically (­static) 
rather than dynamically, to avoid the unnecessary complication of shared libraries. Here is what I 
got:
# ./print_segments &
Pid of the process is = 3283
Addresses which fall into:
1) Data segment = 0x80a000c
2) BSS segment = 0x80a1a10
3) Code segment = 0x80481f4
4) Stack segment = 0xbffff8e4

# /sbin/insmod print_kern_ds.ko pid_mem=3283


Got the process id to look up as 3283.
print_segments[3283]

This mm_struct has 5 vmas.

Vma number 1:
Starts at 0x8048000, Ends at 0x80a0000

Vma number 2:
Starts at 0x80a0000, Ends at 0x80a1000

Vma number 3:
Starts at 0x80a1000, Ends at 0x80c3000

Vma number 4:
Starts at 0xb7fff000, Ends at 0xb8000000

Vma number 5:
Starts at 0xbffff000, Ends at 0xc0000000

Code Segment start = 0x8048000, end = 0x809fc38


Data Segment start = 0x80a0000, end = 0x80a0ec4
Stack Segment start = 0xbffffb30

Let's analyze the output. According to the userland program the address 0x80a000c should fall into 
the data segment. This can be verified by looking into the information we got from the kernel 
module, on printing the Data segment starting address and VMA number 2. For the code segment, it 
is starting at 0x8048000 as per the kernel data structures. Also according to the userland program 
the address 0x80481f4 should fall into the code segment. Hence userland and kernel tallies. 
Now, lets look at the Stack segment: the userland program says that the address 0xbffff8e4 should 
fall into it and kernel data structures states that stack will start from 0xbffffb30. In a 386­based 
architecture the stack grows downwards. The BSS is not stored in any particular variable of the 
kernel, but there is a VMA allocated for the corresponding location ­ from the userland program, the 
address 0x80a1a10 should come inside the BSS, and a look at VMA 3 makes it clear that this is the 
corresponding VMA for the BSS.

9. Gathering Information from /proc 
We have been using custom programs to explore the contents of the data structures inside the kernel, 
but the kernel provides a standard interface for us to access such information. The memory maps of 
a particular process can be obtained by doing a 'cat /proc/<pid>/maps' where <pid> should 
be the pid of the process of which we need to get the details about. When I ran it, the program used 
pid 3283; here is the memory map, trimmed to fit:
# cat /proc/3283/maps | cut -f1 -d' '
08048000-080a0000
080a0000-080a1000
080a1000-080c3000
b7fff000-b8000000
bffff000-c0000000
ffffe000-fffff000

A close look at the output shows that the first region corresponds to the code segment, the second 
region matches the data segment, the third is the BSS segment and the 5th region corresponds to the 
stack segment. 
10. Conclusion 
We have looked at the userland perspective of how the segments are treated for a program. Then we 
examined the data structures in the kernel which keep track of the segments. We verified that our 
assumptions are correct using userland and kernel programs. Finally we used the standard kernel 
interface to obtain information regarding the memory regions of a specific process.

Potrebbero piacerti anche