Summary of computer operating system notes: Part2 process and thread


1 process

1.1 The concept, composition and characteristics of the process

Program: It is static , it is an executable file stored in the disk, and it is a series of instruction sets.
Process (Process): It is dynamic and is an execution process of a program. A process is the running process of a process entity and an independent unit for resource allocation and scheduling by the system.

Example: There is only one QQ program, but multiple QQs can be opened at the same time, and there will be several more processes. That is, multiple executions of the same program will correspond to multiple processes.
insert image description here

❓Thinking : The operating system is the manager of these processes, how does it distinguish each process?

Answer: When a process is created, the operating system will assign a unique and non-repeating "ID number" to the process - PID (Process ID, process ID).

The composition of the process
The entity (process image) of a process 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.
insert image description here

Process control block (PCB) - the only sign of the existence of the process!
The operating system needs to record the PID, the user ID (UID) to which the process belongs, and what resources are allocated to the process (such as: how much memory is allocated, which I/O devices are being used, which files are being used), and the process's Operation status (such as: CPU usage time, disk usage, network traffic usage, etc.).
The above information is stored in a data structure PCB (Process Control Block), that is, the process control block.
The information required by the operating system to manage the process is stored in the PCB. The PCB is the only sign of the existence of the process. When the process is created, the operating system creates a PCB for it. When the process ends, its PCB will be recycled.
insert image description here
Understand the composition of the process through the running process of the program:
before a program starts running, the program needs to be read from the hard disk into the memory. Before running, it is necessary to create the corresponding process, and at the same time create the corresponding process control block, that is, PCB. The CPU then reads a sequence of instructions, which we call program segments. In the process of executing the program segment, some data will be generated, which we call the data segment.
insert image description here

The characteristics of the process
The program is static and the process is dynamic. Compared with the program, the process has the following characteristics:

  • Dynamics: Dynamics is the most fundamental characteristic of a process. A process is an execution process of a program, which is dynamically generated, changed and destroyed.
  • Concurrency: There are multiple process entities in memory, and each process can execute concurrently.
  • Independence: A process is a basic unit that can run independently, obtain resources independently, and receive scheduling independently.
  • Asynchrony: Each process moves forward at an independent and unpredictable speed, and the operating system must provide a "process synchronization mechanism" to solve the asynchronous problem.
  • Structural: Each process configures a PCB. Structurally, a process is composed of program segment, data segment and PCB.

1.2 Process state and transition

state of the process

Creation state, ready state
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 thread is created, it will enter the "ready state". The process in the ready state is ready to run, but because there is no idle CPU, it cannot run temporarily.

Running state
When the CPU is idle, the operating system will select a ready process and let it run on the processor. If a process is running on the CPU at this time, the process is in the "running state", and the CPU will execute the program (execute instruction sequence) corresponding to the process. There may be many processes in the system that are in the ready state.

Blocking state
In the process of running a process, it may be requested to wait for an event to occur (for example, 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 downwards. At this time, the operating system will let the process off the CPU and put it into a "blocked state".

Terminated state
A process can execute the exit system call, requesting the operating system to terminate the process. At this time, the process will enter the "terminated state", and the operating system will let the process go to the CPU, and reclaim resources such as memory space, and finally recycle the PCB of the process.

Process state transition
As shown in the figure:
insert image description here
In the entire life cycle of a process, most of the time is in three basic states: running state, ready state, and blocked state. In the case of a single CPU, only one process is running at the same time. In the case of a multi-core CPU, there may be multiple processes in the running state.

In the process PCB, there will be a variable state to represent the current state of the process. For example: 1 means creation state, 2 means ready state, 3 means running state...

1.3 Organization of the process

linking and indexing
insert image description here
insert image description here

1.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.

In short, the control of the process is to realize the conversion of the process state.

How to achieve process control?
Process control is implemented using "primitives". The execution of primitives is "atomic" and completed in one go.

So why does process control need to be done in one go?
Eg assumes that the variable State in the PCB represents the current state of the process, 1 represents the ready state, 2 represents the blocked state... as shown in the following figure:
insert image description here

Assuming that the event waiting for process 2 occurs at this time, the kernel program responsible for process control in the operating system needs to complete the following operations at least:

  1. Set the state of PCB2 to 1;
  2. Put PCB2 from the blocking queue to the ready queue.

If, after the first step is completed, an interrupt signal is received, then PCB2's state = 1 will occur at this time, which is a ready state, but it is placed in the blocking queue.

Therefore, if it cannot be done in one go, it may lead to the inconsistency of some key structural information in the operating system, which will affect the operating system to perform other management tasks.

How to achieve the "atomicity" of primitives?
The execution of primitives is atomic, that is, the execution process can only be completed in one go, and no interruption is allowed during the execution. Atomicity can be realized by using the two privileged instructions "disable interrupt instruction" and "enable interrupt instruction".
Under normal circumstances, the CPU routinely checks whether there is an interrupt signal that needs to be processed after each instruction is executed. If there is, the current program will be suspended and the corresponding interrupt handler will be executed instead.
insert image description here
After the CPU executes the interrupt-off command, it will no longer check the interrupt signal routinely, and will not resume the check until the interrupt-on command is executed.
insert image description hereIn this way, these instruction sequences between turning off the interrupt and turning on the interrupt cannot be interrupted, which realizes "atomicity".

Primitives related to process control

Create primitives
insert image description here

undo primitive
insert image description here

Blocking Primitives and Wakeup Primitives
Because something blocks, it should be woken up by something. Therefore, blocking primitives and wakeup primitives are used in pairs.
insert image description here

Switching primitives
Switching primitives realize the mutual switching between the running state and the ready state.
insert image description here

When the process is switched, the running environment of this process is generally saved in the PCB (save some necessary register information). When the original process is put into operation again, its running environment can be restored through the PCB.

1.5 Process communication

What is interprocess communication?
Inter-Process Communication (IPC) refers to data interaction between two processes.

Why does process communication need the support of the operating system?
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, each process can only access the address space of its own process, but not the address space of other processes. That is: a process cannot directly access the address space of another process.
insert image description here

Three ways of process communication

Shared storage
is based on the sharing of 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.
If process P and process Q want to communicate, they can apply for a shared memory area. In this way, the communication between the P and Q processes can be realized by accessing the shared memory area.
However, if multiple processes write data in the shared storage area, it may cause write conflicts and data overwriting problems.
In order to avoid errors, each process's access to the shared space should be mutually exclusive.
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.

Message Passing
Data exchange between processes is in units of formatted messages . Processes exchange data through the two primitives "send message/receive message" provided by the operating system
insert image description here
Direct Communications:
Name-by-name messaging. Process A sends the primitive, edits the message header and message body, and specifies who to send it to. Then the PCB of the process Q in the operating system kernel is received and stored in the message queue. Then the Q process searches for the message sent by the P process in the message queue by receiving the primitive, and completes the reception.
insert image description here

Indirect communication method:
the mailbox is used as an intermediate entity for message delivery. Multiple processes can send messages to the same mailbox, and multiple processes can receive messages from the same mailbox.
insert image description here

Pipeline communication
The flow of data can only be one-way (not at the same time, it is one-way at a certain moment). One end writes data, and the other end reads data. First in first out.
The pipeline mentioned here is a special shared file, also known as a pipe file. In fact, it is to open up a fixed-size memory buffer in memory.
The communication method of shared storage is more free than pipeline communication, because the storage method of shared storage is unlimited and relatively free, while the buffer area of ​​pipeline communication is more like a circular queue.
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 , two pipes need to be set up.
  2. Each process has 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 pipeline is empty, the reading process will block until the writing process writes data to the pipeline, and 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, panic may occur. In this regard, there are usually two solutions: (1) One pipeline allows multiple writing processes and one reading process; (2) Multiple writing processes and multiple reading processes are allowed, but the system will allow each reading process to read from the pipeline in turn. Read data.

2 Thread and multi-thread model

2.1 The concept of thread

A thread can be understood as a lightweight process.
A thread is a basic CPU execution unit and the smallest unit of program execution flow.

Why introduce threads?

insert image description here
After the thread is introduced, not only the processes can be concurrent, but also the threads in the process can be concurrent, which further improves the concurrency of the system, so that a process can also process various tasks concurrently (such as QQ video, text chat, etc.) ,send file).

The changes brought about by the introduction of threads are as follows:

insert image description here
The properties of a thread are as follows:

  • Thread is the unit of processor scheduling
  • In a multi-CPU computer, each thread can occupy a different CPU
  • Each thread has a thread ID, thread control block (TCB)
  • Threads also have three basic states: ready, blocked, and running
  • Threads own few system resources
  • Sharing process resources among different threads of the same process
  • Inter-thread communication within the same process does not even require system intervention due to shared memory address space
  • Thread switching in the same process does not cause process switching
  • Thread switching in different processes will cause process switching
  • Switching threads in the same process has a small system overhead; switching processes has a large system overhead

2.2 Implementation of threads

1. User-level threads
In early operating systems, such as Unix, only processes were supported and threads were not supported. The "thread" at that time was implemented through the thread library.
1. The user-level thread is implemented by the application program through the thread library, and all thread management work is taken care of by the application program (including thread switching)
2. In the user-level thread, the thread switching can be completed in the user state without the intervention of the operating system .
3. From the user's point of view, there are multiple threads. But from the perspective of the operating system kernel, it is not aware of the existence of threads. "User-level threads" are "threads that can be seen from the user's perspective"

Advantages: user-level thread thread switching can be completed in user space, without switching the core state, thread management overhead is small, and the efficiency is high.
Disadvantages: When a user-level thread is blocked, the entire process will be blocked, and the concurrency is not high.
insert image description here

2. Kernel-level threads
Kernel-level threads are threads supported by the kernel and supported by the operating system.
1. The management of kernel-level threads is done by the operating system kernel.
2. The kernel is responsible for thread scheduling and switching, so the switching of kernel-level threads must be completed in the core state.
3. The operating system will establish a corresponding TCB (Thread Control Block, thread control block) for each kernel-level thread, and manage the thread through the TCB. "Kernel-level threads" are "threads that can be seen from the perspective of the operating system kernel"

Advantages: When a thread is blocked, other threads can continue to execute, with strong concurrency. Multiple threads can be executed in parallel on multi-core processors.
Disadvantages: A user process will occupy multiple kernel-level threads. The thread switching is completed by the operating system kernel and needs to be switched to the kernel state. Therefore, the cost of thread management is high and the overhead is high.
insert image description here

2.3 Multithreading model

1. One-to-one model
A user-level thread is mapped to a kernel-level thread. Each user process has the same number of kernel-level threads as user-level threads.
Advantages: When a thread is blocked, other threads can continue to execute, with strong concurrency. Multiple threads can be executed in parallel on multi-core processors.
Disadvantages: A user process will occupy multiple kernel-level threads. Thread switching is completed by the operating system kernel and needs to be switched to the core state. Therefore, the cost of thread management is high.

The diagram just exemplified in the kernel state is a one-to-one model.

2. Many-to-one model
Multiple user-level threads are mapped to one kernel-level thread. And a process is assigned only one kernel-level thread.
Advantages: The switching of user-level threads can be completed in user space without switching to the core state. The system overhead of thread management is small and the efficiency is high.
Disadvantages: When a user-level thread is blocked, the entire process will be blocked, and the degree of concurrency is low. high. Multiple threads cannot run in parallel on multi-core processors
insert image description here
3. Many-to-one model
n users and threads are mapped to m kernel-level threads (n>=m). Each user process corresponds to m kernel-
level threads.
It overcomes the shortcoming of low concurrency in the many-to-one model (one block blocks all), and also overcomes the shortcoming in the one-to-one model that one user process occupies too many kernel-level threads and the overhead is too large.
insert image description here

2.4 Thread state and transition

insert image description here


3 Processor Scheduling

In layman's terms, scheduling is to specify certain rules to determine the order in which these tasks are processed.

3.1 Three levels of scheduling

1. Low-level scheduling (process/processor scheduling)
selects a process from the ready queue according to a certain strategy, and assigns a processor to it.
Process scheduling is the most basic type of scheduling in an operating system, and process scheduling must be configured in a general operating system.
The frequency of process scheduling is very high, usually every tens of milliseconds.
insert image description here
2. Intermediate scheduling (memory scheduling)
When the memory is not enough, the data of some processes can be transferred out of the external memory. When the memory is free or the process needs to run, it will be reloaded into the memory. The state of the process that is temporarily transferred to the external memory to wait is the suspended state. Suspended process PCBs are organized into pending queues.
insert image description here

3. Advanced scheduling (job scheduling)
according to certain principles, selects a job from the job backup queue in the external storage and loads it into the memory, and creates a process. Each job is only called in once and called out once. The PCB is created when the job is called in, and the PCB is canceled when the job is called out.
A job can be simply understood as the user asking the operating system to start a program (to handle a specific task).

Comparison of three scheduling methods
insert image description here

3.2 The suspended state of the process and the seven-state model

The state of the process that is temporarily transferred to the external memory to wait is the suspended state (suspended state, suspend). The suspended state can be further subdivided into two states: ready suspended and blocked suspended.
insert image description here

3.3 Process Scheduling

3.3.1 Timing of process scheduling

Process scheduling (low-level scheduling) is to select a process from the ready queue to assign a processor to it according to a certain algorithm.
The schematic diagram given by Wang Dao is as follows:
insert image description here

3.3.2 Process Scheduling Mode

  • Non-preemptive scheduling, also known as non-preemptive scheduling. That is, only processes are allowed to voluntarily relinquish the processor. Even if more urgent tasks arrive during the running process, the current process will continue to use the processor until the process terminates or actively requests to enter the blocking state.
  • The deprivation scheduling method is also called the preemption method. When a process is executing on the processor, if there is a more important or more urgent process that needs to use the processor, immediately suspend the executing process and assign the processor to the more important and urgent process.

4 Scheduling algorithm (emphasis!!!)

4.1 Evaluation index of scheduling algorithm

CPU utilization
refers to the ratio of the busy time of the CPU to the total time.

cpu utilization = busy time / total time

System throughput
For computers, it is hoped to process as many jobs as possible in as little time as possible. Therefore, system throughput refers to the number of jobs completed per unit time.

System throughput = total number of jobs completed / total usage time

Turnaround time and average turnaround time
For computer users, he is very concerned about how long it takes for his assignment to be completed from submission to completion.
Turnaround time is the time interval between when a job is submitted to the system and when it is completed.
It includes four parts: the time the job waits for job scheduling (advanced scheduling) on ​​the external memory backup queue, the time the process waits for process scheduling (low-level scheduling) on ​​the ready queue, the time the process executes on the CPU, and the process waits for I/0 The time the operation completed. The latter three items may occur multiple times throughout the processing of a job.

(User Concern) Turnaround Time = Job Completion Time - Job Submission Time
(System Concern) Average Turnaround Time = Sum of Job Turnaround Times / Number of Jobs

The waiting time
refers to the sum of the time that the process/job is in the state of waiting for the processor . The longer the waiting time, the lower the user satisfaction.

  • For the process : The waiting time refers to the sum of the waiting time for the process to be served after the process is established, and the period of waiting for I/O completion is actually served, so it is not included in the waiting time.
  • For jobs : not only the waiting time after the process is established, but also the waiting time of the job in the external storage backup queue should be considered.

4.2 Scheduling Algorithms for Batch Processing Systems

4.2.1 First come first served (FCFS)

Serve jobs/processes in the order they arrive.
insert image description here

insert image description here

4.2.2 Short Job First (SJF)

The shortest job/process is served first (the so-called "shortest" refers to the shortest required service time), but the running time of the job/process is provided by the user, which is not necessarily true, and it is not necessarily possible to achieve a real short time. Homework priority!
insert image description here
The short job first algorithm is divided into two cases, the specific examples are as follows:

At non-preemptive
0 time, P1 has arrived, and the processor on P1 runs for 7s, and during this 7s, P2, P3, and P4 processes all arrive, so after P1 ends, P3 that needs the shortest service time is given priority to run. When P3 is over, since the service time requested by P2 and P4 is the same, the one that arrives first will be run first, that is, P2 will be run first, and then P4 will be run.
insert image description here

Preemptive
is to use the shortest remaining time first algorithm: whenever a process is added to the ready queue and changes, it needs to be scheduled. If the remaining time of the newly arrived process is shorter than the remaining time of the currently running process, the new process will preempt the processor.
insert image description here
insert image description here

4.2.3 High Response Ratio Priority (HRRN)

The FCFS algorithm selects a job (process) with the longest waiting time to serve it each time it is scheduled. But it does not take into account the running time of the job, which leads to the problem that it is not friendly to short jobs. The SJF algorithm is to select a job with the shortest execution time to serve it. However, it does not consider the waiting time of each job at all, which leads to the problem of being unfriendly to long jobs, and even causing starvation problems. Therefore, a high response ratio priority scheduling algorithm
is proposed
insert image description here

This algorithm is a non-preemptive scheduling algorithm. Scheduling is only required when the currently running process voluntarily relinquishes the CPU. Each schedule selects the process with the highest response ratio!

Response Ratio = (Wait Time + Requested Service Time) / Requested Service Time

insert image description here

The three scheduling algorithms described above are generally applicable to early batch processing systems, and then the scheduling algorithms for interactive systems will be introduced.

4.3 Scheduling Algorithms for Interactive Systems

4.3.1 Time slice rotation (RR)

Let the processes in the ready queue execute a time slice in turn (each time the process at the head of the ready queue is selected)
insert image description here

Example questions are as follows:

insert image description here
insert image description here
insert image description here
Choice of time slice size:

  • If the time slice is too large, so that each process can be completed within one time slice, the time slice round-robin scheduling algorithm will degenerate into a first-come-first-serve scheduling algorithm, increasing the process response time.
  • If the time slice is too small, frequent process switching will occur, and the system will spend a lot of time processing process switching, resulting in a reduction in the proportion of time spent on process execution.

4.3.2 Priority scheduling algorithm

The job/process with the highest priority is chosen when scheduling. Divided into preemptive and non-preemptive. The non-preemptive type only needs to be scheduled when the process voluntarily gives up the processor, while the preemptive type also needs to check whether preemption will occur when the ready queue changes.
insert image description here

Non-preemptive
The process that has arrived and has the highest priority is selected each time it is scheduled. Scheduling occurs when a process voluntarily relinquishes a processor.
insert image description here

Preemptively
select the process that has arrived and has the highest priority each time it is scheduled. Scheduling occurs when the current process voluntarily gives up the processor, and it is also checked whether preemption occurs when the ready queue changes.
insert image description here

Replenish
There may not be only one ready queue, and it can be organized according to different priorities. In addition, processes with high priority can also be
queued to work closer to the head of the queue.
According to whether the priority can be changed dynamically, the priority can be divided into static priority and dynamic priority.
Static priority: Determined when the process is created, and remains unchanged thereafter.
Dynamic priority: There is an initial value when a process is created, and then the priority will be dynamically adjusted according to the situation.

4.3.3 Multi-level feedback queue scheduling algorithm (subsequent supplement)


The pictures in this article are from the public video of Wang Daoyan Computer Operating System 408, and the content of this article is only used as study notes for communication.

Guess you like

Origin blog.csdn.net/m0_60353039/article/details/126950120