Linux Kernel development-Process Control and Linux kernel development process
This chapter mainly analyzes the definition, status, data structure, and other concepts of processes from the perspective of code.
Process Definition
A process is a running program and a dynamic executable entity. A program is a collection of code and data. A code is a static entity and can be used by multiple processes, for example, the same application can run on different computers to generate multiple processes.
Process four elements
The four elements of a process mainly refer to the differences between threads and processes in code:
L there is a program for execution
L process-specific kernel stack space
L kernel control block (with a task_struct data structure) and kernel control block can be scheduled by the kernel
L independent user space
How to process, user thread, and kernel thread:
Process status
1) # defineTASK_RUNNING 0
It is being executed by the CPU or is ready for execution at any time. The process is in this status when it is just created.
2) # defineTASK_INTERRUPTIBLE 1
A waiting process is awakened when the waiting condition is true. It can also be triggered by a signal interruption.
3) # defineTASK_UNINTERRUPTIBLE 2
A waiting process can be awakened when the resource is valid, but cannot be awakened by signals or interruptions of other processes.
4) # define _ TASK_STOPPED 4
Stop the process. When the SIGSTOP or SIGTSTP signal is received, the process returns to the TASK_RUNNING state after the SIGCONT signal is received.
5) # define _ TASK_TRACED 8
Processes in the debugging status
6) # defineEXIT_ZOMBIE 16
Indicates that the process has been terminated, but the parent process has not collected information about process death through waitpid.
7) # defineEXIT_DEAD 32
The final state of Process Termination. The parent process has collected information about the process's death and will be deleted by the system.
8) # defineTASK_DEAD 64
Process exit is in this State (EXIT_DEAD and EXIT_ZOMBIE are recorded by separate variables, which is a subdivision of TASK_DEAD ).
9) # defineTASK_WAKEKILL 128
Similar to TASK_UNINTERRUPTIBLE, the new status of Linux 2.6.25 kernel can be awakened by SIGKILL.
# Define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
# Define TASK_STOPPED (TASK_WAKEKILL | _ TASK_STOPPED)
# Define TASK_TRACED (TASK_WAKEKILL | _ TASK_TRACED)
# Define TASK_NORMAL (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
Process Data Structure
In Linux, processes and threads are described using the task_struct data structure. The following describes the usage of the most important data structure members.
Volatile long state; // Process status
Unsigned int flags; // process description flag
Int prio, static_prio, normal_prio; // process priority. The greater the value, the smaller the priority.
Unsigned int rt_priority; // process running priority
Unsigned int policy; // process scheduling policy
Int exit_state; // The status when the process exits.
Struct mm_struct * mm; // Process Memory Management Information
Pid_t pid; // process ID
Data Structure and kernel stack space
The Linux Kernel stack has only two page sizes (8 KB). In the 2.4 kernel, task_struct is placed at the bottom of the kernel stack.
However, in version 2.6, the data structure occupies storage space. Only one thread_info_structure data structure is placed at the bottom of the kernel stack. This structure describes the location of task_struct.
There is a current pointer in the Linux kernel, always pointing to the ongoing task_struct.
How does the Linux kernel classify processes?
Yes,
Three Scheduling Methods for Linux kernel:
1. SCHED_OTHER time-based scheduling policy,
2. SCHED_FIFO real-time scheduling policy, first served
3. SCHED_RR real-time scheduling policy, time slice Rotation
Real-time processes will receive priority calls. Real-time processes determine the scheduling weights based on the real-time priority. Time-based processes use nice and counter values to determine the weights. The smaller the nice value, the larger the counter value, the higher the probability of being scheduled, that is, the process that used the least cpu will receive priority scheduling.
SHCED_RR and SCHED_FIFO are different:
When the time slice of the process using the SHCED_RR policy is used up, the system will re-allocate the time slice and place it at the end of the ready queue. Putting it at the end of the queue ensures fair scheduling for all RR tasks with the same priority.
SCHED_FIFO runs continuously once it occupies the cpu. Run until a higher-priority task arrives or you give up.
If a real-time process with the same priority (the scheduling weights calculated based on the same priority are the same) is ready, the task with the same priority can be run only after the process voluntarily gives up in FIFO. RR allows each task to be executed for a period of time.
Similarities:
RR and FIFO are only used for real-time tasks.
The priority is greater than 0 (1-99) during creation ).
It is executed according to the preemptible priority scheduling algorithm.
Ready real-time tasks immediately seize non-real-time tasks.
See
What are the main tasks of process 0 in Linux kernel?
The following are some important points:
1. process 0 is the ancestor of all other processes, also known as idle process or swapper process.
2. process 0 is created by the kernel itself from scratch during system initialization.
3. Most data members of process 0 are statically defined, I .e., they are initialized by predefined macros such as INIT_TASK and INIT_MM.
The descriptor init_task of process 0 is defined in arch/arm/kernel/init_task.c and initialized by the INIT_TASK macro. Struct such as init_mm are defined in include/linux/init_task.h and are the initial values of init_task members. They are initialized by the corresponding initialization macros such as INIT_MM.
Process 1
Process 0 will eventually call kernel_thread to create a kernel thread to execute the init function, this newly created kernel thread is Process 1 (in this case, the resource attributes of kernel Thread 0, such as address space, are still shared ). The init function continues to initialize the remaining kernel, and calls the execve system call at the end of the function to mount the executable program/sbin/init in the user space, in this case, process 1 has its own property resources and becomes a common process (init process ). At this point, the kernel initialization and startup process are complete. Next we enter the initialization of the user space, and finally run the shell login interface. (Note: The Init process remains alive because it creates and monitors activities of all processes executed outside the operating system .)
------
This section references process 0 from Understanding The Linux Kernel-Third Edtion.
The ancestor of all processes, called process 0, the idle process, or, for historical reasons, the swapper process, is a kernel thread created from scratch during the initialization phase of Linux. this ancestor process uses the following statically allocated data structures (data structures for all other processes are dynamically allocated)