Process Structure
Process in the kernel source code in the structure of the expression, long, here to enumerate a small section of the key code, you can find a doubly linked list, the specific can be found in the kernel directory called "sched.h" header file.
structtask_struct {structTask_struct *real_parent;/*Real Parent Process*/ structTask_struct *parent;/*recipient of SIGCHLD, WAIT4 () reports*/ /** children/sibling forms the list of my natural children*/ structList_head children;/*List of my children*/ structList_head sibling;/*linkage in my parent ' s children list*/ structTask_struct *group_leader;/*Threadgroup leader*/...};
The process is stored in a two-way circular list called the Task List (tasklist), where Linux allocates the TASK_STRUCT structure through the slab allocator to achieve object reuse and cache coloring (cache coloring).
The structure is mainly composed of four parts
1. Process Control BLOCK: Process flag
2. Process BLOCK: Can be shared with other programs
3. Process data BLOCK: process-specific space for storing various private data and stack space
4. Stand-alone space: Threads
Process status
#define task_running 0#define task_interruptible 1#define task_uninterruptible 2# Define __task_stopped 4#define __task_traced 8*** * * Define Exit_zombie #define exit_dead /** * * * Define task_dead #define task_wakekill #define task_waking 256
There are five different states of the process.
1.task_running (running)--------process is executing, or waiting to be executed in the queue. This is the only possible state of the process in the user space, or it can be applied to the process being executed in the kernel space. 2.task_interruptible (interruptible)--------process is sleeping (that is, he is blocked) waiting for certain conditions to be reached. Once these conditions are reached, the kernel will set the process state to run, and the process in this state will be woken up and put into operation as soon as the signal is received. 3.task_uninterruptibale (non-interruptible)--------In addition to being woken up for receiving a signal to run, the state is the same as the interruptible state. This state usually occurs when the process must wait without interference or when the event will occur very quickly. Because the task in this state does not respond to the signal, less is used than the interruptible state. 4.task_zombie (Zombie)-------The process is over, but the parent process has not yet called the Wait () system call. Once the parent process calls wait (), the process descriptor is freed. 5.task_stopped (stop)---------process stops executing and the process is not operational or operational.
Creation of processes
In a Linux system, all processes are descendants of the init process with PID 1. The kernel initiates the INIT process at the last stage of system startup. The process reads the initialization script of the system and executes other related programs, eventually completing the entire process of system startup.
A process is the basic execution unit (the thread is the smallest dispatch unit) in the system, and the fork function can be used to create a new process;
void )
The fork () function does not require arguments, but returns two times, with three cases of return values:
(1) For the parent process, the fork function returns the ID of the new child process.
(2) For child processes, the fork function returns 0.
(3) If there is an error, the fork function returns-1.
The fork function creates a new process and obtains a new process ID for the process from the kernel, assigns the process space to the new process, and copies the contents of the process space of the parent process to the process space of the child process, including the parent process's data segment + Stack segment and share the code snippet with the parent process.
After the fork function, the child process executes from the wait fork return, instead of starting from scratch.
Note: The child process completely replicates the contents of the parent process's address space, including the contents of the stack segment + data segment. However, the child process does not copy the code snippet, but it shares the code snippet with the parent process. The code snippet is read-only, there are no modification problems, so it can be shared. After a child process is created, the child process's address space is completely separate from the parent process, and the parent-child process is two separate processes.
The Linux environment provides a function similar to the fork function that can be used to create a child process that has a shared parent process address space.
pid_t vfork ();
The difference between vfork () and fork ():
(1) Vfork the resulting child process and the parent process fully share the address space, including the code snippet + data segment + Stack segment . The modification of a child process to a shared resource also affects the parent process.
(2) The Vfork function produces a child process that must run first than the parent process. That is, the parent process calls the Vfork function and waits for the child process to run before running.
Linux process management and scheduling (i)