Operating System-8 Deadlock and Interprocess Communication

1 Deadlock

1.1 Deadlock problem

Insert picture description here
In daily life: In
Insert picture description here
os:
Insert picture description here

1.2 Deadlock model

Demand side: The process represents the
required resources: CPU, memory unit, IO resources, and a shared variable. . . There can be n
resources used by each process as follows:

  • Idle resources, may be applied for may not be applied
  • Used state, other resources can not use this resource
  • Idle resource, when the process finishes using this resource, the resource is released

Insert picture description here
Insert picture description here
Insert picture description here
P i -> R j : The process needs resources
R j -> P i : The current resource is used by the process . 1 resource of
Insert picture description here
Insert picture description here
R 2 is given to P 1 , and the other resource is given to P 2 The only resource of
R 1 is given to P 2 , P 1 wants the resources of R 1 but can only wait (sleep) The only resource of
R 3 is given to P 3 , and P 2 wants the resources of R 3 but can only wait (sleep)
due to the process P 3 after a period of time After the resource is used up, it will be released, so there is no deadlock. After
Insert picture description here
adding 1 edge, P 3 needs R 2 resources (forming a ring)
. The typical feature of deadlock: the link relationship between the process and the required resources is formed ring
Insert picture description here
Insert picture description here
although there ring, but no deadlock, because once P 2 or P . 4If resources are released, P 1 or P 3 will get the resources and execute.
So there will be loops in the deadlock, but there may not necessarily be deadlocks if there are loops.
Insert picture description here

1.3 Deadlock characteristics

Insert picture description here
The 4 necessary conditions
for deadlock. Deadlock will cause the above 4,
but the above 4 does not necessarily cause deadlock.
Insert picture description here
Right P2 and P4 hold but do not wait

1.4 Deadlock handling method

Insert picture description here
Why is there a design that ignores this deadlock?
1. Recovering deadlock is very expensive.
2. Setting a series of constraints so that the os will not enter the deadlock state will greatly limit the function of the os, making the application and the function of the os unable to be fully executed, so it is ignored by assumptions The problem of deadlock, which is often used, has a small overhead

1.4.1 Deadlock Prevention

Break any of the 4 conditions for deadlock, as long as it is a reasonable break, there will be a deadlock prevention effect
Insert picture description here
. Mutual exclusion of access to resources will not be guaranteed, which will cause uncertainty in execution, which is not very good
Insert picture description here
at the beginning Occupy all the resources required by a process, but the interval between the process using each resource may be very large, so the efficiency of the system will be very low, and it may produce starvation
Insert picture description here
mutually exclusive resources. Using preemption will lose its meaning, so change it to Preemption also does not work.
Insert picture description here
Sort the resources and only apply for resources with increasing or decreasing sequence numbers to ensure that the process does not form a ring.
More used in submerged systems

1.4.2 Deadlock Avoidance

When applying for resources, judge whether it will cause a deadlock. If it can cause a deadlock, the resource will be used, and the system needs to have some additional prior information.
Insert picture description here
Insert picture description here
If the resource will not cause a deadlock after the resource is
allocated, the resource is allocated in an unsafe state. after may form a ring, it is unsafe condition
unsafe state includes a deadlock condition
assumptions turn P: according to a sequence of processes is performed successively safe condition i executed, then P 0 to P i. 1- after use resource release and none has been able to use resources to meet P i executed, all processes can be normal operation, does not deadlock
Insert picture description here
Insert picture description here

Banker's algorithm

Insert picture description here
Insert picture description here
It is too troublesome to judge the deadlock, so it is changed to judge whether it is insecure.
Insert picture description here
Security state detection algorithm:
Insert picture description here
Insert picture description here
banker algorithm:
Request vector: the number of each resource required by each process
Insert picture description here
. Can a secure sequence be found in the above figure?
Assuming that the four processes from P1 to P4 are not ended, see if there is an execution sequence that allows each process to end normally.
Find whether the Need of a process is less than the Available vector 0, 1, 1, and P2 are satisfied. Set finish[2] If it is true, P2 will be released after the resource is used up, and the (6, 1, 2) of P2 in A will be added to the V (0,
Insert picture description here
Insert picture description here
Insert picture description here
1, 1 ) of Available, which becomes: All 1 safe sequence is found: P2, P1, P3, P4
Insert picture description here
Assuming that P1 sends a request 1, 0, 1, will the banker's algorithm accept it? After
assigning 1, 0, 1 to P1, it
Insert picture description here
will be found that the available 0, 1, 1 does not meet the needs of any process, so the banker algorithm will not satisfy the request of 1, 0, 1 issued by P1

1.4.3 Deadlock Detection

Conditions relaxed

  • Deadlock avoidance means that there is no deadlock. Once it is found to be in an unsafe state, you cannot continue to apply for resources
  • Deadlock detection is to allow the system to enter a deadlock state. At a certain stage, it is judged whether there is a deadlock. Once a deadlock is found, the recovery mechanism is started. Moving the detection of deadlock to the system is no longer judging every time a request is made.
    Insert picture description here
    Insert picture description here
    Insert picture description here
    This algorithm is executed regularly to see if the deadlock in the system is
    expensive. It is necessary to know the maximum number of resources required by each process, so it is not It is often used
    Insert picture description here
    P0->P2->P1->P3->P4 to
    Insert picture description here
    Insert picture description here
    detect deadlock, it is necessary to consider which process to kill, there is no clear stipulation which process is difficult to select, so the detection algorithm is more used in the development phase

1.4.4 Recovery from Deadlock

Which process to kill, the scheme is as follows (terminate the process):
Insert picture description here
Resource preemption:
Insert picture description here

2 Inter-process communication (IPC)

Insert picture description here

2.1 Overview

Insert picture description here
Insert picture description here
(A): Indirect communication between processes
(b): Direct communication between processes. The
Insert picture description here
establishment of a link requires the support of os (os can access all computer resources to establish this path), because the link breaks the isolation and
Insert picture description here
Insert picture description here
Insert picture description here
blocking between processes : If you want to send a message, block it if you can't finish it. Only after sending it, go back and do the following.
Non-blocking: If you want to send a message, regardless of success or failure, this operation will return soon. There may be a big gap between the time of success and end and the time of send and return itself. This is the manifestation of asynchrony.

2.2 Inter-process communication method

If the message is stored in a cache, the main thing of the cache is to improve efficiency to avoid mismatches between the sender and the receiver (it may be sent quickly and received very slowly, or received very quickly and sent very slowly. ) Temporarily store the sender and receiver information that cannot be processed to improve efficiency.
Insert picture description here
If there is no data, the receiver has to wait
Insert picture description here

Signal (notification)

Insert picture description here
Signal is different from a hardware interrupt: the hardware lets the CPU handle it, and a hardware interrupt (interput) is generated. The signal is the software
os that sends a signal to the application for some reason. How does the application handle it?

  • catch:
  • ingore: no processing
  • mask:

It is flexible and efficient. The signal is only a small bite, which has the effect of notification and cannot transmit data. After being processed, it will directly return to the interrupted program to re-execute
. What kind of means does os provide to complete the above-mentioned functions?
Insert picture description here

  1. The application processes a single signal. When the program starts, register a handle for a signal and send this as a system call to os. The os will know that when a signal is generated, it will call the application. A signal processing function written by the program
  2. Once this signal is generated, how can os stop the currently running process and jump to the signal processing function to execute it? When the os receives this signal, it is in the kernel state. When it returns from the kernel state to the user state to respond to the signal which program should be prepared in advance, the return point should be changed to the entry of the signal processing function, combined with the system call It can be seen that he needs to modify the stack of the application software so that the next statement that was originally returned to the system call becomes the entry of the signal processing function, and the address to be executed after the signal processing function is used as the subsequent return address. This will make it return from the kernel to the execution of the application program, he will jump to the signal processing function entry to execute according to the remaining stack information, then return to the user mode, after the user mode of this function is executed, it will return to call it The function is interrupted, and then continue to execute. Generally, the stack of the user program will not be modified, and the Trojan horse will

Pipeline (data exchange)

Very early mechanism, the design of unix experts hope to implement each small program a single small function, and then combine them flexibly to achieve a complex function. The output of one program is directed to the input of another program. The
Insert picture description here
shell is a process. When this process receives ls | more, he knows that this is a combination of two commands, and then creates two processes-ls and more , And then he treats the output of ls with special processing, redirecting the output to a pipe (equivalent to a buffer in memory) instead of the screen, and then the more process takes data from the pipe. The
pipe is limited. If the ls is Output to the pipeline will be blocked if it is too late to fetch

Message queue (data exchange)

Message processing and pipeline

  • The pipeline is a channel established by the parent process for the child process. If there is no parent-child relationship between the processes, the pipeline will not work.
  • The data in the pipeline is a byte stream, which is not represented by structure
  • Message queues can support no parent-child relationship between processes
  • send\receive a structured data, which makes it more flexible and convenient to write complex programs. It
    Insert picture description here
    will also be full or empty

Shared memory

Insert picture description here
Each process can be seen
directly read and write memory to complete data sharing
Insert picture description here
How to achieve shared memory?
Map the same block of physical memory to the same or different address spaces of different processes

Guess you like

Origin blog.csdn.net/qq_42713936/article/details/105586542