Operating System Notes--Processes and Threads

1--Process

1-1--The definition of the process

        A process represents a dynamic execution process of a program with certain independent functions on a data set;

1-2--The composition of the process

        A process consists of the following parts: ① the code of the program; ② the data processed by the program; ③ the value in the program counter, which indicates the next instruction to be executed; ④ the current value of a common set of registers, heap and stack; ⑤ a Group system resources; in short, process all state information of a running program;

1-3--The relationship between process and program

A process has the following relationship to a program:        

        ① The program is the basis for generating the process;

        ② Each run of the program constitutes a different process;

        ③ The process is the embodiment of the program function;

        ④ Through multiple executions, a program can correspond to multiple processes; through the call relationship, a process can include multiple programs;

The difference between a process and a program:

        ① The process is dynamic and the program is static;

        ② A program is a collection of ordered codes, a process is the execution of a program, and a process has a core state and a user state;

        ③ The process is temporary, and the program is permanent; the process is a process of state change, and the program can be saved for a long time;

        ④ The composition of process and program is different. The composition of process includes program, data and process control block (Process Control Block, that is, PCB is used to describe the data structure of process. The operating system maintains a PCB for each process, which is used to save and Various state information about the process);

1-4--The characteristics of the process:

A process has the following characteristics:

        ①Dynamics: Processes can be created and ended dynamically;

        ② Concurrency: Processes can be scheduled independently and occupy processors to run; (Concurrency: Multiple threads execute simultaneously within a period of time; Parallel: Multiple threads execute simultaneously at a time;)

        ③ Independence: The work of different processes does not affect each other;

        ④ Constraints: Processes will have mutual constraints due to access to shared data and resources, or synchronization between processes (involving mutual exclusion, locks, etc.); 

1-5--Process control structure

        Process control block: Indicates the combination of information used by the operating system to manage and control the operation of the process; the operating system describes the basic situation of the process and the process of running changes through the process control block (PCB), and the PCB is the only sign of the existence of the process;

        When using the process control block, creating a process will generate a PCB for the process, terminating the process will recycle its PCB, and the organization and management of the process is realized through the organization and management of the PCB;

PCB contains the following three categories of information:

        ① Process identification information: the identification of the process, the identification of the parent process of the process, the user identification, etc.;

        ② Processor state information storage area: save the running site information of the process;

        ③ Process control information: such as scheduling and status information, inter-process communication information, storage management information, resources used by the process, and related data structure connection information;

How the PCB is organized:

        ① Linked list: PCBs of processes in the same state form a linked list, and different states (such as ready, blocked, etc.) correspond to different linked lists;

        ② Index table: Processes in the same state are classified into one index table, and different states (such as ready, blocked, etc.) correspond to different index tables;

1-6--The life cycle of the process

Process life cycle management includes the following parts:

        ① Process creation: When the system is initialized, the user requests to create a new process, and the running process executes the system call to create the process;

        ② Process running: The kernel selects a ready process, lets it occupy the processor and run it;

        ③ Process waiting: In some cases, the process will enter the waiting (blocking) state. For example, when requesting and waiting for a system service, it cannot be completed immediately, and the process will enter the waiting state; when an operation is started, it cannot be completed immediately, and the process will enter Waiting state; when the data required by the process has not arrived, the process will enter the waiting state; the process can only block itself;

        ④ Process wake-up: The process will wake up from the waiting state and enter the wake-up state in the following situations, for example, when the resources required by the blocked process are satisfied; when the event waiting for the blocked process arrives; the PCB of the process is inserted into the ready queue; The process can only be woken up by other processes or the operating system;

        ⑤ Process end: The process will end in the following four situations, such as voluntary normal exit, voluntary error exit, forced fatal error, and forced to be killed by other processes;

1-7--Process state change model

A process will be in and only in the following three basic states before its life ends:

① Running: A process is running on the processor;

② Ready state (Ready): A process has obtained all resources except the processor, and can run when it gets the processor;

③ Waiting/Blocked state (Blocked): A process is waiting for an event and is suspended;

1-8--Process hangs

        When the process is in the suspended state, the process does not occupy memory space , and the process in the suspended state will be mapped to the disk (external memory);

        There are two kinds of suspended states: blocking suspended state, the process waits for an event to occur in the external memory;

                ​​​​​​​        ​​​​​​​        ​​​​​​​       就绪挂起状态,进程在外存中,当进入内存后即可运行;

        ​​​​​​​​Suspending is essentially transferring a process from memory to external memory;

Common pending state transitions are as follows:

        ① Blocking → Blocking Suspend: When no process is in the ready state or the ready process requires more memory resources, this transition occurs to submit a new process or run the ready process;

        ② Ready → ready to suspend: When there are high priority (the system thinks it will be ready soon) blocked process and low priority ready process, the system will suspend the low priority ready process;

        ③ Running→Ready Suspend: For the preemptive time-sharing system, when a high-priority blocked suspend process enters the ready suspend due to an event, the system will convert the currently running process to the ready suspend state;

        ④ Blocking pending → ready pending: occurs in the external memory, when the blocked suspended process occurs due to related events, the system will convert the blocked suspended process into a ready suspended process;

The unsuspend/activate flag transfers a process from external storage to internal memory, generally in the following situations:

        ① Ready pending → Ready: When there is no ready process or the priority of the ready pending process is higher than that of the ready process, the ready pending process will be converted into a ready process;

        ② Blocking pending → blocking: When a process releases enough memory, the system will convert a high-priority blocking suspended process into a blocked process;

        The operating system manages and schedules processes in different states based on the state queue through the PCB and the defined process state; different states are represented by different queues (such as ready queue, blocking queue, etc.);

        The PCB of each process will add the process to the corresponding queue according to its state. When the state of a process changes, its PCB will be separated from one state queue and added to another queue;

2--Thread

2-1--The basic concept of thread

        A thread represents an execution process in a process; a process can be understood as combining a group of related resources (code segment, data segment, etc.) to form a resource platform, and a thread represents an execution process of code on this resource platform;

        Therefore, threads can be understood as: thread = process - shared resource

Advantages of threads:

        ① A process can have multiple threads at the same time;

        ② Each thread can be executed concurrently;

        ③ Each thread can share resources such as address space and files;

Disadvantages of threads:

        A thread crash will cause all threads of the process it belongs to to crash (security issue);

2-2--Comparison between processes and threads

① Process is the unit of resource allocation, and thread is the scheduling unit of CPU;

② The process has a complete resource platform, and the thread only enjoys the essential resources, such as the register and stack in the above figure;

③ Threads also have three basic states of ready, blocked and executing, and also have transition relationships between states;

④ Threads can reduce the time and space overhead of concurrent execution:

        Thread creation and termination times are shorter than processes;

        The thread switching time in the same process is also shorter than that of the process;

        Since memory and file resources are shared between threads of the same process, it is not necessary to communicate through the kernel ;

Guess you like

Origin blog.csdn.net/weixin_43863869/article/details/130505216