Compilation of computer operating system knowledge - Summary of process and thread knowledge (Introduction Guide to Computer Operating Systems)

Chapter 2 Processes and Threads

There is a lot to learn about processes and threads. The blogger will gradually organize and update the content. Currently, 20,000+ words of pictures and text have been updated...

2.1 Process

2.1.1 The concept of process

When it comes to processes, we have to connect them with programs, because a process is an execution process of a program and is the basic unit of resource allocation and scheduling in the system .

Note: The concept of threads has not been introduced at this time.

We know that a program is an executable file stored on the hard disk , that is, a series of instructions. Under multi-programming, the operating system allows programs to execute concurrently, which results in the loss of closure between these programs. In order to better describe and control the concurrent execution of programs, the operating system introduces the concept of process .

Examples of processes and procedures:

When we start an application A, we can see a corresponding process in the control panel. If we start multiple application A, multiple corresponding processes will appear. For multiple applications A that are started, their instruction set is the same, but each corresponding process has its own data (this part is related to the composition of the process, we will study it in detail in the next chapter ), that is, the same application executed multiple times will correspond to multiple different processes.

Section summary

Insert image description here

2.1.2 Characteristics of processes

Process is a concept derived from the concurrent execution of multiple programs. Therefore, the basic characteristics of a process are compared to the sequential execution of a single program, and it is also a basic requirement for process management.

Characteristics of processes include dynamics, concurrency, independence, and asynchronousness.

  • Dynamic

    A process is an execution of a program. It has processes such as creation, activity, suspension, and termination. It has a certain life cycle and is dynamically generated, changed, and destroyed. Therefore, dynamics is the most basic characteristic of the process.

  • Concurrency

    It means that multiple processes exist in the memory at the same time and can run at the same time for a period of time. The purpose of introducing processes is to enable multiple processes to execute concurrently. Concurrency is an important feature of processes and also of operating systems.

  • independence

    It means that a process is a basic unit that can run independently, obtain resources independently, and accept scheduling independently.

  • Asynchronicity

    Due to the mutual constraints of processes, processes execute at independent and unpredictable speeds. Asynchrony will lead to non-reproducibility of execution results. For this reason, the corresponding process synchronization mechanism must be configured in the operating system.

These four characteristics of the process only need to be understood.

Section summary

Insert image description here

2.1.3 Composition of processes

Before introducing the composition of the process, we will introduce a new concept: process entity (process image).

The process entity consists of three parts: process control block , program segment and data segment . After introducing the concept of process entity, a process can also be defined as " a process is the running process of a process entity and an independent unit for resource allocation and scheduling by the system. " That is, the process entity is static and the process is dynamic. If no special distinction is made, we generally refer to process entities as processes . Therefore, it can also be said that a process is an independent running unit and the basic unit for resource allocation and scheduling by the operating system. It consists of three parts: process control block, program segment and data segment .

2.1.3.1 Process control block
  • definition

    Process Control Block (PCB) is a data structure specially configured for a process and is used to describe the basic situation and running status of the process.

  • relationship with process

    When a process is created, the operating system will create a PCB for it, and then this PCB will be placed in the memory. When the process is executed, the system can understand the current status information of the process through the PCB of the process, so the operating system can It can be controlled and managed. When the process ends, the operating system will also delete the PCB from the memory, and the process will die. It can be known from this that PCB is the only sign of the existence of a process .

  • Main information contained in the process control block

    PCB mainly contains process description information, process control and management information, resource allocation list and processor-related information.

    Process description information Process control and management information resource allocation list Processor related information
    Process, user identifier (PID, UID) Current status of the process, process priority Code run entry address, program external memory address and other code segments, data segment, stack segment pointer, file descriptor, keyboard, mouse, etc. General, address, control, flag register values, status words, etc.
    • Process description information

      Process identifier: identifies each process, and each process has a unique ID.

      User identifier: Identifies the user to which each process belongs.

    • Process control and management information

      Current status of the process: describes the status information of the process as the basis for processor allocation and scheduling.

      Process priority: Describes the priority of a process to seize the processor. The higher the priority process, the easier it is to seize the processor.

    • resource allocation list

      Used to describe the status of the memory address space or virtual address space, the list of open files and the I/O device information used.

    • Processor related information

      Also called the context of the processor, it mainly refers to the value of each register in the processor. When the process is running, a lot of information about the processor is in the registers. When the process is suspended, the status information of the processor must be is saved in the corresponding PCB so that when the process is re-executed, execution can continue from the last execution position.

  • How the system uses PCB to control the process

    1. When the operating system wants to schedule a process to run, it must find out its current status and priority from the PCB of the process.
    2. When the operating system successfully schedules the process, it must set the process to resume running (continue execution from where it was last interrupted) based on the processor status information saved in its PCB, and based on the memory starting address of the program and data in its PCB , find its programs and data.
    3. During the execution of the process, if it needs to synchronize, communicate or access files with the process it cooperates with, it also needs to access the PCB.
    4. When a process suspends execution for some reason, the processor context of its breakpoint needs to be saved in the PCB.

    It can be seen from the above process that throughout the life cycle of the process, the system controls the process through the PCB, that is, the system can only perceive the existence of the process through the PCB.

  • How PCB is organized

    Since there are often many processes in the operating system, some are in the ready state and some are in the blocked state, and the reasons for blocking are different. In order to facilitate process scheduling and management, it is necessary to organize the PCB of each process with an appropriate structure. There are two common methods: link method and index method. (The status of the process will be introduced in the next section)

    The link method connects the PCB into a queue. Different states correspond to different queues. They are divided into different queues according to the different states of the process, such as ready queue and blocking queue . They are even divided into different blocking queues according to different reasons for blocking.

    The index method is to put the PCB into an index table. Different states correspond to different index tables, such as ready index table and blocking index table .

2.1.3.2 Program segment

The program segment is the program code segment that is scheduled to be executed by the CPU by the process scheduler, that is, the code segment to be executed by the process in the CPU.

Note: A program can be shared by multiple processes, that is, multiple processes can run the same program.

2.1.3.3 Data segment

The data segment of a process contains the original data processed by the program corresponding to the process and the intermediate/final results generated during the running of the process.

Section summary

Insert image description here

2.1.4 Process status and transitions

2.1.4.1 Five states of process

A process has a complete life cycle, which mainly includes five states: creation state , ready state , running state , blocking state and end state .

  • Created state

    When the process is being created, it is in the creation state. At this stage, the operating system will allocate various resources to the process and initialize the PCB.

    When creating a process, it is divided into multiple steps

    1. Request a blank PCB and fill in the PCB with information for controlling and managing the process
    2. Allocate resources necessary for the process to run
    3. Transfer the process to the ready state and place it in the ready queue. If the resources required by the process cannot be met at this time, it cannot be transferred to the ready state. At this time, the creation of the process has not been completed, and the process is in the process of creation. state
  • ready state

    After the process obtains all necessary resources except the CPU, it will be transferred to the ready state. Once the process in this state is called by the CPU, it will be executed immediately. Since a single-core CPU can only execute one process at a time, there will be a large number of ready processes in the system, and these processes will be placed in a queue, which is called the ready queue .

  • Running state

    The state in which a process is running when it is running on the CPU is the running state.

  • blocking state

    When a running process needs to wait for an event and is suspended, it is in a blocking state. A process in this state will continue to wait for a resource (for example, waiting for an I/O device). A process in this state will not be executed even if the CPU is idle. Since many resources in the system are shared exclusively, there will also be multiple blocked processes in the system, and these processes will be placed in a queue. This queue is called a blocking queue, and the system will even The reason is to place these blocked processes in different blocking queues.

  • end state

    When a process needs to be terminated, the system will transfer the process to the termination state.

  • The difference between ready state and blocking state

    A process in the ready state has all necessary resources except the CPU. It only needs to obtain the CPU resource and will be executed immediately, while the process in the blocked state continues to wait for a resource other than the CPU, even if the CPU is idle at the moment. , the process will not be executed.

Notice:

Ready state, running state, and blocked state are called the three basic states of the process.

2.1.4.2 Process state transition

The following figure represents the transitions between the five states of the process

Insert image description here

Among them, the more important state transitions are the transitions of the three basic states.

  • Ready state->Running state

    A process in the ready state will enter the running state after being scheduled by the CPU and obtaining CPU resources.

  • Running state->Ready state

    A process in the running state will be transferred to the ready state after the allocated time slice is used up.

  • Running state->Blocking state

    A process in the running state will change from the running state to the blocking state if it requests the use of a certain resource or waits for the occurrence of a certain event. The process will actively request the operating system to provide services through system calls. That is, converting from the running state to the blocking state is an active behavior of the process.

  • Blocked state->Ready state

    After the resource requested by the process in the blocked state or the event it is waiting for arrives, the interrupt handler will transfer the process from the blocked state to the ready state.

Notice:

The transition of a process from the running state to the blocked state is an active behavior of the process, while the transition from the blocked state to the ready state is a passive behavior and requires the assistance of other processes and interrupt handlers.

Section summary

Insert image description here

2.1.5 Process control

2.1.5.1 Definition and implementation method
  • Definition of process control

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

  • How to implement process control

    As can be seen from the above, process control realizes the conversion of process status. The process state conversion process needs to be completed "in one go". Therefore, we can implement process control using the primitives introduced in Chapter 1 . (For an introduction to primitives, see section 1.3.5.3)

    Why must we ensure that the process state transition is completed "in one go"?

    Next, we will give an example. Assume that there is a process in the ready state at this time (that is, the PCB of the process is in the ready queue). Now we want to convert the process into the blocking state. At this time, the kernel program responsible for process control must at least do The following two things:

    1. Set the status of the PCB of the process to the blocking state
    2. Place the PCB of the process in the blocking queue

    Obviously, the above two steps must be completed in one go. If the above two steps cannot be completed in one go, for example, after completing the first step, the CPU receives an interrupt signal and then executes the corresponding interrupt handler. , then at this time, the status of the PCB of the process is blocked, but the PCB is in the ready queue, which leads to the inconsistency of key data structure information in the operating system, thus affecting the subsequent management work of the operating system.

2.1.5.2 Creation of process

The process state corresponding to the creation of the process is converted to:

  • None->Created state
  • Created state->Ready state

The creation of a process is achieved through the creation primitive , which includes the following steps:

  1. Assign a unique process identification number (PID) to the new process and apply for a blank PCB. If the PCB application fails, the creation fails (PCB is limited).
  2. Allocate resources necessary for the process to run (memory, files, CPU time, I/O devices, etc.). If the resources are insufficient, the process will be in the creation state at this time, waiting for necessary resources.
  3. Initializing the PCB mainly includes initialization flag information, processor status information, processor control information, and setting the priority of the process.
  4. Insert the PCB into the ready queue (if the ready queue is not full), set the PCB status to the ready state, and the process is converted to the ready state.

Events that cause process creation generally include the following:

  1. User login. In a time-sharing system, if the user logs in successfully, the system will create a process for it.
  2. Job scheduling, in a multi-channel batch processing system, if a new job is put into the memory, a process will be created for it.
  3. Provide services. When a user provides certain services to the operating system, a process will be created for it to handle the request.
  4. Application request, the user process actively requests the creation of a child process.

In the operating system, there is the concept of parent-child process , that is, one process is allowed to create another process. At this time, the creator is the parent process , and the created process is the child process . The child process can inherit all the resources of the parent process. When the child process When revoked, the resources it obtained from the parent process are returned to the parent process.

2.1.5.3 Termination of process

The process state corresponding to the termination of the process is converted to:

  • Running state/blocked state/ready state->terminated state

The termination of the process is achieved through the termination primitive , which includes the following steps:

  1. According to the PID of the terminated process, find the PCB of the process from the PCB collection and read the status of the process.
  2. If the process is in execution state, the execution of the process is immediately terminated and the CPU is allocated to other processes.
  3. If the process has descendant processes, all of its descendant processes will be terminated.
  4. Return all resources owned by the process to the operating system or its parent process.
  5. Remove the PCB from the queue.

Events that can cause process termination generally include the following:

  1. Ending normally means that the process's task has been completed and is ready to exit (the process itself requests termination, such as the exit system call).
  2. Abnormal end means that an abnormal event occurred while the process was running, making it impossible for the program to continue running (integer division by 0, illegal use of privileged instructions, run timeout, I/O failure, etc.).
  3. External intervention means that the process terminates at the request of the outside world, such as user or operating system intervention, parent process request and parent process termination.
2.1.5.4 Process blocking

The process state corresponding to the blocking of the process is converted to:

  • Running state->Blocking state

Process blocking is achieved through blocking primitives , which include the following steps:

  1. Through the PID of the process, find the PCB of the process from the PCB collection and read the status of the process
  2. If the process is in the running state, protect its running site, convert it to the blocking state, and stop running.
  3. Insert the PCB into the corresponding waiting queue and schedule the CPU to other processes in the ready state.

Events that can cause process blocking generally include the following:

  1. A process failed to request certain system resources, such as waiting for I/O device resources.
  2. A process waits for the completion of some operation.
2.1.5.5 Process wake-up

The process state corresponding to the wake-up of the process is converted to:

  • Blocked state->Ready state

The wake-up of a process is achieved through the wake-up primitive . The wake-up of a process is based on a certain event and is intended to wake up a blocked process waiting for a certain event. It includes the following steps:

  1. Through the PID of the process, find the PCB of the corresponding process from the waiting queue of the event.
  2. Remove it from the waiting queue and set it to ready state.
  3. Insert the PCB into the ready queue and wait for CPU scheduling.

Events that can cause a process to wake up generally include the following:

  1. The system resources requested by the blocked process are free and allocated to the process, such as I/O device resources.
  2. After the blocked process is waiting for some operation to complete.

Note: The blocking primitive and wake-up primitive of a process are a pair of primitives with exactly opposite effects and must be used in pairs. If a blocking primitive is called in a process, it must be used in a cooperating or other related process. Arrange a corresponding wake-up primitive in the process to wake up the blocked process. Otherwise, the blocked process will never be awakened and will be permanently blocked.

2.1.5.6 Process switching

The process state corresponding to the process switching is:

  • Running state->Blocked state/Ready state
  • Ready state->Running state

Process switching is achieved through switching primitives , which includes the following steps:

  1. According to the PID of the process, the corresponding PCB is found from the PCB collection, and the current running environment information is stored in the PCB.
  2. Move the PCB into the corresponding queue.
  3. Select another process to execute and update its PCB.
  4. The operating environment required to restore the new process according to the PCB.

Events that can cause process switching generally include the following:

  1. The process's time slice time has expired.
  2. Arrival by a higher priority process.
  3. The current process is actively blocked.
  4. The current process is actively terminated.

When switching processes, there is an important step - storing the running environment information in the PCB .

The running environment information mentioned here can be understood as the running context (Context) of the CPU. Through the previous study, we understand that there are various registers in the CPU (such as the PSW register used in CPU state switching). This aspect is the composition of the computer. The knowledge in the principle will not be introduced in detail here). When a process is running, a lot of information will be generated in the register. This information is the context in which the current process is running . When the CPU is occupied by other processes, it will be used again. When the current process is executed, the register information in the CPU may be overwritten by the information of other processes . Therefore, in order for the process to continue to run without errors, the running context information of the process needs to be saved to the PCB . When the CPU executes it again When the process is running, the running environment information of the process is read from the PCB , and the process can be continued to execute.

From the above introduction of the five primitives related to process control, we can make a further summary:

Regardless of the process primitive, what is done is contained in the following three things:

  1. Update the process PCB information (modify the status of the process, protect/restore the running environment information of the process).
  2. Insert the PCB into the appropriate queue (blocking queue, ready queue)
  3. Allocate/recycle resources

Note: context switching and mode switching are different. When mode switching, the CPU may still be logically executing the same process. User processes initially run in user mode. If the process enters the core mode due to interruption or exception, it will run in the core mode after execution. Return to user mode and run the process that was just interrupted. The switch between user mode and kernel mode is called mode switching . It does not change the current process. Context switching can only occur in kernel mode. It is a necessary feature in multi-tasking operating systems.

Section summary

Since there is too much content in this section, the blogger divided the mind map into several small pictures

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

2.1.6 Communication between processes

2.1.6.1 Introduction
  • What is inter-process communication?

    Process communication refers to the exchange of information (data exchange) between processes . Inter-process communication relies on the support provided by the operating system .

  • Why does inter-process communication rely on operating system support?

    From the above study, we know that the process is the basic unit for the operating system to allocate system resources, and the memory addresses owned by each process are independent of each other . Therefore, if data exchange is to be carried out between various processes, it must rely on the support of the operating system.

  • Why should the memory addresses owned by each process be independent of each other?

    From the perspective of system security, if the memory addresses owned by each process are not independent of each other, this means that each process can access each other's data. This will be a very dangerous thing. For example, some of your apps store your private information (bank account, ID card information, etc.). At this time, you install a malicious APP. When you run it at the same time, When these two apps are used, this malicious APP can freely access the data of your APP that stores private information, and then your private information will be at risk of being exposed.

    Therefore, to ensure security, one process cannot directly access the memory address of another process .

2.1.6.2 Classification of process communication

Inter-process communication can be divided into three categories: shared storage , message passing , and pipeline communication .

2.1.6.3 Shared storage
2.1.6.3.1 Introduction

Shared storage means that several processes that need to communicate apply for a shared space from the operating system. The shared space can be directly accessed by these processes. Information exchange between processes is realized by reading/writing operations on this shared space . The operating system will map this shared space to the virtual address space of the corresponding process. (will be introduced in subsequent chapters)

In layman's terms, there are two people A and B. They need to exchange items through the black box. A can only get the items that B puts into the black box from the black box, but cannot directly get items from B. In the same way, if B wants to give the item to A, he can only put the item into the black box first, but cannot give it to A directly.

Insert image description here

2.1.6.3.2 Features
  • In order to avoid errors, when reading/writing operations on shared space processes, you need to use synchronous mutual exclusion tools (such as P/V operations) to control reading/writing.
  • The operating system is only responsible for providing shared space and synchronization mutual exclusion tools for communication processes, and data exchange is completed by the user's own arrangement of read/write instructions.
2.1.6.3.3 Classification

Shared storage can be further divided into data structure-based sharing and storage-based sharing .

  • Data structure-based sharing (low-level)

    The operating system defines a shared space of a certain data structure in the memory for each process to use. For example, the shared space can only enlarge an array of size 10. This sharing method is slow and has many restrictions.

  • Bucket-based sharing (advanced)

    The operating system divides a shared storage area in the memory without specifying a data structure. The form and location of data storage are controlled by the process. This sharing method is fast.

2.1.6.4 Message passing
2.1.6.4.1 Introduction

Message passing refers to the exchange of data between processes in the unit of formatted information . If there is no shared space between communicating processes, the message passing method provided by the operating system can be used to achieve process communication.

Formatted information includes information header and information body .

  • Information header

    Including information such as sending process ID, receiving process ID, message length, etc.

  • information body

    What the process wants to exchange.

2.1.6.4.2 Features
  • Message passing relies on the exchange of data with two primitives provided by the operating system for sending messages and receiving messages .

    Send message primitive: send(pid,msg)

    Receive message primitive: receive(pid,&msg)

  • Because it hides communication implementation details and simplifies the design of communication programs (relying on two primitives), it is currently the most widely used inter-process communication mechanism.

2.1.6.4.3 Classification

Messaging can be further divided into two types: direct communication and indirect communication.

  • direct communication

    The sending process directly sends the message to the receiving process through the PID of the receiving process through the sending primitive, and hangs it on the message buffer queue of the receiving process, and the receiving process obtains information from the queue.

  • indirect communication method

    The sending process sends the message to an intermediate entity, and the receiving process obtains the information from the intermediate entity. This intermediate entity is generally called a "mailbox", so the indirect communication method is also called a mailbox communication method.

The popular understanding is that there are two people A and B. A wants to write a letter to B. A will hand the written letter to the courier. The first way is that the courier hands the letter directly to B's hand. The second way That is, the courier puts the letter into B's mailbox, and B picks up the letter from the mailbox.

2.1.6.5 Pipeline communication
2.1.6.5.1 Introduction

Pipe communication refers to the exchange of information between processes through special shared files such as pipes.

A pipe refers to a shared file (pipe file) used to connect a reading process and a writing process to achieve communication between them. The process that writes data to the file is called a writing process , and the process that reads data from the file is called a reading process .

Insert image description here

2.1.6.5.2 Features
  • The data in the pipeline adopts FIFO method.
  • Pipes can only use half-duplex communication, that is, only one-way transmission can be achieved within the same time period. If you want two-way transmission, you need two pipes.
  • The operating system controls the mutual exclusive access of various processes to the pipeline.
  • When the pipe is full, the writing process will block until the reading process removes the data from the pipe so that the pipe has free space.
  • When the pipe is empty, the reading process will block until the writing process writes data to the pipe so that the pipe is no longer empty.
  • Once the data in the pipe is read, it will disappear completely. Therefore, when multiple processes read data from the same pipe, confusion may occur. In this case, there are usually two solutions.
    1. A pipe allows multiple writing processes and one reading process.
    2. A pipe allows multiple writing processes and multiple reading processes, and the operating system will let each reading process take turns reading data from the pipe (Linux operating system solution)
2.1.6.5.3 Pipes in Linux

In Linux, pipes are a very frequently used communication mechanism. Pipes can overcome two of the following problems with using files for communication:

  1. Limit the size of the pipeline. In fact, a pipe is a fixed-size buffer. In Linux, the size of a pipe is 4KB, which makes its size unlike a file that can increase without checking. Using a fixed-size buffer will cause a problem. For example, when writing data, the pipe may be full. At this time, the write() call to the pipe will be blocked.
  2. The reading process may also work faster than the writing process. When all the data has been read, the pipe is empty, and the read() call to the pipe will be blocked.
2.1.6.5.4 Differences from shared storage
  • Mutually exclusive access to shared storage is implemented by the communication process, and mutually exclusive access to pipeline communication is implemented by the operating system.
  • In shared storage, processes are allowed to read/write data anywhere in the shared space. In pipeline communication, data is only allowed to be written from one side and read from the other side.
  • In shared storage, if a process wants to access the shared space, no other process must perform writing operations in the shared space, otherwise the access behavior will be blocked; in pipeline communication, as long as there is data in the pipeline, it can be accessed from the pipeline. Read data without worrying about being blocked because other processes are writing to the pipe.

Section summary

Insert image description here

2.2 Threads

2.2.1 Basic concepts and properties of threads

2.2.1.1 Introduction

Thread is a basic CPU execution unit and the smallest unit of program execution flow. It is composed of thread ID, program counter, register set and stack. It is an entity in the process and the basic unit that is independently scheduled and dispatched by the system. It can be understood as a "lightweight process".

2.2.1.2 Why should threads be introduced?

We learned earlier that the reason for introducing processes is to enable multi-programs to execute concurrently and improve resource utilization. A process may need to perform multiple tasks at the same time (for example, QQ needs to conduct video chats, transfer files, etc. at the same time). These functions cannot be solved by sequential processing of one process. At this time, the concept of threads is introduced, which further divides the process. A process can have multiple threads. These threads share the resources owned by the process. Each thread can be independently scheduled by the CPU. These threads can Responsible for different functions respectively . For example, in the above QQ example, it can be a QQ process, corresponding to two threads (video chat and file transfer).

It can be seen from the above that introducing threads can further improve the concurrency performance of the operating system. After the introduction of threads, the meaning of process has changed. At this time, the process is only used as the allocation unit of system resources other than the CPU, and the thread is used as the allocation unit of the CPU .

2.2.1.3 Main attributes of threads
  • A thread does not own system resources, it shares the resources of the process it belongs to.
  • Different threads can execute the same program.
  • Threads are the basic unit of CPU independent scheduling, and multiple threads can be executed concurrently.
  • After a thread is created, it begins its life cycle until it terminates. During the life cycle, the thread will experience various state changes such as blocked state, ready state, and running state.

Section summary

Insert image description here

2.2.2 Thread status

Very similar to a process, a thread also has five states: new state, ready state, running state, blocked state, and terminated state.

Since threads can be understood as further dividing processes, these five states and the transitions between the five states are basically the same as processes. For details, see Section 2.1.4

2.2.3 Thread organization and control

2.2.3.1 Thread control block

Similar to a process, the operating system is also equipped with a thread control block TCB for each thread, which is used to record information about controlling and managing threads.

The main contents of TCB are shown in the figure below.

Insert image description here

2.2.3.2 Thread organization

Similar to processes, the operating system divides TCB into different thread tables according to different states, and organizes threads through thread tables.

2.2.3.3 Thread control
  • Thread creation

    The operating system provides a system call to create a thread. When a user program starts, there is usually only one thread called the "initialization thread" being executed. Its main function is to create a new thread. When creating a new thread, you need to use Create a thread system call and provide the corresponding parameters.

  • Thread termination

    When a thread completes its task or the thread encounters an exception during running and is forcibly terminated, the terminating thread calls the corresponding terminating thread system call to terminate the thread.

    Note: After a thread is terminated, the resources it occupies will not be released immediately. The terminated thread will release the resources only after other threads in the process execute the detach function.

Section summary

Insert image description here

2.2.4 Comparison of threads and processes

  • Scheduling

    Before the introduction of threads, the process was the basic unit of resource allocation and scheduling. Each time scheduling required context switching, which was expensive.

    After the introduction of threads, the process is the basic unit of resource allocation other than the CPU. The thread is the basic unit of CPU scheduling. In the same process, the overhead of switching threads is very small and will not trigger process switching. However, when switching from a thread in one process to a thread in another process, the process switch will still be triggered.

  • Concurrency

    Before threads were introduced, processes could execute concurrently.

    After the introduction of threads, not only processes can be executed concurrently, but threads can also be executed concurrently, further improving the concurrency performance of the operating system.

  • Have resources

    The process is the basic unit of resource allocation in the system except the CPU. Threads do not own system resources. Threads can access the system resources of the process to which they belong.

  • independence

    Each process has an independent address space and resources. Threads between different processes are invisible to each other. Multiple threads of the same process share the resources of the process.

  • Multi-CPU system

    Before threads were introduced, a process could only run on one CPU, no matter how many CPUs there were.

    After introducing threads, multiple threads in the process can be assigned to different CPUs for execution.

  • Implementation

    Before threads are introduced, a process in the "execution state" means that the process is being executed by the CPU.

    After the thread is introduced, the process in the "execution state" means that a thread in the process is being executed by the CPU.

Section summary

Insert image description here

2.2.5 Thread implementation

The implementation of threads can be divided into two categories: user-level threads (ULT) and kernel-level threads (KLT) .

2.2.5.1 User-level threads
  • concept

    When using user-level threads, all work regarding thread management is implemented by the application through the thread library .

    A thread library refers to an API library provided by a programming language for programmers to create and manage threads. There are two main ways to implement a thread library:

    • Provide an API library without kernel support in user space. All codes and data structures in the API library are in user space. Calling functions in the library will only result in the call of a local function in user space.
    • Implement a kernel-level API library directly supported by the operating system. All codes and data structures in the API library are located in the kernel space. Calling functions in the library will result in system calls to the kernel.

    At this time, the operating system kernel cannot sense the existence of threads, only the existence of processes. That is, from the user's perspective, multiple threads are executing concurrently, but to the operating system kernel, only one process is running. The structure diagram of a system using user-level threads is shown in the figure below.

Insert image description here

Using an operating system with the above structure, the CPU scheduling unit is still a process (because the operating system kernel cannot sense user-level threads), and each process takes turns executing time slices. For example, process A in the system contains 1 user-level thread, and the process B contains 10 user threads. At this time, the running time of the threads in process A is ten times that of each thread in process B.

  • advantage

    1. Since the management and maintenance of user-level threads is implemented in the application program, switching between threads does not require conversion to the kernel state, saving the overhead of CPU switching states.
    2. The scheduling algorithm of each process can be dedicated, and different processes can choose different scheduling algorithms to schedule their own threads according to their own needs.
    3. The code related to the implementation and management of user-level threads in the application does not depend on the operating system platform.
  • shortcoming

    1. When any thread among the user-level threads of the same process encounters a system call-related blocking problem, it will affect other threads in the same process, causing other threads to also block.
    2. Unable to take advantage of multiple cores, the operating system kernel allocates only one CPU to a process at a time, so only one thread can execute in the process at the same time.
2.2.5.2 Kernel-level threads
  • concept

    When using kernel-level threads, all work related to thread management is implemented by the operating system kernel program . The operating system will set a TCB for each kernel-level thread to manage the kernel-level threads (in the previous chapter, we have introduced TCB related knowledge). The structure diagram of a system using kernel-level threads is shown in the figure below.

Insert image description here

Using the operating system with the above structure, the scheduling unit of the CPU is the kernel-level thread.

  • advantage

    1. It can take advantage of multi-core, and multiple threads of the same process can be scheduled and executed by different CPUs.
    2. If a thread in the same process is blocked, it will not affect other threads in the process, and the system will let other threads occupy the CPU.
    3. Kernel-level threads have small data structures and stacks, fast thread switching, and low overhead.
  • shortcoming

    Switching between user-level threads in the same process requires conversion from user mode to kernel mode, which requires a large system overhead.

2.2.5.3 Mixed forms
  • concept

    The hybrid form refers to a combination of user-level threads and kernel-level threads. In this implementation, the kernel supports the creation, scheduling, and management of multiple kernel-level threads, and also allows user programs to create, schedule, and manage user-level threads. One kernel-level thread can correspond to multiple user-level threads (these user-level threads time-division multiplex kernel-level threads). The structure diagram of the system using this implementation is shown in the figure below.

Insert image description here

Using the operating system with the above structure, the scheduling unit of the CPU is the kernel-level thread, but applications are allowed to manage user-level threads through the thread library.

  • Advantages and Disadvantages

    This approach combines the advantages of user-level threads and kernel-level threads.

Section summary

Insert image description here

2.2.6 Multi-threading model

When the operating system supports both user-level threads and kernel-level threads, that is, when the hybrid method introduced above is used, due to the different connection methods of user-level threads and kernel-level threads, it can be divided into three different multi-threading models. : One-to-one model , many-to-one model and many-to-many model .

2.2.6.1 One-to-one model
  • concept

    Each user-level thread mapping is mapped to a different kernel-level thread . The structure diagram is shown in the figure below.

Insert image description here

As can be seen from the above figure, this multi-threading model is actually the thread implementation method we introduced above when enabling kernel-level threads.

  • advantage

    When a thread is blocked, the CPU is allowed to schedule another thread to run, with strong concurrency capabilities.

  • shortcoming

    Every time a user thread is created, a corresponding kernel thread needs to be created, which requires a large system overhead.

2.2.6.2 Many-to-one model
  • concept

    Map multiple user-level threads to one kernel-level thread . These user threads belong to one process. Thread scheduling and management are completed in user space. Only when user-level threads need to access the kernel, they are mapped to one kernel-level thread. , and only one thread is allowed to map at a time. The structure diagram is shown in the figure below.

Insert image description here

As can be seen from the above figure, this multi-threading model is actually the implementation of pure user-level threads we introduced above.

  • advantage

    Thread management is performed in user space and is highly efficient.

  • shortcoming

    1. If a thread blocks while accessing the kernel, the entire process will be blocked.
    2. Only one thread can access the kernel at the same time.
    3. Multiple threads cannot be scheduled and executed by multiple CPUs at the same time.
2.2.6.3 Many-to-many model
  • concept

    Map n user-level threads to m kernel-level threads (n is greater than or equal to m) . The structure diagram is as shown in the figure below.

Insert image description here

As can be seen from the above figure, this multi-threading model is actually the hybrid implementation method we introduced above.

Under this multi-threading model, user-level threads can be understood as the carrier of "code logic", and kernel-level threads can be understood as the carrier of "running opportunities", that is, kernel-level threads are the unit of CPU scheduling. Kernel-level threads can run code in any mapped user-level thread.

  • Features

    It not only overcomes the shortcomings of low concurrency of the many-to-one model, but also overcomes the disadvantage of too much overhead caused by one user process occupying too many kernel-level threads in the one-to-one model.

Section summary

Insert image description here

2.3 CPU scheduling

2.3.1 Scheduling related concepts

2.3.1.1 What is scheduling

When there are multiple things to be processed at the same time, due to limited resources, these things cannot be processed at the same time. At this time, you need to decide the order in which these things should be processed according to certain rules. This is "scheduling " .

In a multiprogramming system, since the number of processes is often greater than the number of CPUs, there will be multiple processes competing for the CPU, and it is very common. At this time, you need to select a certain process from the ready queue according to certain rules. process and allocates the CPU to the process so that the process can execute.

2.3.1.2 Seven-state model

In section 2.1.4, we learned about the five states of the process. At this time, in order to learn CPU scheduling, we need to additionally learn a special state of the process, the suspension state (suspension state) . The suspension state can be further studied. It is subdivided into two states : ready suspension and blocking suspension , so the five-state model of the process can be further extended into a seven-state model .

So when will a suspended process appear?

Due to limited memory, the data of some processes needs to be temporarily transferred to external memory. When these processes need to run and the memory is free, the data will be transferred back into the memory. The status of these processes that are temporarily saved to external memory and waiting is the suspended state. . The PCBs of processes in a suspended state are organized into suspend queues

The transition diagram of the seven-state model is shown below

Insert image description here

Comparing blocking and suspending

The same thing: they are all in a service where CPU is temporarily unavailable.

The difference: The process image in the blocked state is still in the memory, while the process image in the suspended state is in external memory.

2.3.1.3 Three-level scheduling

Scheduling is divided into three levels in the operating system, from high to low: high-level scheduling -> mid-level scheduling -> low-level scheduling.

  • Advanced scheduling

    Advanced scheduling, also known as job scheduling , refers to selecting a job from the external job backup queue and transferring it into the memory according to certain principles, and creating a corresponding process so that it can compete for the CPU. Each job is only loaded once and loaded once . A PCB is created for a job when it is called in, and its PCB is destroyed when it is called out.

    A job refers to a specific task. Submitting a job to the system means that the user asks the operating system to start a program (to handle a specific task).

    The memory space is limited, so when the memory space is full and another user submits a job to the operating system, these jobs will be placed in the job backup queue outside the memory .

  • Intermediate Scheduling

    Intermediate scheduling, also known as memory scheduling , refers to deciding which suspended process will be read back into the memory according to certain rules. A process may be called out and brought into the memory multiple times . The frequency of memory scheduling is higher than that of job scheduling.

  • low level scheduling

    Low-level scheduling, also known as process scheduling , refers to selecting a process from the ready queue according to certain rules and allocating the CPU to the process.

    Process scheduling is the most basic kind of scheduling in the operating system , with a very high frequency, usually once every tens of milliseconds.

2.3.1.4 Connection and comparison of three-level scheduling
Things to do The scene that happened frequency Impact on process status
Advanced scheduling According to certain rules, select the appropriate job from the backup queue, transfer it into the memory, and create a process External storage->Memory (job-oriented) lowest None->Created state->Ready state
Intermediate Scheduling According to certain rules, select the appropriate process from the suspension queue and transfer its data back to the memory External memory -> memory (process-oriented) medium Ready suspension -> ready state (blocking suspension -> blocking state)
low level scheduling According to certain rules, select a process from the ready queue to allocate CPU to it Memory->CPU Highest Ready state->Running state

Section summary

Insert image description here

2.3.2 Implementation of scheduling

2.3.2.1 Scheduler and idle process
  • scheduler

    In the operating system, the CPU is scheduled and allocated through a scheduler. The scheduler usually consists of three parts: a queue, a dispatcher, and a context switcher.

    • queuer

      Used to queue all ready processes in the operating system into one or more queues according to certain rules to facilitate selection by the scheduler.

    • Dispatcher

      According to the process selected by the scheduler, it is called out from the ready queue and the CPU is allocated to it.

    • context switcher

      When the CPU switches processes, the running environment of the two processes is protected and restored through the context switcher.

      Two pairs of context (running environment) switching operations occur in the context switcher.

      1. Save the running environment of the current process to the corresponding PCB, and then load the running environment of the dispatch program so that the dispatch program can run.
      2. Remove the dispatcher's running environment and load the new process's running environment into each register of the CPU.

    The structure diagram of the scheduler is shown below

Insert image description here

  • Hanging out process

    When there is no ready process in the system, the CPU will execute the idle process. If there are no other ready processes, the CPU will always execute the idle process and test the interruption when executing the idle process.

    It can be understood that when the CPU is idle, it will continuously patrol by executing the idle process.

    The priority of the idle process is the lowest. Once a ready process exists, it will give up the CPU immediately.

2.3.2.2 Scheduling and switching timing

In Section 2.3.1.3, we introduced the related concepts of process scheduling, so when will process scheduling and switching occur?

First, in Section 2.3.2.1, we introduced the scheduler. It can be seen that process scheduling is completed through the scheduler, which is the operating system kernel program. When the event requesting scheduling occurs, the scheduler can be run, and the process switching will not occur until a new ready process is scheduled. However, not in all cases, process scheduling and switching will definitely occur after scheduling is requested.

  • Situations where process scheduling and switching can be performed

    1. The current process actively gives up the CPU
      • When the process terminates normally, process scheduling and switching will occur
      • During the running of the process, when an exception occurs and terminates, process scheduling and switching will occur.
      • When a process actively requests blocking (requesting I/O, etc.), process scheduling and switching will occur.
    2. The current process passively gives up the CPU
      • When the time slice allocated to the current process is used up, process scheduling and switching will occur.
      • When there is a process with a higher priority to run, process scheduling and switching will occur.
      • When there are more urgent things to deal with (such as I/O interruptions), process scheduling and switching will occur.
  • Situations where process scheduling and switching are not possible

    1. During the process of handling the interrupt , process scheduling and switching cannot be performed. Because the interrupt handling process is very complex, it is difficult to switch processes in implementation.

    2. The process is in the critical section of the operating system kernel and cannot be scheduled and switched. When a process enters the critical section of the operating system kernel, the critical section needs to be locked and other processes are not allowed to access it. You should not switch to other processes before unlocking to avoid the critical section being unable to be unlocked.

      Critical resources: Resources that only one process is allowed to use within a period of time. Each process must have mutual exclusive access to critical resources.

      Critical section: A code fragment that accesses critical resources.

      Kernel critical section: Generally used to access certain operating system kernel data structures, such as the ready queue of a process, etc. Therefore, the kernel critical section must release the lock as soon as possible, otherwise it may affect other management tasks of the operating system kernel.

      Based on the above introduction, the process cannot perform process scheduling and switching in the critical section of the operating system kernel, but when the process is in the ordinary critical section, it can perform process scheduling and switching.

    3. When performing atomic (primitive) operations , process scheduling and switching cannot be performed. When performing atomic operations, even interrupts must be masked, and process scheduling and switching should not be performed.

2.3.2.3 Classification of scheduling methods

The process scheduling method refers to the way in which the CPU is allocated when a process is executing on the CPU and there is a more important process that needs to be processed. Generally, there are two process scheduling methods: non-preemptive scheduling and preemptive scheduling .

  • non-preemptive scheduling

    Only processes are allowed to voluntarily give up the CPU , that is, when a process is executed on the CPU, even if a process with a higher priority enters the ready queue, the executing process will still be allowed to continue executing until the process voluntarily gives up the CPU.

    The advantages of non-preemptive scheduling are simple implementation and low system overhead. It is suitable for batch processing systems, but is not suitable for time-sharing systems and real-time systems .

  • Preemptive scheduling

    Allow the process to passively give up the CPU , that is, when a process is executed on the CPU, if a process with a higher priority enters the ready queue, the executing process will be immediately suspended, converted to the ready state, and the CPU will be allocated to Ready processes with higher priority.

    The advantage of preemptive scheduling is that it can improve system throughput and response efficiency, and is suitable for time-sharing systems and real-time systems .

2.3.2.4 Thread Scheduling

There are two types of thread scheduling in the operating system, which are user-level thread scheduling and kernel-level thread scheduling.

  • User-level thread scheduling

    Since the kernel does not know the existence of user-level threads, at this time, the unit of kernel scheduling is still the process.

  • Kernel-level thread scheduling

    Since the kernel knows the existence of kernel-level threads, at this time, the kernel will select a specific thread to run and allocate a time slice to it. If the time slice ends, the thread will be forced to suspend.

Section summary

Insert image description here

2.3.3 Scheduling algorithm

2.3.3.1 Evaluation indicators of scheduling algorithms

Just like everyone has different advantages, different scheduling algorithms also have different characteristics. In order to evaluate the performance of scheduling algorithms, people have proposed many evaluation criteria. The main evaluation criteria are: CPU utilization , system throughput , turnaround time , waiting time and response time .

  • CPU utilization

    CPU utilization refers to the proportion of time the CPU is busy.

    CPU utilization = CPU effective working time/(CPU effective working time + CPU idle waiting time)

  • System throughput

    System throughput refers to the number of jobs completed by the CPU per unit time.

  • Turnaround time

    Turnaround time refers to the time it takes for a job to be submitted to the system until it is completed, that is, the sum of time spent waiting for the job, queuing in the ready queue, running on the CPU, and I/O operations.

    Turnaround time = job completion time - job submission time

    For the operating system, it is more concerned about the overall job turnover performance, so another evaluation criterion has been extended - average turnover time. The average turnover time refers to the average of multiple job turnover times.

    Average turnaround time = sum of turnaround times for each job/number of jobs

    For different jobs, the larger job turnaround time is generally longer, and the smaller job turnaround time is generally shorter. Therefore, an evaluation index is extended - weighted turnaround time. Weighted turnaround time refers to the job turnover time and job turnover time. Ratio of actual running time.

    Weighted turnaround time = job turnaround time/job actual running time

    Based on the weighted turnaround time, another evaluation criterion is naturally extended - the average weighted turnaround time.

    Average weighted turnaround time = weighted turnaround time of each job/number of jobs

  • waiting time

    The waiting time refers to the sum of the time the process waits to be served after it is established.

    Note: The waiting time does not include the time waiting for the I/O device operation to complete, because during this time, the process is being served by the I/O device.
    Waiting time = turnaround time - running time

  • Response time

    Response time refers to the time it takes from the user submitting a request to the system until the first response is generated. The shorter the response time, the better the user experience.

Section summary

Insert image description here

2.3.3.2 First come, first served scheduling algorithm
  • Scope of application

    Can be used for job scheduling and process scheduling

  • Algorithm introduction

    Similar to queuing up to buy things in daily life, the first come first served (FCFS) scheduling algorithm provides services according to the order in which jobs or processes arrive .

    In job scheduling, FCFS will select one or several jobs that enter the queue first from the backup job queue, and transfer them into memory for running.

    In process scheduling, FCFS will select the first process to enter the queue from the ready queue, allocate CPU to it, and let it run.

  • Is it preemptible?

    FCFS is a non-preemptive scheduling algorithm

  • case analysis

    Assuming that there are 4 processes in the system (pure computing processes, that is, processes without I/O operations), their arrival and estimated running time are as shown in the table below.

    process Time of arrival Estimated run time
    P1 0 8
    P2 4 6
    P3 5 2
    P4 6 6

    Using the FCFS scheduling algorithm, the scheduling process of each of the above processes is P1->P2->P3->P4 . According to the calculation rules of each indicator in section 2.3.3.1, the indicators of each of the above processes can be calculated as shown in the table below.

    process Complete time Turnaround time Rights turnaround time waiting time
    P1 0+8=8 8-0=8 8/8=1 8-8=0
    P2 6+8=14 14-4=10 10/6=1.67 10-6=4
    P3 14+2=16 16-5=11 11/2=5.5 11-2=9
    P4 16+6=22 22-6=16 16/6=2.67 16-6=10

    Average turnaround time=(8+10+11+16)/4=11.25

    Average weighted turnaround time = (1+1.67+5.5+2.67)/4=2.71

    Average waiting time=(0+4+9+10)/4=5.75

    It can be seen that the value of the privileged turnaround time of process P3 is 5.5, which is much higher than the other three processes. Therefore, it means that the process P3 obviously only needs to run for a short time, but it has to wait for a long time. long.

  • Advantages and Disadvantages

    • Advantages: fair, simple algorithm
    • Disadvantages: Short jobs ranked behind long jobs (processes) need to wait for a long time, and the turnaround time is very long, which is very unfriendly to short jobs, that is, it is good for long jobs but not good for short jobs.
  • Will it lead to starvation (jobs/processes not being served for a long time)

    Since the jobs/processes queued in the queue will be scheduled in order, starvation will not occur.

2.3.3.3 Short job priority scheduling algorithm
  • Scope of application

    Can be used for job scheduling and process scheduling

  • Algorithm introduction

    Based on the shortcomings of the FCFS scheduling algorithm, people have proposed short job first (SJF) and short process first (SPF) scheduling algorithms, that is, priority scheduling for short jobs (processes).

    SJF will select one or several jobs with the shortest estimated running time from the backup queue and transfer them into memory for running.

    SPF will select a process from the ready queue with the shortest estimated running time, allocate CPU to it, and let it run.

    There is also a short job priority scheduling algorithm called the shortest remaining time priority algorithm SRTN. This algorithm needs to be scheduled whenever a process joins the ready queue or the process is completed . If the remaining time of the newly arrived process is shorter than the remaining time of the currently running process, , the new process will seize the CPU and change the currently running process to the ready state.

  • Is it preemptible?

    SJF and SPF are non-preemptive algorithms, and SRTN is a preemptive algorithm.

  • case analysis

    Assuming that there are 4 processes in the system (pure computing processes, that is, processes without I/O operations), their arrival and estimated running time are as shown in the table below.

    process Time of arrival Estimated run time
    P1 0 8
    P2 4 6
    P3 5 2
    P4 6 6

    Using the SPF scheduling algorithm, the scheduling process of each of the above processes is P1->P3->P2->P4 . According to the calculation rules of each indicator in section 2.3.3.1, the various indicators of each of the above processes can be calculated as shown in the table below.

    process Complete time Turnaround time Rights turnaround time waiting time
    P1 0+8=8 8-0=8 8/8=1 8-8=0
    P2 10+6=16 16-4=12 12/6=2 12-6=6
    P3 8+2=10 10-5=5 5/2=2.5 5-2=3
    P4 16+6=22 22-6=16 16/6=2.67 16-6=10

    Average turnaround time=(8+12+5+16)/4=10.25

    Average weighting turnaround time = (1+1.67+5.5+2.67)/4=2.0425

    Average waiting time=(0+4+9+10)/4=4.75

    Using the SRTN scheduling algorithm, the scheduling process of each of the above processes is as follows (Pn(m) represents the remaining time m of the current process Pn):

    Time 0 (P1 arrives): P1(8)

    Time 4 (P2 arrives): P1(4) , P2(6)

    Time 5 (P3 arrival): P1(3), P2(6), P3(2)

    Time 6 (P4 arrives): P1(3), P2(6), P3(1) , P4(6)

    7 moments (P3 completed): P1(3) , P2(6), P4(6)

    Time 10 (P1 completed): P2(6) , P4(6)

    Time 16 (P2 completed): P4(6)

    Time 22 (P4 completed), end

    According to the calculation rules for each indicator in Section 2.3.3.1, the indicators for each of the above processes can be calculated as shown in the table below.

    process Complete time Turnaround time Rights turnaround time waiting time
    P1 10 10-0=10 10/8=1.25 10-8=2
    P2 16 16-4=12 12/6=2 12-6=6
    P3 7 7-5=2 2/2=1 2-2=0
    P4 22 22-6=16 16/6=2.67 16-6=10

    Average turnaround time=(10+12+2+16)/4=10

    Average weighted turnaround time = (1.25+2+1+2.67)/4=1.73

    Average waiting time = (2+6+0+10)/4=4.5

    Comparing the average turnover, average weighted turnover, and average waiting time of FCFS, SPF, and SRTN algorithms, it can be seen that the time spent is FCFS>SPF>SRTN.

  • Advantages and Disadvantages

    • advantage

      "Shortest" average waiting time, average turnaround time

    • shortcoming

      This algorithm is detrimental to long jobs and beneficial to short jobs. If short jobs continue to arrive, long jobs may not be scheduled for a long time.

      Since the estimated running time of a job is provided by the user, the estimated running time does not represent the real running time, so this algorithm may not necessarily achieve priority scheduling of short jobs.

  • Will it lead to starvation (jobs/processes not being served for a long time)

    Can lead to long job starvation.

2.3.3.4 High response ratio scheduling algorithm
  • Scope of application

    Suitable for job scheduling and process scheduling

  • Algorithm introduction

    It balances the advantages and disadvantages of FCFS and SJF scheduling algorithms, and takes into account the waiting time and estimated running time of each job. When scheduling each job, first calculate the response ratio of each job in the backup job queue, and select The job with the highest response ratio runs.

    Response ratio = (waiting time + required service time) / required service time

  • Is it preemptible?

    Non-preemptive algorithm

  • case analysis

    Assuming that there are 4 processes in the system (pure computing processes, that is, processes without I/O operations), their arrival and estimated running time are as shown in the table below.

    process Time of arrival Estimated run time
    P1 0 8
    P2 4 6
    P3 5 2
    P4 6 6

    Using the high response ratio priority scheduling algorithm, the scheduling process of each process mentioned above is as follows (Pn(m) indicates that the response ratio of the current process Pn is m):

    At time 0, only P1 arrives in the ready queue and P1 runs

    At 8 o'clock, P1 finishes running and actively gives up the CPU. At this time, there are P2 (1.67), P3 (2.5), P4 (1.33), and P3 running in the ready queue.

    At 10 o'clock, P3 completes the operation and actively gives up the CPU. At this time, there are P2(2), P4(1.67), and P2 running in the ready queue.

    At 16 o'clock, P2 completes the operation and actively gives up the CPU. At this time, there is only P4 in the ready queue and P4 is running.

    At 22:00, P4 operation is completed and ends.

  • Advantages and Disadvantages

    A compromise between the FCFS algorithm and the SJF algorithm takes into account the waiting time and running time.

  • Will it lead to starvation (jobs/processes not being served for a long time)

    Does not cause hunger

2.3.3.5 Time slice rotation scheduling algorithm
  • Scope of application

    used for process scheduling

  • Algorithm introduction

    The time slice rotation scheduling algorithm is mainly used in time-sharing operating systems . The system will arrange all ready processes into a ready queue according to the FCFS policy. The scheduler always chooses the first process in the ready queue to execute, but this process can only execute one Fixed time slice. When the time slice ends and the process has not finished executing, the process will be deprived of the CPU usage rights and the process will be re-queued at the end of the ready queue.

  • Is it preemptible?

    It can be preempted, and the clock device issues a clock interrupt to notify the CPU that the time slice has arrived.

  • case analysis

    Assuming that there are 4 processes in the system (pure computing processes, that is, processes without I/O operations), their arrival and estimated running time are as shown in the table below.

    process Time of arrival Estimated run time
    P1 0 8
    P2 4 6
    P3 5 2
    P4 6 6

    使用时间片轮转调度算法进行调度,按照时间片大小分别为2和8来进行分析。

    时间片大小为2时,调度过程如下(Pn(m)表示进程Pn的剩余运行时间m就绪队列(队头…队尾)):

    时刻 就绪队列中的进程情况 CPU分配情况
    0 P1(8) P1
    2 P1(6) P1
    4 P2(6),P1(4) P2
    5 P1(4),P3(2) P2
    6 P1(4),P3(2),P4(6),P2(4) P1
    8 P3(2),P4(6),P2(4),P1(2) P3
    10 P4(6),P2(4),P1(2) P4
    12 P2(4),P1(2),P4(4) P2
    14 P1(2),P4(4),P2(2) P1
    16 P4(4),P2(2) P4
    18 P2(2),P4(2) P2
    20 P4(2) P4
    22 空闲 空闲

    上面的表格的文字描述版本:

    0时刻,就绪队列中的进程情况为(P1(8)),此时让P1运行。

    2时刻,时间片用完,此时就绪队列中的进程情况为(P1(6)),此时继续让P1运行。

    4时刻,P2到达,且时间片刚好用完,此时就绪队列中的进程情况为(P2(6),P1(4)),此时让P2运行。

    5时刻,P3到达,但是时间片没有用完,此时就绪队列中的进程情况为(P1(4),P3(2)),此时P2继续运行。

    6时刻,P4到达,且时间片刚好用完,此时就绪队列中的进程情况为(P1(4),P3(2),P4(6),P2(4)),此时让P1运行。

    8时刻,时间片用完,此时就绪队列中的进程情况为(P3(2),P4(6),P2(4),P1(2)),此时让P3运行。

    10时刻,P3执行结束,且时间片用完,此时就绪队列中的进程情况为(P4(6),P2(4),P1(2)),此时让P4运行。

    12时刻,时间片用完,此时就绪队列中的进程情况为(P2(4),P1(2),P4(4)),此时让P2运行。

    14时刻,时间片用完,此时就绪队列中的进程情况为(P1(2),P4(4),P2(2)),此时让P1运行。

    16时刻,P1执行结束,且时间片用完,此时就绪队列中的进程情况为(P4(4),P2(2)),此时让P4运行。

    18时刻,时间片用完,此时就绪队列中的进程情况为(P2(2),P4(2)),此时让P2运行。

    20时刻,P2执行结束,且时间片用完,此时就绪队列中的进程情况为(P4(2)),此时让P4运行。

    22时刻,P4执行结束,至此所有进程执行完毕。

    时间片大小为8时,调度过程如下(Pn(m)表示进程Pn的剩余运行时间m就绪队列(队头…队尾)):

    时刻 就绪队列中的进程情况 CPU分配情况
    0 P1(8) P1
    4 P2(6) P1
    5 P2(6),P3(2) P1
    6 P2(6),P3(2),P4(6) P1
    8 P2(6),P3(2),P4(6) P2
    14 P3(2),P4(6) P3
    16 P4(6) P4
    22 空闲

    上面的表格的文字描述版本:

    0时刻,就绪队列中的进程情况为(P1(8)),此时让P1运行。

    4时刻,P2到达,时间片没有用完,此时就绪队列中的进程情况为(P2(6)),此时继续让P1运行。

    5时刻,P3到达,时间片没有用完,此时就绪队列中的进程情况为(P2(6),P3(2)),此时继续让P1运行。

    6时刻,P4到达,时间片没有用完,此时就绪队列中的进程情况为(P2(6),P3(2),P4(6)),此时继续让P1运行。

    8时刻,P1执行结束,且时间片用完,此时就绪队列中的进程情况为(P2(6),P3(2),P4(6)),此时让P2运行。

    14时刻,P2执行结束,此时就绪队列中的进程情况为(P3(2),P4(6)),此时让P3运行。

    16时刻,P3执行结束,此时就绪队列中的进程情况为(P4(6)),此时让P4运行。

    22时刻,P4执行结束,至此所有进程执行完毕。

    当时间片为8时,可以看出这四个进程的执行顺序为P1->P2->P3->P4,此种情况与使用FCFS调度算法时的结果是一致的。

    由此,可以知道如果时间片太大,使得每个进程都可以在一个时间片内完成,则时间片轮转调度算法会退化为FCFS调度算法,并且会增大进程响应时间。因此时间片不能太大

    所谓的增大进程响应时间,在此举例说明,若系统中有10个进程在并发执行,且时间片为1秒,则一个进程被响应可能需要等待9秒,即,如果用户在自己的进程的时间片外通过键盘发出调试命令,可能需要等待9秒才能被系统响应,这种用户体验就是极其糟糕的了。

    与此同时,由之前的章节我们已经了解到,进程调度与切换是有时间代价的,因此如果时间片太小,进程调度切换太过于频繁,系统就会花费大量的时间来处理进程切换,就会导致实际用于处理用户进程的时间比例减少,因此时间片也不能太小

    一般来说,时间片要让切换进程的开销占比不超过1%

  • 优缺点

    • 优点

      公平、响应快,适用于分时系统

    • 缺点

      由于高频率的切换进程,因此有一定的系统开销

      无法区分任务的紧急程度

  • 是否会导致饥饿

    不会

2.3.3.6 优先级调度算法
  • 适用范围

    适用于作业调度以及进程调度。

  • 算法介绍

    优先级调度算法中的优先级用于描述作业/进程的紧急程度。

    在作业调度中,每次从后备作业队列中选择优先级最高的一个或几个作业,将它们调入内存。

    在进程调度中,每次从就绪队列中选择优先级最高的一个或几个进程,为其分配CPU。

  • 是否可抢占

    既有抢占式的也有非抢占式的。

    抢占式优先级调度算法,当一个进程正在运行时,若有优先级更高的进程进入就绪队列,则将当前进程暂停,将CPU分配给优先级更高的进程。

    非抢占式优先级调度算法,当一个进程正在运行时,即使有优先级更高的进程进入就绪队列,依旧让当前进程继续运行,直至由于当前进程自身原因让出CPU后,才会将CPU分配给优先级更高的进程。

  • 案例分析

    假设系统中有4个进程(纯计算进程,即没有I/O操作的进程),它们的到达时间、预估运行时间以及优先级如下表所示。

    进程 到达时间 预估运行时间 优先级
    P1 0 8 3
    P2 4 6 2
    P3 5 2 1
    P4 6 6 2

    使用非抢占式优先级调度算法时,运行情况如下:

    0时刻,只有P1到达,此时运行P1

    8时刻,P1运行完毕,此时就绪队列中有P2、P3、P4。P3优先级最高,让P3运行。

    10时刻,P3运行完毕,此时就绪队列中有P2、P4,此时虽然P2和P4的优先级一致,但是P2比P4更早进入就绪队列,因此让P2运行。

    16时刻,P2运行完毕,此时就绪队列只有P4,让P4运行。

    22时刻,P4运行完毕,所有进程执行结束。

    使用抢占式优先级调度算法时,运行情况如下(Pn(m)表示进程Pn的剩余运行时间m):

    0时刻,只有P1(8)到达,此时运行P1

    4时刻,P2(6)到达,且优先级比P1高,此时就绪队列中进程情况为(P2(6))此时让P2运行,将P1重新放回就绪队列。

    5时刻,P3到达,此时就绪队列中进程情况为(P1(4)、P3(2)),由于P3的优先级比P2的优先级高,因此让P3运行。

    6时刻,P4到达,此时就绪队列中进程情况为(P1(4),P2(5),P4(6)),由于此时P3的优先级依旧是最高的,因此继续让P3运行。

    7时刻,P3运行结束,此时就绪队列中进程情况为(P1(4),P2(5),P4(6)),由于此时P2与P4的优先级相同,并且比P1高,并且P2先到达就绪队列,因此让P2运行。

    12时刻,P2运行结束,此时就绪队列中进程情况为(P1(4),P4(6)),由于此时P4的优先级最高,让P4运行。

    18时刻,P4运行结束,此时就绪队列中进程情况为(P1(4)),让P1运行。

    22时刻,P1运行结束,所有进程执行结束。

  • 优缺点

    • 优点

      用优先级区分紧急程度,适用于实时操作系统。

    • 缺点

      若不断地有高优先级的进程到来,可能会导致饥饿

  • 是否会导致饥饿

    可能会导致饥饿

  • 优先级

    • 优先级分类

      优先级可分为两种:动态优先级、静态优先级。

      动态优先级是指在进程运行过程中,根据进程情况的变化动态调整优先级,主要依据有CPU的占用时长,就绪进程等待CPU的时长等。

      静态优先级是指在创建进程时就确定了,且在进程的整个运行期间保持不变,主要依据有进程类型、进程对资源的要求、用户要求等。

    • 优先级设置的原则

      1. 系统进程>用户进程,系统进程作为系统的管理者,应该拥有更高的优先级
      2. 交互型进程>非交互型进程(前台进程>后台进程)
      3. I/O型进程>计算机型进程,I/O型进程是指会频繁使用I/O设备的进程,计算机型进程是指会频繁使用CPU的进程(很少使用I/O设备),由于I/O设备可以与CPU并行运行,因此若将I/O型进程的优先级设置得更高,就可以使I/O设备更多地和CPU并行,提高系统的整体效率。
2.3.3.7 多级反馈队列调度算法
  • 适用范围

    用于进程调度

  • 算法介绍

    多级反馈队列调度算法是时间片轮转调度算法和优先级调度算法的综合与发展,在该算法中,通过动态地调整进程优先级和时间片的大小,来达到兼顾多个方面的系统目标。

    具体思想:

    1. 设置多个就绪队列,为每个队列赋予不同的优先级,第1级队列的优先级最高,第2级的队列优先级次之,以此类推至后续队列。

    2. 为每个队列的进程赋予不同大小的时间片,优先级越高的队列,时间片越小。

    3. 在每个队列中采用FCFS调度算法,当新进程进入内存后,首先将它放入第1级队列的末尾,按照FCFS原则等待调度。当轮到该进程执行时,如果它能够在给予的时间片内完成,则该进程就正常的执行完毕,否则,调度程序会将它转入第2级队列的末尾继续按照FCFS原则等待调度,若它在第2级队列的时间片内依旧未完成,就会被转入第3级队列,以此类推,当该进程被转入最后的第n级队列后,在第n级队列中便采用时间片轮转调度算法进行调度(因为此时已经没有更低优先级的队列了)。

    4. 按队列的优先级进行调度,只有第1级队列为空时,才会调度第2级队列中的进程,即,仅当第k级队列为空时,才会调度第k+1级队列中的进程。如果正在执行第k级队列中的某个进程时,系统有一个新的进程进入了更高优先级的队列时,就会立即把正在运行的进程放回到第k级队列中,把CPU分配给新进入的进程。

      具体规则稍微有些复杂,各位同学最好结合后面的案例分析来进行理解。

  • 是否可抢占

    是抢占式算法

  • 案例分析

    假设系统中有4个进程(纯计算进程,即没有I/O操作的进程),它们的到达以及预估运行时间如下表所示。

    进程 到达时间 预估运行时间
    P1 0 8
    P2 4 6
    P3 5 2
    P4 6 6

    并且,在系统中设置了三个优先级队列,分别是第1级队列、第2级队列、第3级队列,其时间片大小分别是1、2、4。

    使用多级反馈队列调度算法后,这些进程的执行情况如下(Pn(m)表示进程Pn的剩余运行时间m):

    0时刻,P1(8)进入第1级队列,执行P1。

    此时队列中的进程情况为:第1级队列[P1(8)],第2、3级队列为空。

    1时刻,P1(7)时间片用完,此时P1会进入第2级队列的队尾,此时由于第1级队列中没有任何进程,系统继续执行P1。

    此时队列中的进程情况为:第1、3级队列为空,第2级队列[P1(7)]。

    3时刻,P1(5)时间片用完,此时P1会进入第3级队列的队尾,此时由于第1、2级队列中没有任何进程,系统继续执行P1。

    此时队列中的进程情况为:第1、2级队列为空,第3级队列[P1(5)]。

    4时刻,P1(4)时间片没有用完,但是,这是P2(4)进入第1级队列,因此P1被重置到第3级队列队尾,系统执行P2。

    此时队列中的进程情况为:第1级队列[P2(4)],第2级队列为空,第3级队列[P1(4)]。

    5时刻,P2(5)时间片用完,且P3(2)进入第1级队列,此时P2进入第2级队列,执行P3。

    此时队列中的进程情况为:第1级队列[P3(2)],第2级队列[P2(5)],第3级队列[P1(4)]。

    6时刻,P3(1)时间片用完,且P4(6)进入第1级队列,此时P3进入第2级队列队尾,执行P4。

    此时队列中的进程情况为:第1级队列[P4(6)],第2级队列[P2(5),P3(1)],第3级队列[P1(4)]。

    7时刻,P4(5)时间片用完,进入第2级队列队尾,此时第1级队列为空,第2级队列的队头为P2,系统执行P2。

    此时队列中的进程情况为:第1级队列为空,第2级队列[P2(5),P3(1),P4(5)],第3级队列[P1(4)]。

    9时刻,P2(3)时间片用完,进入第3级队列队尾,此时第1级队列为空,第2级队列的队头为P3,系统执行P3。

    此时队列中的进程情况为:第1级队列为空,第2级队列[P3(1),P4(5)],第3级队列[P1(4),P2(3)]。

    10时刻,P3执行完毕,此时第1级队列为空,第2级队列的队头为P4,系统执行P4。

    此时队列中的进程情况为:第1级队列为空,第2级队列[P4(5)],第3级队列[P1(4),P2(3)]。

    12时刻,P4(3)时间片用完,进入第3级队列队尾,此时第1、2级队列为空,第3级队列队头为P1,系统执行P1。

    此时队列中的进程情况为:第1、2级队列为空,第3级队列[P1(4),P2(3),P4(3)]。

    16时刻,P1执行完毕,此时第1、2级队列为空,第3级队列队头为P2,系统执行P2。

    此时队列中的进程情况为:第1、2级队列为空,第3级队列[P2(3),P4(3)]。

    At 19:00, P2 is executed. At this time, the first and second level queues are empty, the head of the third level queue is P4, and the system executes P4.

    At this time, the status of the processes in the queue is: the first and second level queues are empty, and the third level queue [P4(3)].

    At 22:00, P4 is completed and all processes are executed.

  • Advantages and Disadvantages

    • advantage

      • Relatively fair to all types of processes (advantages of first-come-first-serve scheduling algorithm)
      • Each newly arrived process can get a response quickly (the advantage of the time slice round-robin scheduling algorithm)
      • Short processes can be completed in less time (the advantage of short process priority scheduling algorithm)
    • shortcoming

      Algorithm implementation is more complex

  • Will it cause hunger

    Will cause starvation (if short processes arrive all the time, it will lead to starvation of long processes in the low-priority queue)

2.3.3.8 Comparison and summary of various scheduling algorithms
Algorithm name Is it preemptible? advantage shortcoming Scope of application Default decision mode
First come, first served Non-preemptive Fair and simple to implement Not conducive to short work batch processing system Non-preemptive
Short jobs first Both preemptive and non-preemptive The average waiting time is the least and the efficiency is the highest (the shortest remaining time priority algorithm is included by default here) Long jobs will be starved, and the estimated running time is difficult to determine. Job scheduling, batch processing system Non-preemptive
High response ratio priority Non-preemptive Balance long and short assignments Computing the response ratio is expensive time sharing system Non-preemptive
time slice rotation Preemptive Fair, fast response, suitable for time-sharing system Frequent switching of processes will cause additional overhead time sharing system Preemptive
priority scheduling algorithm Both preemptive and non-preemptive Use priority to distinguish urgency, suitable for real-time operating systems. may cause hunger real time system Non-preemptive
Multi-level feedback queue Preemptive (not necessarily within each queue) Taking into account both long and short tasks, with good response time and strong feasibility The algorithm is more complex Universal Preemptive

Section summary

Insert image description here
To be continued…

references

"2023 Operating System Postgraduate Entrance Examination Review Guide"-Wangdao Forum

If this article is helpful to you, you may wish to like, forward, and save it~

Guess you like

Origin blog.csdn.net/weixin_42584100/article/details/127831572