2.6 Operating System (Producer Consumption Problem Multi-Producer-Consumer Problem Smoker Problem)

content

1. Producer consumption problem

 Can the order of adjacent P and V operations be changed?

 2. The multi-producer-consumer problem

 How to achieve?

 Can we use mutex semaphore?

If the plate (buffer) capacity is 2

Knowledge review and important test points  

3. The problem of smokers

How to achieve 


 Personal Homepage: Personal Homepage
 Series Column: Operating System

1. Producer consumption problem

  • There are a set of producer processes and a set of consumer processes in the system. The producer process produces a product into the buffer each time, and the consumer process
  • The process pulls one product from the buffer at a time and uses it. (Note: The "product" here is understood as some kind of data)
  • Producers and consumers share an initially empty buffer of size n.
  • The producer can put the product into the buffer only when the buffer is not full , otherwise it has to wait. [Buffer is not full → Producer produces]  

insert image description here 

 insert image description here

 

Consumers can take products from the buffer only if the buffer is not empty , otherwise they have to wait. [buffer is empty → consumer consumption]   

insert image description here

 Buffers are critical resources that must be accessed mutually exclusive by each process

 insert image description here

 

 Steps for topic analysis:

1. Relationship analysis. Find out the various processes described in the title, and analyze the synchronization and mutual exclusion relationship between them.
2. Organize your thoughts. The approximate sequence of P and V operations is determined according to the operation flow of each process. [The producer consumes one free buffer at a time and produces (V) one product. The consumer consumes (P) one product at a time and releases a free buffer V). Putting/removing products from the buffer requires mutual exclusion.
3. Set the semaphore. And determine the initial value of the semaphore according to the subject conditions. (The initial value of the mutual exclusion semaphore is generally 1, and the initial value of the synchronization semaphore depends on the initial value of the corresponding resource)

 insert image description here

 

 

 Can the order of adjacent P and V operations be changed?

 

If the buffer is full of products at this time, empty=0 , full=n .
Then the producer process executes ① to make mutex 0 , and then executes ②. Since there is no free buffer, the producer is blocked.
Since the producer blocks, switch back to the consumer process. The consumer process executes ③, because the mutex is 0 , that is, the producer has not yet
Release the "lock" on the critical resource, so the consumer is also blocked.
This results in a situation where the producer waits for the consumer to release the free buffer, and the consumer waits for the producer to release the critical section.
The producer and the consumer cycle waiting to be woken up by each other, resulting in a "deadlock".
Likewise, if there are no products in the buffer, full=0 , empty=n . Executing in the order of ③④① will cause deadlock.
Therefore, the mutually exclusive P operation must be implemented after the synchronized P operation .
The V operation does not cause the process to block, so the order of the two V operations can be swapped .

The producer-consumer problem is a comprehensive problem of mutual exclusion and synchronization.

Sometimes the consumer needs to wait for the producer to produce, and sometimes the producer needs to wait for the consumer to consume. These are two different "one-before-one-after problems", so two synchronization semaphores also need to be set.

 

insert image description here 

 

 2. The multi-producer-consumer problem

Problem Description

There is a plate on the table, and only one fruit can be put into it at a time. The father puts apples on the plate, the mother puts oranges on the plate, the son waits to eat the oranges on the plate, and the daughter waits for the apples on the plate. Only when the plate is empty can Mom or Dad put a piece of fruit on the plate. A son or daughter can remove fruit from the plate only if the fruit they need is on the plate.
The above process is implemented with PV operation:

 

Relationship analysis . Find out the various processes described in the title, and analyze the synchronization and mutual exclusion relationship between them.
Organize your thoughts . The approximate sequence of P and V operations is determined according to the operation flow of each process. [Mutual exclusion: PV before and after the critical section; Synchronization: P before and after V]
Set the semaphore . Set the required semaphore, and determine the initial value of the semaphore according to the subject conditions. (The initial value of the mutual exclusion semaphore is generally 1, and the initial value of the synchronization semaphore depends on the initial value of the corresponding resource)
 

 

 How to achieve?

 Can we use mutex semaphore?

insert image description here

 Analysis: At the beginning, the son and daughter processes will be blocked even if they run on the processor. If the father process runs on the processor first, then: father P(plate), can access the plate → mother P(plate), block waiting for the plate → father puts apple V(apple), the daughter process is awakened, and other processes Even if it is running, it will be blocked, and it is temporarily impossible to access the critical resource (plate) → daughter P(apple), access the plate, V(plate), the mother process waiting for the plate is awakened → the mother process accesses the plate (no other process can temporarily enter the critical district) →…

Conclusion: Even if a special mutex variable is not set, there will be no phenomenon that multiple processes access the disk at the same time

The reason is: the buffer size in this question is 1. At any time, at most one of the three synchronization semaphores apple, orange and plate is 1. Therefore, at any time, at most one process' P operation will not be blocked and enter the critical section smoothly...

If the plate (buffer) capacity is 2

insert image description here

The father P(plate) can access the plate → the mother P(plate) can access the plate the father is putting apples on the plate, and the mother can also put oranges on the plate. So there is a situation where two processes access the buffer at the same time, which may cause the data written to the buffer by the two processes to overwrite each other.
Therefore, if the buffer size is greater than 1, a mutex semaphore must be specially set to ensure exclusive access to the buffer.

 

Summary : In the producer-consumer problem, if the buffer size is 1, then it is possible to achieve the function of mutually exclusive access to the buffer without setting the mutex semaphore. Of course, this is not absolute, and specific problems need to be analyzed in detail.

Suggestion: If it is too late to analyze carefully in the exam, you can add a mutex semaphore to ensure that each process will access the buffer in a mutually exclusive manner. But it should be noted that the mutually exclusive P operation must be implemented after the synchronized P operation, otherwise it may cause a "deadlock"

Problem solving ideas for PV operation problems:

①, relationship analysis. Find out the various processes described in the title, and analyze the synchronization and mutual exclusion relationship between them.
②, organize ideas. The approximate sequence of P and V operations is determined according to the operation flow of each process.
③, set the semaphore. Set the required semaphore, and determine the initial value of the semaphore according to the subject conditions. (The initial value of the mutual exclusion semaphore is generally 1, and the initial value of the synchronization semaphore depends on the initial value of the corresponding resource)

 

Knowledge review and important test points 

  • The key to solving the "multi-producer-multi-consumer problem" is to sort out the complex synchronization relationship.
  • When analyzing the synchronization problem (the problem of tandem), it cannot be analyzed from the perspective of the behavior of a single process. What happens in the "tandem" should be regarded as the context of two "events".
  • For example, if we consider it from the perspective of the behavior of a single process , we will have the following conclusions:
    1. If there are apples in the plate, then the father or mother can only put the fruit after the daughter has taken the apples. 2. If the plate contains
    apples If there are oranges, then the father or mother must put the fruit after the son has taken the oranges
  • Does this mean that four synchronization semaphores need to be set up to realize the four "one in tandem" relationship?
  • The correct analysis method should be considered from the perspective of "event" . We can abstract the above four pairs of "context of process behavior" into a pair of "context of event"
  • Plate empty event → put fruit event. The "plate emptying event" can be triggered by both sons and daughters; the "fruit-putting event"
  • It may be performed by the father or by the mother. In this case, you can use a synchronization semaphore to solve the problem

 

 

3. The problem of smokers

Suppose a system has three smoker processes and one provider process . Every smoker keeps rolling and smoking a cigarette, but to roll and smoke a cigarette, the smoker needs three materials: tobacco, paper, and glue. Of the three smokers, the first has tobacco, the second has paper, and the third has glue . The supplier process ᨀ feeds three materials indefinitely, the supplier puts two materials on the table at a time, the smoker who has the remaining material rolls a cigarette and smokes it , and gives the supplier process a signal telling completion , the supplier will put the other two ingredients on the table, and this process is repeated ( let three smokers take turns smoking )

insert image description here 

 

In essence, this problem is also a "producer-consumer" problem, and more specifically, it should be "a single producer that can produce multiple products-multiple
consumer".
1. Relationship analysis. Find out the various processes mentioned in the title, and analyze the synchronization and mutual exclusion relationship between them.
2. Organize your thoughts. Determine the approximate sequence of P and V operations according to the operation flow of each process
3. Set the semaphore. Set the required semaphore, and determine the initial value of the semaphore according to the subject conditions. (The initial value of the mutual exclusion semaphore is generally 1, and the initial value of the synchronization semaphore depends on the initial value of the corresponding resource)

insert image description here 

insert image description here 

 

How to achieve 

insert image description here

 The smoker problem can give us an idea for solving the "single producer who can produce multiple products" problem.

The essence worth learning is: "Let each smoker take turns smoking" necessarily requires "taking turns to put a combination of 1, 2, and 3 on the table", pay attention to understand how we use an integer variable i to achieve this "turn" process .

If the title is changed to "Let one smoker randomly at a time", how should we write this logic in code?

If a producer wants to produce multiple products (or will trigger multiple precursor events), then each V operation should be placed after the corresponding "event" occurs.

 

 

Guess you like

Origin blog.csdn.net/Javascript_tsj/article/details/124329520