In this chapter we will discuss about the concept of process and concept related to process like thread and how Linux kernel manage process.
Process and Thread:-
Process is a piece of code which is in currently execution. A process also include some resources like signal ,open files and memory sections (code , data , stack , heap ). Kernel has to maintain all these info in proper way and efficient way.
Threads are the lightweight process or we can say a small entity of process which has unique stack, program counter etc. Linux does not differentiate between process and thread. Linux kernel understand a thread as a process and implements process and thread in same way.
Process Descriptor:-
In Linux kernel a process is represented by Process descriptor which is a structure of type task_struct . Kernel stores all the process by maintaining a doubly linked list in which each node is type of task_struct. In other words we can say kernel maintained a doubly linked list of process descriptors.
The task_struct is a large data structure around 1.7 Kb on a 32 bit machine. This structure maintain all the information about a process. This structure contains info like signals, files ,stack ,state and much more as we can see below picture.
Process descriptor allocation:-
The tast_struct is allocated by slab allocator. Prior to kernel 2.6 the task_struct is stored at the end of process kernel stack. Now the new structure thread_info is dynamically allocated by slab allocator which also leaves at the end of kernel stack of a process.
struct thread_info {
struct task_struct *task;
struct exec_domain *exec_domain;
__u32 flags;
__u32 status;
__u32 cpu;
int preempt_count;
mm_segment_t addr_limit;
struct restart_block restart_block;
void *sysenter_return;
int uaccess_err;
};
Process descriptor and kernel Stack
the system identify process by a unique identification value or PID which is of type pid_t which is an int. The kernel stores each value as pid in process descriptor. In Kernel code that deals with the process use the task_struct structure to manipulate the process and we can refer to the currently executing process by current macro.
Process State:-
the state field of process descriptor identifies the current situation of process. In linux a process can be in one of the following state.
1) TASK_RUNNING :-
this state specify that process is either running or in ready state and waiting in runqueue .
2) TASK_INTRUPTTIBLE:-
this condition states that the task is in sleeping state and waiting for some event to occur. When this condition occurs task comes in TASK_RUNNING state.
3) TASK_UNINTRRUPTIBLE:-
this state is similar as previous state but in this state process does not wait for any event to occur.
4)_TASK_TRACED:-
The process is getting traced by another process , such as a debugger.
5)_TASK_STOPPED:-
this state states that task is not running and it has stopped the execution.
Process context:-
Normally process runs in user space but when a program calls a syscall or triggers an exception then it enter into the kernel-space at that time kernel is said to be executed on behalf of process and in other word kernel is in “process context”.
The Process Family Tree:-
A tree hierarchy is maintained by Linux between each process. Every process in system has one parent. and a process has zero or more child. Process descriptor contains a member parent which points to the parent of that process and a list of children is maintained by children. So we can traverse the tree and can search the required process.
Reference:- Linux Kernel Development By Robert Love