1. Preface
This paper mainly based on the Linux Kernel 2.6.32 source code, the Linux process model analysis, can be summarized as follows:
1. Preface
2. Introduction of the process
3. How the operating system organizes processes
4. Conversion of Process State
5. Scheduling of processes
6. References
2. Process Introduction
2.1 Concept of the process
process is a computer program on a data set on a running activity, the system is the basic unit of resource allocation and scheduling, is the basis of the operating system structure. In the early process design-oriented computer architecture, the process is the basic execution entity of the program, and in the contemporary thread-oriented computer architecture, the process is the container of the thread. A program is a description of instructions, data, and its organization, and the process is the entity of the program.
2.2 View of the process
2.2.1 Windows viewing process
The Ctrl+alt+del key combination is available under Windows system to open Task Manager to view the process:
2.2.2 Linux under View process
Linux can be used to view processes in the terminal with the PS command:
3. How the operating system organizes processes
3.1 Process Control block (PCB)
The main active entity in a Linux system is the process.
Each process executes a separate program and has a separate control thread when the process is initialized. In other words, each process has a separate program counter that can be used to track the next command that will be executed.
All processes are placed in a data structure called the Process Control block (PCB), which can be understood as a collection of process properties that are created and managed by the operating system. Each process has a process control block in the kernel to maintain process-related information, and the process control block of the Linux kernel is the (TASK_STRUCT) structure. Linux uses the task_struct (PCB) structure to describe all information about a process, and the structure is defined when the Include/linux/sched.h 中。 process is created, the operating system creates a new PCB structure, which then resides in memory and can be accessed at any one time. , which is deleted at the end of the process. PCB is a part of the process entity, the operating system through the PCB table to manage and control the process, is the only sign of process existence.
3.2 Process identifier (PID)
The process identifier is defined under the TASK_STRUCT structure:
pid_t pid; // The ID of the kernel to identify the process pid_t tgid; // struct pid{atomic_t count; unsigned int level; /* Lists of the tasks that use the This PID Span style= "COLOR: #008000" >*/ struct Hlist_head Tasks[pidtype_max]; struct Rcu_head RCU; struct upid numbers[1 ";};
Each process has a unique identifier (PID), the kernel through this identifier to identify different processes, while the process identifier (PID) is the kernel provided to the user program interface, the user program through the PID to dictate the process. PID is a 32-bit unsigned integer that is sequentially numbered: the PID of the newly created process is usually the PID of the previous process plus 1. However, to maintain compatibility with traditional Linux systems on 16-bit hardware platforms, the maximum PID number allowed on Linux is 32767, and when the kernel creates a No. 32768 process in the system, it must restart using the idle PID number. In a 64-bit system, the PID can be extended to 4194303.
4 . Transformation of Process State
4.1 Three-state model
The entire life cycle of a process from creation to revocation can be carved with a set of States, and the life cycle of the process can be divided into three process states according to the three-state model:
1. Operating State (running): Possession processor is running
2. Ready: With operating conditions, wait for the system to allocate the processor to run
3. Wait state (blocked): No running conditions, waiting for an event to complete
4.2 Five-State model
In a real system, the state of the process and its transformation are more complex than those described in the previous section, such as the introduction of a specialized new state (new) and a terminating state (exit)
The status transition diagram looks like this:
5, the process of scheduling
5.1 The evolution of Linux Scheduler
The current kernel supports two scheduler classes (the Sched_setscheduler system calls the policy that modifies the process): CFS (FAIR), RT (real-time), 5 scheduling Strategies: Sched_noraml (the most common strategy), Sched_ BATCH (in addition to not being preempted outside of the regular task, allows the task to run longer, better use the cache, suitable for batch work), Sched_idle (it is even weaker than nice 19, to avoid priority reversal) and SCHED_RR (cyclic dispatch, Have time slices, put them at the end of the queue), Sched_fifo (no time slices, can run any length of time); the previous three strategies used the CFS scheduler class, followed by two using the RT Scheduler class.
Structure of the 5.2 CFS scheduler
The first is the dispatch entity Sched_entity, which represents a dispatch unit that can be equated to a process when the group is scheduled to close. Each task_struct has a sched_entity, and the vruntime and weights of the process are kept in the structure. So how are all the sched_entity organized together? Red and black trees. All sched_entity with Vruntime as key (actually in Vruntime-min_vruntime, is it to prevent overflow?) Anyway the result is the same) inserted into the red-black tree, while the leftmost node of the cache tree, that is, vruntime the smallest node, so that the vruntime minimal process can be quickly selected. Note Only the ready state of the CPU waiting on the tree, the sleep process and the running process are not on the tree. if (source see Resources):
6. References
1. Https://baike.baidu.com/item/%E8%BF%9B%E7%A8%8B/382503?fr=aladdin
2. Https://baike.baidu.com/item/%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95/3017645?fr=aladdin
3. https://www.cnblogs.com/puputongtong/p/5451210.html
4.52067518
First assignment: Analysis of the Linux process model