Operating system--producer-consumer problem philosopher dining problem banker algorithm

Reference Knowledge Link

Single producer-consumer problem Consider synchronization

Basic constraints:

1. If the buffer is full, the producer cannot write data

2. If the buffer is empty, the consumer cannot read the data

Fake code:

data represents the semaphore of data, and empty represents the semaphore of the number of buffers

Use two semaphores to achieve the simplest synchronization problem

Producer

产生一个数据; 
P(empty); 
将数据放入buf;
V(data); 

consumer

P(data);
从buf中取数据;
V(empty);
打印; 

 

 

Multi-producer-consumer issues consider mutual exclusion

Only the above two are obviously not enough. One more semaphore is needed to indicate mutual exclusion: mutex: indicates whether the bounded buffer is occupied, the initial value is 1

New constraints:

1. Multiple producers write data to an address, which needs to be mutually exclusive

2. Multiple consumers fetching data from the same address, they need to be mutually exclusive

Fake code:

 


生产者: 
产生一个数据; 
P(empty); 
P(metex);
将数据放入buf;
V(metux);
V(data); 


消费者 
P(data);
P(metux);
从buf中取数据;
V(metex);
V(empty);
消费; 

 

Philosopher dining problem

From Baidu Encyclopedia

The philosopher's dining problem can be expressed in this way, assuming that five philosophers sit around a round dining table and do one of two things: eat, or think. When they eat, they stop thinking, and they stop eating when they think. There is a large bowl of pasta in the middle of the dining table and a fork between every two philosophers. Because it is difficult to eat pasta with one fork, it is assumed that philosophers must eat with two forks. They can only use the two forks on their left and right. The philosopher's meal problem is sometimes described with rice and chopsticks instead of pasta and forks, because it is obvious that two chopsticks must be used to eat rice.

Philosophers never talk, which is very dangerous and may cause a deadlock. Every philosopher holds a fork in his left hand and always waits for a fork in the right (or vice versa). Even if there is no deadlock, resource exhaustion may occur. For example, suppose that when a philosopher waits for another fork for more than five minutes, he puts down the fork in his hand and waits another five minutes before making the next attempt. This strategy eliminates the deadlock (the system will always move to the next state), it is still possible "took place live lock ." If the five philosophers enter the restaurant at exactly the same time and pick up the fork on the left at the same time, then these philosophers will wait five minutes, put down the fork in their hands, wait another five minutes, and pick up these meals at the same time cross.

In actual computer problems, the lack of a fork can be compared to the lack of shared resources. A commonly used computer technology is resource locking, which is used to ensure that resources can only be accessed by one program or one piece of code at a time. When the resource that one program wants to use has been locked by another program, it waits for the resource to be unlocked. When multiple programs involve locked resources, a deadlock may occur in some cases. For example, a program needs to access two files. When two such programs lock a file, they are both waiting for the other to unlock another file, and this will never happen.

 

Solution: (deadlock prevention)

  • Up to 4 philosophers are allowed to sit on the table at the same time.
  • Only when both chopsticks of a philosopher are available, he can pick them up (he must pick up two cowls in the critical zone).
  • Use an asymmetric solution. The single-numbered philosopher first picked up the left chopsticks, then the right chopsticks; the double-numbered philosopher first picked up the right chopsticks, then the left chopsticks.

 

 

Banker algorithm

From Baidu Encyclopedia

In the bank, the number of customers applying for loans is limited. Each customer must declare the maximum amount of funds required to complete the project when applying for the loan for the first time. When meeting all loan requirements, the customer should return it in time. Bankers should try to meet the needs of customers when the number of loans applied by customers does not exceed the maximum value they have. In this description, a banker is like an operating system, where funds are resources, and customers are equivalent to the process of applying for resources.

The banker algorithm is one of the most representative algorithms to avoid deadlock . In the method of avoiding deadlock, the process is allowed to apply for resources dynamically, but the system

Banker algorithm

Before the system allocates resources, it should first calculate the security of the allocated resources. If the allocation does not cause the system to enter an unsafe state, then allocate, otherwise wait. In order to implement the banker's algorithm, the system must set up several data structures .

To explain the banker's algorithm, you must first explain the operating system's safe state and unsafe state.

The safe sequence refers to a process sequence {P1, ..., Pn} is safe, that is, for each process Pi (1≤i≤n), the amount of resources it needs in the future does not exceed the current remaining resources of the system and all processes Pj (j <i) The sum of the resources currently occupied.

 

 

Realize the avoidance of deadlock by reasonably allocating resources

Let's look at a sample problem:

 

 

 

Published 519 original articles · praised 69 · 50,000+ views

Guess you like

Origin blog.csdn.net/qq_41286356/article/details/105466043
Recommended