[Operating System] Process Management (1) - Process

1. The concept, composition and characteristics of the process

Program: It is static, it is an executable file stored in the disk, such as: QQ.exe.

Process: It is dynamic, and it is an execution process of the program, such as: multiple QQ programs can be started at the same time.

Multiple executions of the same program will correspond to multiple processes.

How does the program work?

A high-level language code may be translated into multiple machine instructions.

The process of program running is actually the process of CPU executing machine instructions one by one.

insert image description here
A process entity (process image) consists of PCB, program segment, and data segment.

Processes are dynamic, and process entities (process images) are static.
The process entity reflects the state of the process at a certain moment (for example: after x++, x=2)

The composition of the process

The program segment, data segment, and PCB constitute the process entity (process image). After introducing the concept of process entity, a process can be defined as:
a process is the running process of a process entity, and is an independent unit for the system to allocate and schedule resources.

insert image description here
The PCB is for the operating system. The program segment and data segment are for the process itself.

Hanging up three QQ numbers at the same time will correspond to three QQ processes. Their PCBs and data segments are different, but the content of the program segments is the same (they are all running the same QQ program).

The composition of the process - PCB

When a process is created, the operating system will assign a unique, non-repeating "identity number" to the process - PID (Process ID, process ID). PCB is a sign of the existence of a process . When a process is created, the operating system creates a PCB for it. When the process ends, its PCB will be recycled.

In PCB (Process Control Block), the function of the process control block :

  • The operating system should record the PID and the user ID (UID) to which the process belongs [Basic process description information, which allows the operating system to distinguish each process]
  • Record what resources are allocated to the process (such as: how much memory is allocated, which I/O devices are being used, and which files are being used) [can be used to implement resource management by the operating system]
  • Record the running status of the process (such as: CPU usage time, disk usage, network traffic usage, etc.) [can be used to realize the control and scheduling of the process by the operating system]

The operating system needs to manage each concurrently running process, but all the information required for management will be placed in the basic process description information in the PCB, which allows the operating system to distinguish each process and can be used to realize the resource management of the operating system.

process characteristics

Programs are static and processes are dynamic. Compared with programs, processes have the following characteristics:

insert image description here

insert image description here

2. The state and transition of the process

insert image description here

The state of the process - creation state, ready state

insert image description here

When a process is being created, its state is "creation state". At this stage, the operating system will allocate resources for the process and initialize the PCB.

When the process is created, it enters the "ready state". The process in the ready state is ready to run, but because there is no idle CPU, it cannot run temporarily.

The state of the process - running state

If a process is running on the CPU at this time, then the process
is in the "running state". The CPU will execute the program (execute sequence of instructions) corresponding to the process.

insert image description here

The state of the process - blocked state

During the running of a process, it may be requested to wait for an event to occur (such as waiting for the allocation of a certain system resource, or waiting for the response of other processes).
Before this event occurs, the process cannot continue to execute. At this time, the operating system will let this process off the CPU and let it enter the "blocking state". When the CPU is idle, it will choose another "ready state" process to run on the CPU.

The state of the process - terminated state

insert image description here

A process can execute the exit system call to request the operating system to terminate the process.
At this time, the process will enter the "termination state", and the operating system will let the process go down the CPU, and reclaim resources such as memory space, and finally recycle the PCB of the process.
When the work of terminating the process is complete, the process disappears completely.

Process State Transitions

insert image description here

insert image description here

In the process PCB, there will be a variable state to represent the current state of the process. For example: 1 means the creation state, 2 means the ready state, and 3 means the running state...
In order to uniformly manage each process in the same state, the operating system will organize the PCB of each process.

3. Organization of the process

Organization of processes - linking methods

insert image description here

Process organization - indexing

insert image description here

insert image description here

Summarize

insert image description here

4. Process control

The main function of process control is to effectively manage all processes in the system. It has
functions such as creating new processes, canceling existing processes, and realizing process state transitions.

Simplified understanding: anyway, process control is to realize process state transition

insert image description here

insert image description here

How to achieve process control?

Implemented with "primitive language"; the execution of primitive language is "atomic" and completed in one go. If it cannot be done in one go, it may lead to the inconsistency of some key data structure information in the operating system, which will affect other management tasks of the operating system.

insert image description here

The execution of primitives is atomic, that is, the execution process can only be completed in one go, and no interruption is allowed during this period. Atomicity can be achieved by using the two privileged instructions "off interrupt instruction" and "on interrupt instruction"

insert image description here

Normal situation: Every time the CPU executes an instruction, it will routinely check whether there is an interrupt signal that needs to be processed. If there is, the current program will be suspended and the corresponding interrupt handler will be executed instead.

Primitives related to process control

insert image description here

insert image description here

insert image description here

insert image description here

insert image description here

Summarize

insert image description here
Learning tip: Process control can cause process state transitions. No matter which process control primitive, there are only three types of things to do:

  1. Update the information in the PCB
    a. All process control primitives must modify the process status flag
    b. Depriving the CPU usage right of the current running process must save its running environment
    c. Before a process starts running, it must restore the running environment
  2. Insert the PCB into the appropriate queue
  3. Allocate/reclaim resources

insert image description here

Five, process communication (IPC)

Inter-Process Communication (IPC) refers to data interaction between two processes .
insert image description here

A process is a unit that allocates system resources (including memory address space), so the memory address spaces owned by each process are independent of each other. For security reasons, one process cannot directly access the address space of another process.

To avoid errors, each process should be mutually exclusive access to the shared space. Each process can use the synchronization mutual exclusion tools provided by the operating system kernel (such as P, V operation).

Process Communication - Shared Storage

insert image description here

Sharing based on data structure : For example, only one array with a length of 10 can be placed in the shared space. This sharing method is slow and restrictive, and it is a low-level communication method.

Sharing based on the storage area : the operating system sets up a shared storage area in the memory, and the form and storage location of the data are controlled by the communication process, not the operating system. This type of sharing is fast and an advanced form of communication.

Process communication - message passing

Data exchange between processes takes formatted messages (Message) as the unit. Processes exchange data through the two primitives "send message/receive message" provided by the operating system.

insert image description here

insert image description here

Messaging (direct communication method)

insert image description here

Message passing (indirect communication method)

The indirect communication method uses "mailbox" as an intermediate entity for message delivery.

Multiple processes can send messages to the same mailbox, or multiple processes can receive messages from the same mailbox.

insert image description here

Process communication - pipeline communication

"Pipeline" is a special shared file, also known as pipe file. In fact, it is to open up a fixed-size memory buffer in memory.

insert image description here

  1. The pipeline can only use half-duplex communication, and only one-way transmission can be realized within a certain period of time. If two-way simultaneous communication is to be achieved,
    two pipes need to be set up.
  2. Each process must have exclusive access to the pipe (implemented by the operating system)
  3. When the pipeline is full, the writing process will be blocked until the reading process takes the data in the pipeline, and then the writing process can be woken up.
  4. When the pipe is empty, the reading process will be blocked until the writing process writes data into the pipe, then the reading process can be woken up.
  5. Once the data in the pipeline is read, it disappears completely. Therefore, when multiple processes read the same pipe, they may panic. In this regard,
    there are usually two solutions: ①One pipeline allows multiple writing processes and one reading process (official answer of 408 Zhenti Higher Education Society in 2014); ②Multiple writing processes and multiple reading processes are allowed, but the system will let Each reading process takes turns reading data from the pipe (Linux solution).

insert image description here

The writing process writes data to the pipe, even if the pipe is not full, as long as the pipe is empty, the reading process can read data from the pipe.
The reading process reads data from the pipe, even if the pipe is not read empty, as long as the pipe is not full, the writing process can write data to the pipe.

Guess you like

Origin blog.csdn.net/weixin_43848614/article/details/127128079