The first assignment: Analysis of the Linux process model

1 Introduction

This article is mainly based on the source code of Linux Kernel 2.6.32, and analyzes the process model of Linux, which can be roughly summarized as follows:

1 Introduction

2. Process introduction

3. How the operating system organizes processes

4. Transformation of process status

5. Process scheduling

6. References


 

2. Process introduction

2.1 The concept of process

  A process is a running activity of a program in a computer on a data set, the basic unit of resource allocation and scheduling in the system, and the basis of the operating system structure. In the early computer structure of process-oriented design, the process is the basic execution entity of the program; in the contemporary computer structure of thread-oriented design, the process is the container of the thread. A program is a description of instructions, data, and their organization, and a process is the entity of a program.

2.2 View of the process

  2.2.1 Windows view process

    In Windows, you can use the CTRL+ALT+DEL key combination to open the Task Manager to view the process:

    

  2.2.2 View process under Linux

    Under Linux, you can use the ps command to view the process in the terminal:

    


 

3. How the operating system organizes processes

3.1 Process Control Block (PCB)

  The main active entity in a Linux system is a process.

  Each process executes a separate program and has a separate thread of control when the process is initialized. In other words, each process has an independent program counter, which can be used to track the next instruction to be executed.

  All processes are placed in a data structure called a process control block (PCB), which can be understood as a collection of process attributes, which is created and managed by the operating system. Each process has a process control block in the kernel to maintain process-related information. The process control block of the Linux kernel is a (task_struct) structure. Linux describes all the information of a process through the task_struct (PCB) structure. When the structure is defined in the include/linux/sched.h 中。process, the operating system creates a new PCB structure, which then resides in memory at any time. Can be accessed, deleted at the end of the process. The PCB is a part of the process entity. The operating system manages and controls the process through the PCB table, which is the only sign of the existence of the process.

3.2 Process Identifier (PID)

  The process identifier is defined under the task_struct structure:

pid_t pid;       // The id used to identify the process in the kernel 
pid_t tgid;      // Used to implement the thread mechanism


struct pid
{
    atomic_t count;
    unsigned int level;

    /* lists of tasks that use this pid */
    struct hlist_head tasks[PIDTYPE_MAX];
    struct rcu_head rcu;
    struct upid numbers[1];
};

  Each process has a unique identifier (PID), and the kernel uses this identifier to identify different processes. At the same time, the process identifier (PID) is also an interface provided by the kernel to the user program, and the user program issues commands to the process through the PID. PIDs are 32-bit unsigned integers that are numbered sequentially: the PID of a newly created process is usually the PID of the previous process plus 1. However, in order 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 the 32768th process in the system, it must restart using the idle PID number. On a 64-bit system, the PID can be extended to 4194303.


 

4. Transformation of process status

4.1 Three-state model

The entire life cycle of a process from creation to revocation and death can be characterized by a set of states. According to the three-state model, the life cycle of a process can be divided into the following three process states: 
1. Running:
2. Ready state (ready): has the running conditions, waiting for the system to allocate the processor to run  3. 
Waiting state (blocked): does not have the running conditions, is waiting for the completion of an event

 

4.2 Five-state model

In an actual system, the state of the process and its transitions are more complicated than those described in the previous section, such as the introduction of special new states (new) and termination states (exit). 

The state transition diagram is shown below: 

 


 

5. Process scheduling

  5.1 Evolution of the Linux scheduler

  

  The current kernel supports two scheduler classes (the sched_setscheduler system call can modify the process strategy): CFS (fair), RT (real time); 5 scheduling strategies: SCHED_NORAML (the most common strategy), SCHED_BATCH (except that it cannot be preempted and Like regular tasks, allows tasks to run longer, better use of cache, suitable for batched jobs), SCHED_IDLE (even weaker than nice 19, used to avoid priority inversion) and SCHED_RR (round-robin Scheduling, has a time slice, and puts it at the end of the queue after the end), SCHED_FIFO (no time slice, can run for any length of time); the first three strategies use the cfs scheduler class, and the latter two use the rt scheduler class.

  5.2 Structure of CFS Scheduler

   The first is the scheduling entity sched_entity, which represents a scheduling unit, which can be equated to a process when group scheduling is turned off. There is a sched_entity in each task_struct, and the vruntime and weight of the process are stored in this structure. So how are all the sched_entities organized together? Red black tree. All sched_entities are inserted into the red-black tree with vruntime as the key (actually in vruntime-min_vruntime unit, is it to prevent overflow? Anyway, the result is the same) into the red-black tree, and at the same time cache the leftmost node of the tree, that is, the smallest vruntime node, so that the process with the smallest vruntime can be quickly selected. Note that only the ready state processes waiting for the CPU are in this tree, neither sleeping processes nor running processes are in the tree. As shown below (source see references):


 

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. https://blog.csdn.net/gatieme/article/details/52067518

 

 

 

 

  

    

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325117784&siteId=291194637