MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-theme-Font:
Minor-Fareast "> processes are the basis for operating systems. In this article, we will look at the Linux process from a rough perspective.
Minor-Fareast; MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast;
MSO-Hansi-theme-Font: minor-Fareast ">
MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-theme-Font:
Minor-Fareast "> each user-state process corresponds to a process descriptor in the task_struct structure in the kernel, the descriptor contains the Process status and execution information, virtual memory information, open file information, file system information, and signal-related information (resources used by the process are limited, for example, the number of opened files, the physical page used, and the file size are included in the task_struct-> signal-> rlim field in the descriptor. You can also use ulimit
-).
Minor-Fareast; MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast;
MSO-Hansi-theme-Font: minor-Fareast ">
MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-theme-Font:
Minor-Fareast "> each process is identified by an ID, which is called a process number. The kernel allocates a process number for the newly created process through bitmap management. The kernel uses a linked list to link all process descriptors for unified management (process information can be viewed through the ps command), and the header-type init_task descriptor of the process linked list, that is, the 0 process or the Swapper process descriptor. In order to obtain the process descriptor conveniently through the process number, the kernel chain all processes to different discrete lists according to their PID (pgid, Sid.
Minor-Fareast; MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast;
MSO-Hansi-theme-Font: minor-Fareast ">
MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-theme-Font:
Minor-Fareast "> A process can be created through a fork system call. A newly created process becomes a sub-process. When it is created, it copies all resources of its parent process, is a copy of the parent process. Execve system calls can execute new applications. Generally, fork is used together with execve system calls to create sub-processes through fork, execve is then called in the child process to execute the new application (replace the virtual address space of the child process with the virtual address space of the application ).
Minor-Fareast; MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast;
MSO-Hansi-theme-Font: minor-Fareast ">
MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-theme-Font:
Minor-Fareast "> the ancestor of all processes is process 0, idle process, or Swapper process. It is a kernel thread created from none in the Linux Initialization phase, this process uses a series of static data structures for initialization (init_mm, init_fs, etc ). Process 0 executes the cpu_idle function. In essence, the HLT command is executed repeatedly when the process is interrupted. The scheduler selects process 0 only when no other process is in a running state. After process 0 initializes all the data structures required by the kernel, it creates a kernel thread (INIT process) called process 1 and runs it by calling execve system to load/sbin/init. before the system is shut down, the INIT process remains alive because it creates and monitors activities of all processes executed outside the operating system.
Minor-Fareast; MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast;
MSO-Hansi-theme-Font: minor-Fareast ">
MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-theme-Font:
Minor-Fareast "> to facilitate scheduling, the kernel sets various states for the process, including running status, interrupted waiting status, non-interrupted waiting status, and pause status. Depending on different conditions, processes can be converted in different States. In addition, considering the different nature of the task, the process has different priorities. Each running state process descriptor is linked to the corresponding priority linked list at the same time (a total of 140 priority linked lists, the corresponding priority ranges from 0 to 139 ). For processes in the waiting state, they are divided into several categories. Each category represents a specific event and processes are organized through the waiting queue.
Minor-Fareast; MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast;
MSO-Hansi-theme-Font: minor-Fareast ">
MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-theme-Font:
Minor-Fareast "> the main task of the scheduler is to select the next process to run and perform context switching (the architecture-related operations are completed by switch_to ). The kernel scheduler is mainly completed by the schedule function. The schedule () function can be called in two ways:
; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-theme-Font: minor-Fareast;
MSO-bidi-font-family:; MSO-bidi-theme-Font: minor-Fareast "> 1.; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-theme-Font: minor-Fareast "> actively schedule and directly call the schedule () function (), such as process exit or sleep.
; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-theme-Font: minor-Fareast;
MSO-bidi-font-family:; MSO-bidi-theme-Font: minor-Fareast "> 2.; MSO-Fareast-theme-Font: minor-Fareast; MSO-Hansi-theme-Font: minor-Fareast "> mandatory scheduling. Set need_resched in task_struct of the current process. When this bit is returned from the kernel state to the user State, schedule () is called if it is found to have been set. Compared with active scheduling, mandatory scheduling has a certain scheduling delay; in the following three cases, need_resched may be set:
A. in the clock interrupt service program, it is found that the process has used up its own time slice and the CPU needs to be switched out;
B. when a sleep process is awakened, it is found that the process is more CPU-consuming than the current font-family:; MSO-ascii-theme-Font: minor-Fareast; MSO-Fareast-theme-Font: minor-Fareast;
MSO-Hansi-theme-Font: minor-Fareast; MSO-bidi-font-family: "Times New Roman MSO-bidi-theme-Font: minor-bidi ZH-CN> high process priority;
C. A process changes the scheduling policy and priority through system calls.
Minor-Fareast; MSO-Fareast-font-family:; MSO-Fareast-theme-Font: minor-Fareast;
MSO-Hansi-theme-Font: minor-Fareast ">