1. Producer consumer issues
1.1 summary
2. Multi producer multi consumer problem
2.1 summary
3. Smokers
3.1 summary
4. Reader writer questions
5. Dining problem of philosophers
Deadlock may occur
Prevent deadlock
Disadvantages of the third method
There may be two critical resources required by a process that are available but not available and then blocked, or a process may only get one critical resource and then be blocked
5. Tube side
5.1 why is the tube side introduced
5.2 definition and basic characteristics of pipe pass
5.3 solving the problems of producers and consumers by pipe process
5.4 mechanism similar to pipe process in Java
5.5 summary
6. Concept of deadlock
6.1 deadlock, starvation and dead cycle
6.2 necessary conditions for deadlock generation
6.3 when will deadlock occur
6.4 deadlock handling strategy
6.5 summary
7. Deadlock prevention
7.1 breaking mutually exclusive conditions
7.2 destruction of inalienable conditions
7.3 damage request and retention conditions
7.4 failure cycle waiting conditions
After the process has applied for a large number of critical resources, it can no longer apply for a small number of critical resources, breaking the dead chain of the process application resource cycle
7.5 summary
8. Avoid deadlock
8.1 safety sequence
The banker algorithm can be used more easily in practical calculation
9. Deadlock detection and release
9.1 deadlock detection
9.2 deadlock release
9.3 summary
summary
1. Talk about the synchronization problem you know about the application of semaphore mechanism?
Producer consumer issues
//Mutual exclusion should be attached to the access to critical resources, otherwise deadlock will occur //The code segments of producers and consumers that do not involve the operation of critical resources are written outside the PV operation, so as to improve the concurrency of the process semaphore mutex = 1; semaphore empty = n; semaphore full = 0; public void Producer(){ while(1) { Produce a product; P(empty); P(mutex); Put product into buffer; V(mutex); V(full); } } public void Consumer(){ while(1) { P(full); P(mutex); Take the product out of the buffer; V(mutex); V(empty); Consume a product; } }
Multi producer multi consumer problem
// The reason why mutex is not set here is that the critical resource size is 1, so two or more processes will not access the critical resource // Note that only one Plate is set for the semaphore type variable here, because after the son and daughter access the critical resources, the father and mother can start giving birth. semaphore apple = 0; semaphore banana = 0; semaphore plate = 1; public void Dad(){ while(1){ peel an apple; P(plate); Put the apple on the plate; V(apple); } } public void Mom(){ while(1){ Peel bananas; P(plate); Put the bananas on the plate; V(banana); } } public void Son(){ while(1){ P(apple); Take the apple out of the plate; V(plate); Eat apples } } public void daughter(){ while(1){ P(banana); Take out the bananas from the plate; V(plate); Eat bananas; } }
Smoker Problem
semaphore offer1 = 0; semaphore offer2 = 0; semaphore offer3 = 0; semaphore ashtray = 1; int i = 0; public void offer(){ if(i == 0){ production offer1; P(ashtray); take offer1 Put in the ashtray; V(offer1); } else if(i == 1){ production offer2; P(ashtray); take offer2 Put in the ashtray; V(offer2); } else if(i == 2){ production offer3; P(ashtray); take offer3 Put in the ashtray; V(offer3); } i = (i+1)%3; } public void smoker1(){ P(offer1); Take it out of the ashtray offer1; V(ashtray); use offer1; } public void smoker2(){ P(offer2); Take it out of the ashtray offer2; V(ashtray); use offer2; } public void smoker3(){ P(offer3); Take it out of the ashtray offer3; V(ashtray); use offer3; }
2. Talk about the deadlock you know? What are the differences between deadlocks, thread starvation, and dead loops?
The possible conditions for deadlock are: improper allocation of resources to various processes, improper use of semaphores, conflict between mutually exclusive operations before synchronization operations, and the order in which processes apply for resources. The conditions for deadlock generation must meet four points: 1. Apply for mutual exclusion of resources (solved by spooling technology to make mutually exclusive resources virtual and non mutually exclusive). 2. Circular waiting (break the circular waiting chain so that the process that can apply for resources at most is less than N-1, etc.). 3. Inalienable conditions (the process with high priority can be forcibly deprived of the process with low priority, which will lead to the starvation of the process with low priority, and the progress of the process deprived of critical resources will be wasted). 4. Request and hold conditions (apply for all necessary critical resources before the processor on the process enters the running state, which will reduce the system concurrency.) breaking any of the above conditions can statically prevent deadlock. Dynamic deadlock avoidance: the banker algorithm assumes that the critical resources are allocated to some resources to see whether the system sequence will enter an unsafe state to determine whether to allocate the critical resources. Deadlock release: deadlock is detected by whether the allocation edge of critical resource -- > process and the request edge of process -- > critical resource can be completely simplified. (give priority to processes with low priority, few critical resources applied, short running time, and small time from being added to the ready queue to the current time to give up critical resources for simplification.). The methods to remove the deadlock include the resource deprivation method (hanging the deprived resources to the external memory may lead to the hunger of the suspended process), the process revocation method (forcibly revoking some or all processes may lead to the end of some processes, but all previous efforts will be wasted), and the process fallback method (to make some processes fall back to a state sufficient to avoid deadlock, you need to set a series of restore points for process entities)
Deadlock occurs between two processes. It is caused by the improper sequence of critical resources allocated by the operating system to the process. It can only occur in the blocking state. Thread starvation occurs in a single process. It is a problem caused by a single process that can not run and complete. It can occur in the blocking state where the processor and required resources are not available, or in the state where the processor is not available Ready state. A deadlock occurs when the programmer edits the code or intentionally uses the loop. Deadlocks and thread starvation occur in the processing of processes by the operating system (manager level), and deadlocks occur in processes managed by the operating system (manager level)