Process synchronization
Article Directory
- Process synchronization
-
- One, process synchronization, mutual exclusion
- Second, the software implementation method of mutually exclusive processes
- Third, the hardware implementation method of mutual exclusion of processes
- Fourth, the semaphore mechanism
- Five, use semaphores to achieve mutual exclusion, synchronization, and predecessor relationships between processes
- 6. Producer-consumer issues
One, process synchronization, mutual exclusion
1. The concept of process synchronization
Process synchronization refers to coordinating the work sequence of multiple concurrent execution processes
2. The concept of process mutual exclusion
Critical resources: resources that only one process can use in a period of time
Process mutual exclusion means that when a process accesses a critical resource, another process that wants to access the critical resource must wait. The access of the process currently accessing the critical resource ends. After the resource is released, another process can access the critical resource.
3. Access to critical resources
The exclusive access to critical resources can be logically divided into the following four parts:
4. Principles to be followed when accessing critical resources
(1) Free to let in
When the critical section is free, a process requesting to enter the critical section can be allowed to enter the critical section immediately
(2) Waiting if busy
When an existing process enters the critical section, other processes trying to enter the critical section must wait
(3) Limited waiting
For the process requesting access, it should be ensured that it can enter the critical section within a limited time (to ensure that it will not be starved)
(4) Give power and wait
When the process cannot enter the critical area, the processor should be released immediately to prevent the process from being busy waiting (should not let him occupy the processor to execute the cycle and cannot advance, and it should be known that the cycle is not executed when the critical area cannot be entered, and the process is switched directly)
5. Knowledge review
Second, the software implementation method of mutually exclusive processes
1. Single sign method
Idea: After a process accesses the critical section, it will give the permission to use the critical section to another process, that is, the permission of each process to enter the critical section can only be granted by another process
process:
If P0 wants to access the critical section, turn becomes 0, P1 will always circulate in ⑤ and cannot go down. After P0 is accessed, change turn to 1, P1 jumps out of the loop, and P1 can access the critical section
Note:
(1) The algorithm can realize that only one process can access the critical area at the same time.
(2) If the process allowed to enter the critical area at this time is P0, and P0 has not accessed the critical area, then although the critical area is idle at this time, However, P1 has not been allowed to visit. Therefore, the problem with the single-sign law is that it violates the principle of free access
2. Double mark first check method
Idea: Set up a boolean array flag[], each element in the array marks whether each process wants to enter the critical area, true means you want to enter, false means you don’t want to enter; each process first checks whether there are other processes currently wanting to enter the critical area Enter the critical area, if not, change its corresponding flag flag[i] to true, and then start to access the critical area
process:
Existing problem: After the P0 process enters, before changing P0 to true, switch to P1. P1 checks that no other process wants to enter the critical area, so P1 will be changed to true, causing both processes to be true and access at the same time
Note:
(1) The problem with the double-sign first inspection method is: Violation of the busy, then wait principle
(2) Reason for the emergence: Checking and locking of the entry area is not done in one go
3. Double sign post inspection method
Idea: Double-sign check the revision of the law first, lock first and then check, whoever wants to enter, directly change themselves to true, don't care about other processes, after changing to true, check if there are other processes that want to access
process:
存在的问题:P0想进入,P0改为true,在检查之前切换到P1,P1想进入,改为true, 导致两个进程都为true,谁都无法访问临界区,产生饥饿现象
Note:
(1) The double-sign post-checking method solves the problem of waiting while busy, but it violates the principle of letting in idle and limited waiting.
(2) Reasons for emergence: Checking and locking of the entry zone is not done in one go
4. Peterson algorithm
Idea: The revision of the double-mark post-checking method. If two processes want to enter the critical area, they can take the initiative to give the other party priority access to the critical area
process:
Enter the zone and do three things: 1. Actively fight for 2. Actively humbly 3. Check the opponent
Note:
Peterson's algorithm solves the three principles of letting in when idle, waiting when busy, and limited waiting, but it violates the principle of giving power and waiting.
5. Knowledge review
Third, the hardware implementation method of mutual exclusion of processes
1. Interrupt masking method
It is realized by the on/off interrupt instruction, which is the same as the original idea, that is, it is not allowed to be interrupted when a process starts to access the critical area until the end of the access, so process switching cannot occur, so it is impossible for two processes to access the critical area at the same time District situation
Advantages: simple and efficient
Disadvantages: not applicable to multiprocessors; only applicable to kernel processes, not applicable to user processes
2. TestAndSet instruction (TS instruction)
Also known as TestAndSetLock instruction (TSL instruction)
The TSL instruction is implemented by hardware, and the execution process is not allowed to be interrupted and can only be done in one go, as shown in the figure:
If the lock is false at the beginning (no process is accessing the critical area), the TSL return value is false and will not be stuck in the while loop, and the process can access the critical area; if it is true at the beginning (the process is accessing the critical area), TSL If the return value is true, it will be stuck in the while loop until the access of the process that is accessing the critical section ends, and the lock value is changed to false, the process can enter the critical section
Advantages: simple to implement, suitable for multi-processor environment
Disadvantages: Does not meet the power-to-wait principle. Processes that cannot enter the critical section temporarily occupy the CPU and execute TSL instructions cyclically, resulting in busy waiting
3. Swap instruction
Also known as Exchange command (XCHG command)
The Swap instruction is implemented by hardware, and the execution process is not allowed to be interrupted and can only be done in one go, as shown in the figure:
Only when lock is false (no process accesses the critical section) can it break out of the loop and access the critical section
Advantages: simple to implement, suitable for multi-processor environment
Disadvantages: Does not meet the power-to-wait principle, and the process that cannot enter the critical section temporarily will occupy the CPU and execute the Swap instruction cyclically, resulting in busy waiting
4. Knowledge review
Fourth, the semaphore mechanism
1. Cause
The four software implementation methods of process mutual exclusion and the three hardware implementation methods can not realize the power waiting, that is, when the process cannot enter the critical area, it will occupy the processor for a long time cycle (because there is no need to always cycle, you can switch the process directly)
2. Introduction
(1) A semaphore is a variable that represents the amount of a certain resource in the system
(2) The user process can operate on the semaphore by using a pair of primitives provided by the operating system
(3) A pair of primitives is: wait( S) primitive and signal(S) primitive, S is a semaphore (understand this pair of primitives as a function)
(4) Wait(S) is usually called P operation, written as P(S); usually signal (S) is called V operation, written as V(S)
3. Integer semaphore
Use an integer variable as a semaphore to indicate the amount of a certain resource in the system, as follows:
4. Recorded semaphore
Improve on the basis of integer semaphore (not satisfying the power to wait), and use the record data structure to represent the semaphore, as follows:
For example: the
computer has two printers, which need to be allocated to different processes. The initial value is 2, and the waiting queue is empty:
(1) P0 process uses printer, execute wait, value–, value becomes 1
(2) P1 process uses printer, execute wait, value–, value becomes 0 (no printer is available)
(3) P2 process uses printer, execute wait, value–, value becomes -1, value <0, executes the block, and becomes the head of the waiting queue (when it is unavailable, it will not occupy the processor's execution cycle, and there is no busy phenomenon)
(4) P3 process uses the printer , Execute wait, value–, value value becomes -2, value <0, execute block, and become the second process in the waiting queue
(5) Switch to P0, P0 finishes using the printer, execute signal, value++, value value becomes -1 , Value <= 0, execute wakeup, wake up the head of the waiting queue (P2 process), P2 moves out of the waiting queue, P2 uses the printer
(6) Switch to P2, P2 finishes using the printer, executes signal, value++, value becomes- 1, value <= 0, execute wakeup, wake up the head of the waiting queue (P3 process), P3 moves out of the waiting queue, and P3 uses the printer
- Knowledge review
Five, use semaphores to achieve mutual exclusion, synchronization, and predecessor relationships between processes
1. Semaphore mechanism to achieve mutual exclusion of processes
Set the mutual exclusion semaphore mutex, the initial value is 1, as shown in the figure:
Note:
(1) Different mutually exclusive semaphores need to be set for different critical resources
(2) P and V operations must occur in pairs, lack of P cannot guarantee mutual exclusive access, lack of V cannot wake up waiting processes
2. Semaphore mechanism to achieve process synchronization
To achieve process synchronization, it is necessary to ensure that the execution of the process is sequential, that is, one after the other
(1) Set the synchronization semaphore S, the initial value is 0 (understood that there is no resource at the beginning, the P1 process wants to use the resource must be generated through P2)
(2) V(S) is executed after the previous process
(3) Before the post process Perform P(S)
3. The semaphore mechanism realizes the precursor relationship
4. Knowledge review
6. Producer-consumer issues
- problem analysis
There are a set of producer processes and a set of consumer processes in the system. Each time the producer process produces a product and puts it into the buffer, the consumer process takes out a product from the buffer every time and uses it. The producer and consumer share an initial An empty buffer with size n
2. Relationship analysis
(1) Synchronization relationship
i. When the buffer is full, the producer needs to wait for the consumer to take the product
ii. When the buffer is empty, the consumer needs to wait for the producer to put the product in
(2) Mutually exclusive relationship
i. Producers and consumers must mutually exclusive access buffers (the buffer is a critical resource)
ii. Producers and producers must mutually exclusive access buffers (two producers may overwrite data)
iii. Consumers and consumers To access the buffer exclusively (two consumers may read empty data)
- achieve