computer operating system process

Table of contents

process definition

basic concept

Process entities and processes

Composition of PCB

process organization

link method

index method

process characteristics

state of the process

Three basic states of a process

The other two states of the process

Process State Transitions

process control

What is process control

How to implement process control

primitive language

process communication

shared storage

Classification of shared storage

pipe communication

messaging

way of message delivery

direct communication

indirect communication

thread

What is a thread

Changes after introducing threads

thread properties

Implementation of threads

user-level thread

kernel-level thread

important:

multi-threaded model

many-to-one model

one-to-one model

many-to-many model

processor scheduling

Basic Concepts of Processor Scheduling

Advanced Scheduling

Intermediate Scheduling

low-level scheduling

The connection and comparison of three kinds of scheduling

​edit

Suspended states and the seven-state model

process scheduling 

Timing of process scheduling

The situation of process scheduling and switching

Situations where process scheduling cannot be performed

Critical Sections and Critical Resources

The method of process scheduling

process switching

The process of process switching

Evaluation Indicators for Scheduling Algorithms

CPU utilization

system throughput

Turnaround time

waiting time

Response time

Scheduling Algorithm

First come first serve algorithm (first come first serve)

shortest job first

High response ratio priority algorithm (Highest Response Ratio Next)

Time slice rotation (Round-Robin)

priority scheduling algorithm

Multi-Level Feedback Queue Algorithm

Algorithm summary

Process synchronization and mutual exclusion

concept

Mutual exclusive access to critical resources can be logically divided into the following four parts

In order to achieve access to critical resources while ensuring overall performance, the following principles must be followed

Software Realization Method of Process Mutual Exclusion

single sign method

double marker test

double sign post-check

peterson algorithm

Hardware Implementation Method of Process Mutual Exclusion

interrupt mask method

TestAndSet command

Swap command

process definition

basic concept

  • Program: A sequence of instructions
  • Program segment: the place where the program code is stored in the memory, which is generally placed at the low address of the memory
  • Data segment: The part that stores data in the memory, used when the program is running, and is generally placed at the high address of the memory
  • PCB: The process control module, which is stored in the memory, is the data structure used by the operating system to manage the concurrent execution of each process, which contains various information required by the operating system to manage these running programs

Notice:

  • Before the execution of each program, the operating system will allocate a PCB (process control module) data structure for the program to be executed.
  • The PCB is the only sign of the existence of the process
  • There can be multiple program segments or data segments in memory

Process entities and processes

Process entity: It consists of three parts: PCB, program segment, and data segment. It is also called process image. Generally speaking, we can refer to process entity as process for short. The so-called creation process is essentially to create the PCB in the process entity; and the cancellation process is essentially to cancel the PCB in the process entity

Process: It is the running process of the process entity, and is an independent unit (running program) for resource allocation and scheduling by the system

Composition of PCB

Notice:

  • Process identifier: When a process is created, the operating system will assign a unique, unique ID to the process to distinguish different processes
  • User identifier: used to identify who the user the process belongs to
  • The reason why the values ​​of various registers are saved in the PCB: When the process is switched, it is necessary to record the current running status of the process and save it in the PCB. For example, the value of the program counter indicates which sentence the current program is executed to

process organization

Preface: In a system, there are usually tens, hundreds, or even thousands of PCBs. In order to be able to manage them effectively, these PCBs should be organized in an appropriate way.

link method

Understanding: The PCB is divided into multiple queues according to the process status, and the operating system holds pointers to each queue

index method

Understanding: Create several index tables according to different process states; the operating system holds pointers to each index table

process characteristics

Note: Dynamism is the most fundamental characteristic of a process.

state of the process

Preface: A process is an execution of a program. In this execution process, sometimes the process is being processed by the CPU, and sometimes it needs to wait for the CPU service. It can be seen that the state of the process will change in various ways. In order to facilitate the management of each process, the operating system needs to divide the process into several states

Three basic states of a process

  • Running state: already occupying the CPU and running on the CPU (in a single-core processor environment, at most one process is running at a time)
  • Ready state: already has the running conditions, but because there is no idle CPU, it cannot run temporarily (the process in the ready state already has all the required resources except the processor, once the processor is obtained, it can immediately enter the running state and start operation)
  • Blocking state: temporarily unable to run due to waiting for an event

The other two states of the process

  • Creation state: the process is being created, the operating system allocates resources for the process, and initializes the PCB
  • Termination state: The process is being withdrawn from the system, and the operating system will reclaim the resources owned by the process and revoke the PCB

Process State Transitions

Notice:

  • Running state to blocking state is an active behavior made by the process itself
  • From blocking state to ready state is not controlled by the process itself, it is a passive behavior
  • It cannot be directly converted from the blocking state to the running state, nor can it be directly converted from the ready state to the blocking state 

process control

What is process control

Meaning: 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, etc.

Simple understanding: realize the transition between process states

How to implement process control

process:

  • Creating a process requires initializing the PCB and allocating system resources
  • From the creation state to the ready state, it is necessary to modify the PCB content and the corresponding queue
  • From the ready state to the running state, it is necessary to restore the running environment of the process, modify the PCB content and the corresponding queue
  • From the running state to the terminated state, the resources owned by the process must be recovered, and the PCB should be revoked
  • From the running state to the blocking state, the running environment of the process must be saved, and the content of the PCB and the corresponding queue should be modified.
  • From the blocking state to the ready state, the content of the PCB and the corresponding queue must be modified. If you are waiting for resources, you also need to allocate system resources for the process

Note: What needs to be done in the middle of switching the process state is more complicated. We need to modify the PCB content and put the PCB into the corresponding queue; if a certain process puts its PCB from one queue to another queue, but we do not If the corresponding flags in the PCB are not modified, then this situation is very dangerous and may cause system errors; in order to prevent this problem, primitives are used to realize process control.

primitive language

Preface: The characteristic of primitives is that no interruption is allowed during execution, and it can only be done in one go. This kind of operation that cannot be interrupted is called an atomic operation.

Notice:

  • The primitives are implemented by combining the instructions "off interrupt" and "on interrupt"
  • If an external interrupt signal enters after executing the interrupt-off command, the external interrupt signal will be ignored and not processed temporarily; only after the interrupt-on command is executed and an external interrupt message is received, the Transfer to the corresponding interrupt handler to start processing
  • The authority to turn off/on interrupt instructions is very large, and it must only be allowed to execute privileged instructions in the core state

Primitives to do

  1. Update the information in the PCB (such as modifying the flag of the process state, saving the operating environment on the PCB, restoring the operating environment from the PCB)
  2. Insert the PCB into the appropriate queue
  3. Allocate/reclaim resources

process communication

Foreword:

  • 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
  • To ensure security, a process cannot directly access the address space of another process

Meaning: Process communication refers to the exchange of information between processes

Note: Although one process cannot directly access the address space of another process, the exchange of information between processes must be implemented. In order to ensure safe communication between processes, the operating system provides some methods of process communication (shared storage, message passing, pipeline communication)

shared storage

Meaning: Since the two processes cannot directly access each other's address space, the operating system will allocate a shared space for the two processes; the communication between the two processes can be done through the shared space

Notice:

  • The access of two processes to the shared space must be mutually exclusive. When process 1 is writing data to this shared space, process 2 is not allowed to access this shared space. Only process 1 releases the access to this shared space. 2 to start the read operation
  • Mutual exclusion is achieved through facilities provided by the operating system

Classification of shared storage

  • 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 has many restrictions, and it is a low-level communication method
  • Sharing based on the storage area: If a shared storage area is drawn in the memory, the form and storage location of the data are controlled by the process, not the operating system. In contrast, this sharing method is faster and is an advanced communication method

pipe communication

Notice:

  • A pipe refers to a shared file used to connect read and write processes, also known as a pipe file. In fact, it is to open up a fixed-size buffer in memory.
  • The pipeline can only use half-duplex communication, and only one-way transmission can be realized within a certain period of time. To achieve two-way simultaneous communication, two pipelines need to be set up
  • Each process's access to the pipe needs to be mutually exclusive (when process 1 writes data into it, process 2 cannot read the data in the pipe)
  • Data is written into the pipeline in the form of a character stream. When the pipeline is full, the write() system call of the writing process will be blocked, waiting for the reading process to take the data away. When the reading process takes the data away, the pipe becomes empty, and the read() system call of the reading process will be blocked
  • If the pipe is not full, reading is not allowed; if it is not empty, writing is not allowed
  • Once the data is read, it will be discarded from the pipeline, which means that there can only be at most one reading process, otherwise there may be cases of reading wrong data

messaging

Preface: 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

Note: A formatted message will be divided into two parts: message header and message body

way of message delivery

  • Direct communication mode: the message is directly hung on the message buffer queue of the receiving process
  • Indirect communication method: the message must be sent to the intermediate (mailbox) first, so it is also called "mailbox communication method"

direct communication

Understanding: Each process will have a message buffer queue. If another process wants to send a message to the process, the other process will first create a formatted message body, and then the message will be sent to The message; then this message will be hung at the end of the message buffer queue of the process, and then the process will take the messages in the queue one by one through the receiving primitive for processing 

indirect communication

Understanding: The system will manage a mailbox for each communication process, and there may be various messages in this mailbox; and this message may be some messages communicated between different processes; specifically, it is sent by that process and received by that process These are included in the message header; if a process wants to send a message to another process, the message is sent to the mailbox of the intermediate entity using the send primitive, and then the reading process takes the message from the mailbox using the receive primitive.

thread

What is a thread

Preface: Some processes need to handle many things at the same time, while traditional processes can only execute a series of programs serially. To this end, threads are introduced to increase concurrency

Thread: Thread is the smallest unit of program execution flow. It is included in the process and is the actual operating unit of the process.

Notice:

  • The traditional process is the smallest unit of program execution flow, but after the introduction of threads, threads become the smallest unit of program execution flow
  • Before the introduction of threads, a process corresponds to a piece of code, and these codes can only be executed sequentially; but after the introduction of threads, each process can have multiple threads, and these threads can have different codes. It can be that different threads run the same code, but these codes will be processed by the CPU concurrently, and executed sequentially concurrently
  • A thread is a basic CPU execution unit and the smallest unit of program execution flow. 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, send file)
  • After the thread is introduced, the process is only used as a system resource allocation unit other than the CPU (such as printers, memory address spaces, etc. are all allocated to the process)
  • A process can open multiple threads, one of which is the main thread to mobilize other threads in this process; the process switching we see is also the main thread of different processes
  • Each thread has its own independent memory space while sharing the memory space of the same process

Changes after introducing threads

thread properties

Implementation of threads

user-level thread

Notice:

  • User-level threads are implemented by the application program through the thread library, and all thread management work is the responsibility of the application program (including thread switching) 
  • In user-level threads, thread switching can be completed in user mode without operating system intervention
  • 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 opaque to users and transparent to the operating system)
  • It can be understood that user-level threads are threads that can be seen from the user's perspective

kernel-level thread

Notice:

  •  The management of kernel-level threads is done by the operating system kernel. The kernel is responsible for the scheduling and switching of threads, so the switching of kernel-level threads must be completed in the core state
  • It can be understood that kernel-level threads are threads that can be seen from the perspective of the operating system (users can also see)

important:

  • In an operating system that supports both user-level threads and kernel-level threads, a combination of the two can be used: map n user-level threads to m kernel-level threads (n>=m)
  • The operating system can only see kernel-level threads, so only kernel-level threads are the unit of processor allocation

multi-threaded model

Preface: In a system that supports both user-level threads and kernel-level threads, the problem of mapping several user-level threads to several kernel-level threads raises the question of the multithreading model.

many-to-one model

Meaning: Multiple user-level threads are mapped to one kernel-level thread. Each user-level process corresponds to only one kernel-level thread

Advantages and disadvantages 

  • Advantages: Since the management of user-level threads is the responsibility of the application program, the switching of user-level threads only needs to be completed in the user state, without switching to the core state, and the system overhead of thread management is high in efficiency.
  • Disadvantages: When a user-level thread is blocked, the entire process will be blocked (the kernel-level thread is also blocked-because there is only one thread in the operating system, the user-level thread is blocked and the kernel-level thread is also blocked), and the degree of concurrency not tall. Multiple threads cannot run in parallel on a multi-core processor

one-to-one model

Meaning: 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 and disadvantages

  • Advantages: If a user-level thread is blocked, other user-level threads can continue to execute, with strong concurrency; at the same time, multiple user-level threads are mapped to different kernel-level threads, and kernel-level threads are Is the only unit of processor scheduling, so these user-level threads can run in parallel under multi-core processors
  • Disadvantages: A user process will occupy multiple kernel-level threads, and the thread switching is completed by the kernel of the operating system, which needs to be switched to the core state, so the thread management cost is high and the overhead is large

many-to-many model

Meaning: n user-level threads are mapped to m kernel-level threads (n>=m) each user process corresponds to m kernel-level threads

Advantages: It overcomes the disadvantage of low concurrency in the many-to-one model, and also overcomes the disadvantages of one user process occupying too many kernel-level threads in the one-to-one model and the overhead is too large

processor scheduling

Basic Concepts of Processor Scheduling

Scheduling: When there are a bunch of tasks to be processed, but due to limited resources, some things cannot be processed at the same time. This requires determining some kind of rules to determine the order in which these tasks are processed, which is the problem of "scheduling" research

Processor scheduling: In a multiprogramming system, the number of processes is often more than the number of processors, so it is impossible to process each process in parallel at the same time. Processor scheduling is to select a process from the ready queue according to a certain algorithm and assign a processor to it to run, so as to realize the concurrent execution of processes

Advanced Scheduling

Preface: Due to the limited memory space, sometimes all the jobs submitted by users cannot be put into memory, so some rules need to be determined to determine the order in which jobs are loaded into memory

Advanced scheduling (job scheduling): refers to selecting one (or more) jobs from the jobs in the back queue on the external storage according to certain principles, allocating necessary resources such as memory to them, and creating corresponding processes (creating PCBs), to give them the right to compete for processors

Note: Advanced scheduling is scheduling between auxiliary storage (external storage) and memory. Each job is called in only once and called out only once. The corresponding PCB will be created when the job is called in, and the PCB will be canceled when the job is called out. Advanced scheduling mainly refers to the problem of calling in, because only the timing of calling in needs to be determined by the operating system, but the timing of calling out must be called out after the job is finished.

Intermediate Scheduling

Preface: After the introduction of virtual storage technology, the process that cannot run temporarily can be transferred to external memory to wait. When it has the operating conditions again and the memory is a little free, it will be transferred to the memory again; the purpose of this is to improve memory utilization and system throughput

Intermediate Scheduling (Memory Scheduling): It is to decide to reload the suspended process into memory

Notice:

  • The status of the waiting process temporarily transferred to the external storage is the suspended status. It is worth noting that the PCB will not be transferred to the external storage together, but will be resident in the internal memory. The PCB will record the storage location of process data in the external memory, process status and other information. The operating system maintains the monitoring and management of each process through the PCB in the memory. The suspended process PCB will be placed in the suspension queue
  • In intermediate scheduling, a process may be called in and out of memory multiple times, so the frequency of intermediate scheduling is higher than that of advanced scheduling

low-level scheduling

Low-level scheduling (process scheduling): its main task is to select a process from the ready queue according to a certain method and strategy, and allocate processor resources to it

Notice:

  • Low-level scheduling is the most basic scheduling in the operating system, and process scheduling must be configured in general operating systems
  • The frequency of process scheduling is very high, usually every tens of milliseconds
  • Low-level scheduling is scheduling between memory and processors

The connection and comparison of three kinds of scheduling

Suspended states and the seven-state model

Foreword: The state of the process temporarily transferred to the external memory to wait is the suspended state, which can be divided into two states: ready suspension and blocking suspension

Ready hang: If a process in the ready state is under a relatively high load at this time and the memory space is not enough, then he may temporarily transfer a process in the ready state to the external memory. The process enters a ready-suspended state; until the memory space is free, or the process needs to continue to execute, then this process will be activated, and its corresponding data will be moved back to the memory, such a ready-suspended process back to ready

Blocking suspension: A process in the blocked state can also be blocked, or it can be reloaded into memory, and reactivated to return to the blocked state

Notice:

  • Some operating systems may block a suspended process, when his blocking event occurs; this process will directly enter the ready suspended state
  • Sometimes a process is in the running state, and it may be directly put into the external memory after running, so that it enters the ready and suspended state
  • Sometimes when a process in the creation state is created, there may be insufficient memory space. In this case, the process in the creation state may directly enter the ready and suspended state
  • Suspended and blocked states are temporarily unable to obtain CPU services, but the suspended state is to transfer the process image to the external memory, while the process image is still in the memory in the blocked state
  • Some operating systems will divide the ready hang and blocking hang into two hang queues, and even further divide the blocking hang into multiple queues according to the cause of blocking 

process scheduling 

Timing of process scheduling

Process scheduling (job scheduling): It is to select a process from the ready queue according to a certain algorithm to assign a processor to it

The situation of process scheduling and switching

Situations where process scheduling cannot be performed

Critical Sections and Critical Resources

  • Critical resource: A resource that is only allowed to be used by one process in a period of time. Processes need mutually exclusive access to critical resources
  • Critical Section: The section of code that accesses critical resources

The method of process scheduling

  • Non-preemptive scheduling: also known as non-preemptive scheduling. That is, only the process is allowed to voluntarily give up 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.
  • Deprivation scheduling method: also known as preemptive scheduling. When a process is executing on the processor, if there is a more important and more urgent process that needs to use the processor, immediately suspend the executing process and assign the processor to the more important and more urgent process

process switching

Meaning: a process gives up the processor, and another process occupies the processing and process

The process of process switching

  1. Save various data of the original process
  2. Restoration of various data of the new process (such as: program counter, program status word, various data registers and other on-site information of the processor, which are generally stored in the process control block)

Note: Process switching is costly, so too frequent process scheduling and switching will inevitably reduce the efficiency of the entire system, making most of the system's time spent on process switching, and the actual time spent on executing processes is reduced .

Evaluation Indicators for Scheduling Algorithms

CPU utilization

Meaning: refers to the ratio of CPU busy time to the total time

Formula: CPU utilization = CPU busy time / total time

system throughput

Preface: For a computer, it is hoped that it can process as many jobs as possible in as little time as possible

System throughput: the number of jobs completed per unit time

Formula: System throughput = total number of jobs completed / total time spent

Turnaround time

Preface: For a computer, it is very concerned about how much time it takes for its homework from submission to completion

Turnaround time: refers to the time interval from when a job is submitted to the system to when the job is completed

official:

  • Turnaround time = completion time of the job - job submission time
  • Average turnaround time = sum of turnaround times for each job / number of jobs
  • Weighted turnaround time = job turnaround time / actual running time of the job
  • Average weighted turnaround time = sum of weighted turnaround time of each job / number of jobs

Notice:

  • Turnaround time with rights must be >=1
  • Both the entitlement turnaround time and the turnaround time are as small as possible
  • The shorter the weighted turnaround time, the higher the user satisfaction

waiting time

Meaning: 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

Notice:

  • For a process, the waiting time refers to the sum of the waiting time for the process to be served after the process is established. In fact, the process is also being served while waiting for the completion of the IO, so it does not enter the waiting time.
  • For the job, not only the waiting time after the process is created, but also the waiting time of the job in the back queue of the external memory

Response time

Meaning: Refers to the time it takes for a user to make a request and generate a response for the first time

Scheduling Algorithm

First come first serve algorithm (first come first serve)

shortest job first

The shortest remaining time priority 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, and the current running process will return. to the ready queue.

Note: The short job priority scheduling algorithm selects the job/process that has currently arrived and has the shortest running time for each scheduling

High response ratio priority algorithm (Highest Response Ratio Next)

Time slice rotation (Round-Robin)

Time slice rotation algorithm: 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)

Note: If the time slice is too large so that each process can be completed within one time slice, the time slice rotation algorithm will degenerate into a first-come-first-serve algorithm and increase the process response time, so the time slice cannot be too large ; On the other hand, there is a time cost for process scheduling and switching (saving and restoring the running environment). Therefore, if the time slice is too small, the process switching will be too frequent, and the system will spend a lot of time processing the process switching, resulting in the actual use of Process execution time scaled down. It can be seen that the time slice cannot be too small

priority scheduling algorithm

Preemptive priority scheduling algorithm: select the currently arrived process with the highest priority for each scheduling. Scheduling occurs when the current process voluntarily relinquishes a processor. In addition, when the ready queue changes, it is also necessary to check whether preemption will occur

Notice:

  • There may not be only one ready queue, and it can be organized according to different priorities. In addition, the process with the highest priority can also be placed 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 creating a process, and then the priority will be dynamically adjusted according to the situation
  • Generally, the priority of system processes is higher than that of user processes, the priority of foreground processes is higher than that of background processes, and the operating system prefers I/O busy processes

Multi-Level Feedback Queue Algorithm

process:

  • Set up multi-level ready queues, the priority of each queue is from high to low, and the time slices allocated for the queues at all levels are from small to large
  • When a new process arrives, it first enters the first-level queue, and queues up according to the FCFS principle to wait for the allocated time slice. If the time slice is used up and the process has not ended, the process enters the tail of the next-level queue. If it is already in the lowest-level queue at this time, it will be put back at the end of the lowest-level queue
  • Only when the kth level queue is empty, the time slice will be allocated to the process at the head of the k+1 level queue
  • The process of the preempted processor is put back to the end of the original queue

Algorithm summary

Process synchronization and mutual exclusion

concept

Synchronization: Synchronization is also known as direct constraint relationship. It refers to the constraint relationship between two or more processes established to complete a certain task. These processes need to coordinate their work order in certain positions; inter-process The direct constraint relationship is derived from the mutual cooperation between them

Process mutual exclusion: access to critical resources must be mutually exclusive. Mutual exclusion, also known as indirect constraint relationship. Process mutual exclusion means that when one process accesses a critical resource, another process that wants to access the critical resource must wait. The process accessing the critical resource currently accesses it. After the resource is released, another process can access the critical resource.

Mutual exclusive access to critical resources can be logically divided into the following four parts

  • Entry area: responsible for checking whether you can enter the critical area. If you can enter, you should set the flag of accessing critical resources (which can be understood as locking) to prevent other processes from entering the critical area at the same time
  • Critical Section: The section of code that accesses critical resources
  • Exit area: responsible for releasing the flag that is accessing critical resources (understood as unlocking)
  • Remaining area: do other processing

In order to achieve access to critical resources while ensuring overall performance, the following principles must be followed

  1. Idle let-in: When the critical section is idle, a process that requests to enter the critical section can be allowed to enter the critical section immediately
  2. Wait if busy: When an existing process enters the critical section, other processes trying to enter the critical section must wait
  3. Bounded waiting: For the process requesting access, it should be guaranteed to enter the critical area within a limited time (guaranteed not to be hungry)
  4. Right to wait: When the process cannot enter the critical area, the processor should be released immediately to prevent the process from busy waiting

Software Realization Method of Process Mutual Exclusion

single sign method

Algorithm idea: After two processes access the critical section, they will transfer the permission to use the critical section to another process. That is to say, the permission of each process to enter the critical area can only be granted by another process (the two processes use the critical area alternately)

Understanding: As can be seen from the above, turn=0 means that only the P0 process is allowed to enter the critical area at the beginning. If P1 enters, it will cause a while loop check until the time slice of P1 is used up and switch P0 to play; after P0 enters the critical area, after the visit is completed Only then can P1 play (turn=1), and so on;

Problem: P0 is the first to access the critical section. If the P0 process has not accessed the critical section, then the value of turn remains unchanged, and P1 has been unable to access the critical section;

double marker test

Algorithm idea: set a flag bit flag[], and each element in the array is used to mark the willingness of each process to enter the critical section. For example, "flag[0]=true" means that process 0 p0 now wants to enter the critical section. Before entering the critical section, each process checks whether there are other processes currently wanting to enter the critical section. If not, set its corresponding flag flag[i] to true, and then start to access the critical section

Understanding: If P0 wants to enter the critical area, he first checks whether P1 wants to enter, if P1 does not want to enter, it will change its own will to enter the critical area; at this time P1 wants to enter again, then check whether P0 wants to enter, Because P0 wants to enter P1, it keeps cyclically checking until P0 changes its will to not wanting to enter the critical area after it exits the critical area, and then it can enter the critical area, and so on.

Question: If P0 and P1 check whether the other party wants to enter the critical area (1526), ​​then it is found that the other party does not want to enter; eventually P0 and P1 will enter the critical area together (both flags are true); if it is busy, wait

double sign post-check

Algorithm idea: a revised version of the double-mark first check method. The previous algorithm checks first and then locks, but these two operations cannot be done in one go, which leads to the problem that two processes enter the critical section at the same time. Therefore, people think of the method of locking first and checking later to avoid the above problems

Understanding: P0 wants to enter the critical area, then he first changes his will to wants to go, then checks whether P1 wants to enter, sees that P1 does not want to enter, then enters by himself, and at this time P1 wants to enter, then first changes his will to If you want to enter and see P0, you also want to enter, it will continue to loop until P0 is unlocked, and so on.

Problem: Both P0 and P1 want to enter the critical area (flag[0]=flag[1]=true) and then start to check, and find that the other party wants to enter the critical area, but no one can enter, so they can only be in a hurry. , bounded wait

peterson algorithm

Algorithm idea: In the double-flag post-check method, both processes are vying to enter the critical area, but no one will let the other, and no one can enter the critical area in the end. Peterson thought of a method, if both parties are vying to enter the critical area, then the process can try Kong Rong to give pears, and actively let the other party enter the critical area first

Understanding: P0 wants to enter the critical area, but he lets P1 enter, then he checks whether P1 wants to enter, sees that P1 does not want to enter, then he enters; at this time P1 wants to enter, but he sees that P0 wants to enter after letting P0 enter , then it will wait in a loop until P0 enters and changes its own wishes to not want to enter

Analyzing concurrency: Both P0 and P1 want to enter the critical area, so P0 means that he let P1 enter the critical area; at this time, P0 loops until the time slice is used up, and P1 executes to 7. After executing 8, it is found that P0 also wants to enter, and at the same time, he also lets P0 enters, and then starts to loop until the time slice is used up. At this time, it runs to P0, because P1 is modest (the value of turn becomes 0); then the P0 process finds that P1 lets itself in during the while check, and then it into the critical section

Hardware Implementation Method of Process Mutual Exclusion

interrupt mask method

Meaning: Realized by "on/off interrupt instruction" (the same idea as the original language, that is, a process is not allowed to be interrupted from the beginning of accessing the critical area to the end of the access, and process switching cannot occur, so it is impossible to happen case of two simultaneous accesses to the critical section)

Advantages and disadvantages

  • Advantages: simple and efficient
  • Disadvantages: not applicable to multiprocessors; only applicable to operating system kernel processes, not user processes (because the switch interrupt instructions can only run in the kernel state, this group of instructions will be very dangerous if users use them at will)

TestAndSet command

Meaning: TS instruction for short, sometimes also called TestAndSetLock instruction or TLS instruction

Note: TSL instructions are implemented with hardware instructions, and the execution process is not allowed to be interrupted, it can only be done in one go

Understanding: before the thread enters the critical section, the while loop checks whether it is locked. If it is locked, it waits in a loop until another process releases the lock.

Advantages and disadvantages

  • Advantages: simple to implement, no need to check whether there are logical loopholes as strictly as software implementation methods; suitable for multi-processor environments
  • Disadvantages: It does not satisfy the principle of yielding and waiting. Processes that cannot enter the critical section temporarily will occupy the CPU and execute TLS instructions, resulting in busy waiting.

Swap command

Preface: The swap instruction is implemented by hardware, and it is not allowed to be interrupted during execution, it can only be done in one go

Note: The swap instruction is similar to the TestAndSet instruction (lock is false means there is no lock, lock is true means there is a lock, when accessing lock=true, old=false - because it has been swapped, the next process wants to access the critical section and see old= lock=true, there is no way to enter, unless the previous process puts the lock)

Guess you like

Origin blog.csdn.net/m0_60027772/article/details/129583351