Process mutually exclusive synchronization and communication deadlock [operating system]


Process mutually exclusive synchronization and communication deadlock [operating system]

(1) Mutual exclusion between processes

(1) Introduction of multithreading in cinema

Today’s problem is based on the concurrency of multiple threads, so I simply introduce today’s content through a Java multithreading example (today’s main topic is the process, here’s the multithreading problem, just experience the problems)

Add the sleep method to the sellticket class to delay the thread and the execution speed

public class SellTickets implements Runnable {
    //Movie votes
    private int ticket = 10;

    public void run() {
        while (true){
            if (tickets > 0){
                try {
                } catch (InterruptedException e) {
                                   +"Tickets --" + "tickets are being sold";

I have only defined 10 votes for the purpose of space. I can set a little more for my own test to see the result better

public class SellTicketsTest {
    public static void main(String[] args) {
        //Create resource object
        SellTickets st = new SellTickets();

        //Create thread object
        Thread T1 = new Thread (st, "window 1");
        Thread T2 = new thread (st, "window 2");
        Thread T3 = new thread (st, "window 3");

        //Start thread

Execute the test code to see the results

Window 3 is selling ticket 10
Window 2 is selling ticket 8
Window 1 is selling ticket 9
Window 3 is selling ticket 7
Window 2 is selling ticket 5
Window 1 is selling ticket 6
Window 2 is selling ticket 4
Window 3 is selling ticket 3
Window 1 is selling ticket 3
Window 2 is selling ticket 2
Window 1 is selling ticket 1
Window 3 is selling ticket 1

Only through 10 tickets, we can see the problem. First, the order of tickets is disordered, which should be 10, 9, 8… 2, 1, but there are 10, 8, 9 problems. Second, for example, the tickets of Chapter 1 and Chapter 3 have been sold many times, which is obviously unreasonable. If the number is large, there may be negative tickets sometimes. For specific reasons, you can go to see me Introduction to multithreading in java basic tutorial

Let me briefly PICK:

Thread 1 may execute at the same time as thread 2, so it may cause two threads to be squeezed into the middle of the original value and minus 1 process when reading tickets. This is the problem of duplicate tickets. In the final analysis, it is because multiple threads access the variable ticket without control

But how can we solve this problem? Of course, there are many ways. We will also introduce them later. One of the common ways in Java is to lock

The following is an example of lock lock. Of course, there are other locks, such as synchronous lock, etc. but the specific implementation in Java is not what we want to talk about in this article. Let’s take a look at the effect here

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SellTickets2 implements Runnable {

    private int tickets = 10;

    private Lock lock = new ReentrantLock();

    public void run() {
        while (true) {
            try {
                if (tickets > 0) {
                    try {
                    } catch (InterruptedException e) {
                    System. Out. Println (thread. Currentthread(). Getname() + "the" + (tickets -- + "tickets are being sold");
            } finally {

Do it

Window 1 is selling ticket 10
Window 1 is selling ticket 9
Window 2 is selling ticket 8
Window 2 is selling ticket 7
Window 2 is selling ticket 6
Window 2 is selling ticket 5
Window 2 is selling ticket 4
Window 2 is selling ticket 3
Window 2 is selling ticket 2
Window 2 is selling ticket 1

As expected, the above problems have been solved

The same is true for processes. During the concurrent execution of two processes, if a process shares variables (for example:ticket)If the access is not finished completely and another process starts to access, data conflicts will occur. Shared variables like this allow only one process to access at a time,When two processes use this shared variable, there is a competitive relationship, that is, the mutual exclusion between processes。 If this kind of mutual exclusion is not considered in the process of concurrent execution, and thus is not effectively controlled, problems will occur

(2) Mutual exclusion

One or more program segments in a group of concurrent processes must be executed in a unit that does not allow cross execution because they share a public resource
Two or more processes sharing the resource are not allowed to enter the critical area at the same time

Because each process needs to share resources, and some resources need to be mutually exclusive, that is, multiple processes cannot use the same resource at the same time, so each process competes to use these resources. This relationship of processes is mutually exclusive

(3) Critical section

A program that does not allow multiple concurrent processes to cross execute is called a critical area

Some resources in the system can only be used by one process at a time. Such resources are critical resources or mutually exclusive resources or shared variables

(4) Access process of critical area

These nouns will be used by default when introducing mutually exclusive methods~

  • Access area: before entering the critical area, check whether a piece of code can enter the critical area, and if so, setAccessing critical areasign
  • Critical area: a piece of code in the process that accesses critical resources
  • Exit zone: used toAccessing critical areaFlag delete
  • Residual area: rest of code

(5) Critical zone criterion

  • Give way when you have time: when there is no process in the mutex, any process authorized to use the mutex can enter
  • No waiting: two or more processes are not allowed to enter the exclusive zone at the same time
  • Choose one from manyWhen there are no processes in the critical area and multiple processes require to enter the critical area at the same time, only one of them can enter the critical area, and other processes must wait
  • Limited waiting: any requirement to enter the mutex should be met in a limited time
  • Let the right wait: the process in the waiting state should give up CPU occupation
  • Equal competition: any process does not have the right to stop the operation of other processes. There is no rigid regulation on the relative running speed between processes

(2) The way to realize mutual exclusion with lock

(1) Single sign method

The basic idea is to set up a common integer variable, turn, to describe the process identifier that allows access to the critical area

Check the turn before entering the critical area. If it is equal to the process number, you can enter the critical area. Otherwise, the cycle waits, so mutual exclusion can be realized

Process mutually exclusive synchronization and communication deadlock [operating system]

This diagram is simple to say, for example, process PI comes first, and now due to turn = iSo it doesn’t go into the cycle, it goes directly into the critical area. If PJ wants to come in at this time, but becauseturn = i So it can only enter the cyclewhile(tuen != j)Until process PI comes out of the critical area, set trun to j again, and PJ can enter the critical area


  • Processor time is spent waiting
  • The two processes use the processor alternately, and the execution speed depends on the slow process
  • If one terminates (both inside and outside the critical zone), the other will be blocked forever

(2) Double sign method (check first)

Double flag is to set a flag array flag []: to describe whether the process is in the critical area. The initial value is false, indicating that the process is not in the critical area

Its basic idea is:

  • Check first, then modify: check whether another process is in the critical area in the entry area, and modify the mark of this process in the critical area when it is not;
  • Modify the mark of this process in the critical area in the exit area

Process mutually exclusive synchronization and communication deadlock [operating system]

Advantages: no alternative access, can be used continuously;

Disadvantages: PI and PJ may enter the critical zone at the same time. When both PI and PJ are not in the critical area, it is assumed that when two parts of PI PJ enter the critical area concurrently, they will enter the critical area at the same time. That is, after checking the other party’s flag, a process switch occurs, and the results pass the check. The problem here is that inspection and modification cannot be carried out continuously

(3) Double sign method (post inspection)

Process mutually exclusive synchronization and communication deadlock [operating system]

Algorithm 3 is similar to algorithm 2. The difference between algorithm 2 and algorithm 3 is that it is modified first and then checked. It can prevent two processes from entering the critical stage at the same time

Battery limit. The disadvantage is that both PI and PJ may not enter the critical region. When both PI and PJ are not in the critical region, assume that “pi (first step)”

PJ (step 1) PI (step 2) PJ (step 2) “when executing in sequence and concurrently, the critical area will not be entered, that is, it will occur after the flag of entering the critical area is set

Process switch, result check failed

(4) Improved double sign method

Process mutually exclusive synchronization and communication deadlock [operating system]

Algorithm 4 combines algorithm 1 with algorithm 3, which is the correct algorithm. When the flag is modified at the same time, the flag turn is used to describe the accessible process

The main idea is to modify before checking in the entry area, and check the sequence of concurrent modifications

  • Check the other party’s flag. If it’s not in the critical area, enter it by yourself – if it’s idle, enter it
  • Otherwise, check turn again: if a later assignment is saved, the later process will wait, and the earlier process will enter, that is, first come first, then wait

(3) The way to realize mutual exclusion of semaphores

The previous mutual exclusion algorithms have problems. They areA consultation mechanism between equal processes, need aA manager above the processTo solve the use of public resources. OS can deal with mutual exclusion from the perspective of process manager,SemaphoreIt is an effective means provided by OS to manage public resources. The value of the semaphore represents the number of available resource entities

Except one integer value for each semaphore s s.count (count), there is also a process waiting queue s.queue , which stores the identification of each process blocked in the semaphore.

Semaphores can only be accessed through initialization and two standard primitives (P, V primitives)——As the core code of OS, execution is not interrupted by process scheduling

The semaphore is assigned a non negative integer value at initialization, which represents the total number of idle resources (also known as “resource semaphore”)

During process execution, the value of semaphore (i.e. its count value) may change

  • If it is non negative, it indicates the current number of idle resources
  • If it is negative, its absolute value represents the number of processes currently waiting for critical area

(1) P primitive

In the case of mutual exclusion,Apply for critical resourcesP primitive is used for tense, and its implementation principle is as follows:

P(Semaphore s) {
    --s. Count; // indicates to request a resource;
     If (s.count < 0) {// indicates that there are no idle resources;
        Calling process enters the waiting queue s.queue ;
        Blocking the calling process;

(2) V primitive

V primitive usuallyWake up process wait for the first process in the queueThe principle is as follows:

V(Semaphore s ) {
    ++s. Count; // to release a resource;
    If (s.count < = 0) {// indicates that a process is blocked;
        Take a process P from the waiting queue s.queue;
        Process P enters the ready queue;

(4) Difference between lock method and semaphore method

Lock method Semaphore method
1. Locking process can be interrupted Using P and V primitives
2. Loop detection lock, high overhead, Low system overhead
3. No queuing mechanism for processes not entering critical area Processes that do not enter critical areas must wait in the wait queue

(5) Process synchronization

(1) Basic concepts

Process synchronization: SetAsynchronous environmentNext groupConcurrent process, becauseDirect restrictionandSend cancellation to each otherOn the wayMutual cooperation and waiting, andThe process of making processes execute in a certain order and speed

Cooperation process: a group of processes with a synchronization relationship

news: for signals sent by cooperative processes to each other, the following procedures can be used:

  • Wait (message name): indicates that the process is waiting for a message from the cooperation process
  • Signal (message name): indicates to send a message to the cooperation process

(2) Semaphore classification

private semaphore : you can also treat messages sent between processes as semaphores

Common semaphore: the semaphores used in mutual exclusion are called common semaphores

(6) Classic mutual exclusion synchronization problem

(1) Producer consumer issues

Problem Description:Several processes exchange data through a limited shared buffer. Among them, the “producer” process keeps writing,

The “consumer” process keeps reading; there are n shared buffers in total; only one process can share buffers at any time


  • At any time, only one process can operate on the shared buffer. It can be seen that there are mutually exclusive relationships between producers, producers and consumers, and consumers and consumers using the shared buffer.
  • If the buffer is not enough, the producer can write it; if the buffer is not empty, the consumer can read it. It can be seen that there is a synchronous relationship between the producer and the consumer.

(2) Reading and writing problems

Problem Description:For the read-write operation of shared resources, at most one “writer” is allowed at any time, while “reader” is allowed

Multiple, requirements:

  • “Read write” mutual exclusion
  • “Write write” is mutually exclusive
  • Read read allow

(3) The dining problem of philosophers

Dining conditions

If philosophers want to eat, they first ask for food

After putting forward the request of eating, and getting two pairs of chopsticks, we can eat. If chopsticks are obtained by others, you must wait for the person to finish eating before you can obtain chopsticks

For any philosopher who has applied for a meal, he will not put down his chopsticks before he has two chopsticks to eat

At the beginning of the meal, only two philosophers are allowed to ask for a meal

The question to be considered is how to ensure that philosophers’ actions are carried out in an orderly manner? For example:

  • There is no requirement for meals by neighbors at the same time;
  • No one can never get chopsticks

Try to answer these questions:

(1) This paper describes a communication algorithm to ensure that there will be no need for two neighbors to eat at the same time.

(2) Describe an algorithm in which no two neighbors eat at the same time and no one starves (never get chopsticks).

(3) Under what circumstances are all five philosophers unable to eat?

Answer the first question:

(1) First of all, 5 chopsticks are critical resources, so five philosophers, philosopher (I), need to exclude each other when taking chopsticks. In order to meet the first point, they need to take chopsticks in a certain order

Let the public semaphore fork [i] have an initial value of 1, and the value of I is 0-4. It is required to take the right one first, and then the left one when I = 4. The left chopsticks are (i + 1) mod 5Equal to 0

The algorithm results are as follows:

i = 0,1,2,3,4
            P(fork[(i + 1) mod 5])
            V(fork[(i + 1) mod 5])

The above code can guarantee that there will not be two adjacent philosophers eating at the same time, but it may cause deadlock. If five philosophers are hungry and all pick up the left chopsticks at the same time, they will make the five signal quantities chopstick equal to 0. When they try to pick up the chopsticks on the right hand, they will have no chopsticks and fall into infinite waiting

Answer the second question:

Deadlock prevention strategy 1

(2) In this case, only one philosopher needs to pick up chopsticks in reverse order

i = 0,1,2,3
            P(fork[i + 1])
            V(fork[i + 1])

The first three are still to take the right first, then the left, and the last one is to take the left first, then the right


Answer the third question:

(3) In the second case, the phenomenon of starvation is solved. In the third case, it occurs in the first case, that is, when every philosopher takes the chopsticks on the left and tries to take the chopsticks on the right, there will be five philosophers who can’t eat

Of course, there are many ways to prevent deadlock. If you are interested, you can write your own specific code to try, and refer to some good algorithms of others

(7) Process communication

(1) Classification

There are four ways of process communication

A: Master-slave

Master process is free to use slave process resources

The action of the slave process is limited by the master process

Fixed relationship between master process and slave process

Applied to terminal control process and terminal process

B: Conversational

The user process must be licensed before using the services provided by the service process

The service process provides services according to the requirements of the using process. The single control belongs to the service process itself. The relationship between the using process and the service process is fixed

Apply to user processes and disk management

C: Message or mailbox mechanism

The sending process can send messages as long as there is an empty cache or mailbox

No direct connection between sending process and receiving process

There is a buffer or mailbox between the sending process and the receiving process to store the transferred messages

D: Shared memory mechanism

The shared memory mode does not require data movement, and two processes that need mutual information communicate with each other through the operation of the same shared memory

(2) Message buffer mechanism

The system sets a set of buffers in the operating system space, where each buffer can hold a message

When the sending process needs to send messages, execute the send system call. The operating system allocates an empty buffer for the sending process, copies the sent messages from the sending process to the buffer, and then connects the buffer containing messages to the end of the message chain of the receiving process, thus completing the sending process

At a later time, when the receiving process executes the receive primitive, the operating system takes the buffer containing the message out of the message chain, copies the message content to the receiving process space, and then retracts the buffer, thus completing the message reception

Since the buffer used in the message buffer mechanism is a public buffer, when using the message buffer mechanism to transfer data, the communication process should meet the following conditions:

Process operations on buffers must be mutually exclusive
When no messages exist in the buffer, the receiving process cannot receive any messages
Set public semaphore mutex, receive process private semaphore SM, message M

Send (m):
Request a message buffer from the system
Send message M to message buffer of new request

Copy message m from buffer to receive and release buffer

(3) Email correspondence

For a mailbox used by only one sending process and one receiving process, inter process communication shall meet the following conditions:

  • When the sending process sends a message, there is at least one unit in the mailbox that stores the message
  • At least one message in mailbox exists when receiving process receives message

(4) Pipeline

UNIX system starts from system V and provides two data communication modes, named pipeline and nameless pipeline
Anonymous pipeline provides a communication channel for the process and its subprocesses to transmit messages in bitstream mode. The pipeline is a pipeline file in logic and a cache of the file system in Physics

Pipeline is a communication channel which is transmitted by byte stream between processes. It is implemented by the buffer (usually tens of kilobytes) of OS core, which is unidirectional. Before using the pipeline, the corresponding pipeline shall be established, and then it can be used.

In UNIX system, two file descriptors are created by the pipe system call to write and read. The specific call forms are:

  • int pipe(int fildes[2]);

Where, the file descriptor Fildes [0] is the read end, and Fildes [1] is the write end

When communicating, write and read the pipeline through the system call write and read

If you need two-way communication between processes, you usually need two pipes.

UNIX nameless pipeline is only applicable between the parent and child processes or between the child processes arranged by the parent process (only related processes can share nameless pipeline);

Named pipes in UNIX can be established through mknod system call (unrelated processes can only share named pipes and specify mode as s ﹤ ififo). The specific form is as follows:

  • int mknod(const char *path, mode_t mode, dev_t dev);

    example 1Using unnamed pipeline to realize parent-child process communication

#include <stdio.h>
main() { 
    int x,fd[2];
    char buf[30],s[30];
     if(x==0) { 
        sprintf(buf, “This is an example\n”);
     } else { 

(8) Deadlock

(1) Basic concepts

Definition of deadlockWhen multiple processes are deadlocked due to competition for resources, they will never move forward without external force. This phenomenon is called deadlock

Cause of deadlock: the resource competition and process promotion order of concurrent processes are improper

(2) Generating conditions

  • mutual exclusion : exclusivity of resources
  • Conditions of non deprivation: the process can not be deprived of the right to use the obtained resources by other processes before they are used up
  • Partial distribution conditions: each time a process requests a new resource, it also takes up the allocated resource
  • Loop conditions: there is a process loop chain, and the resources obtained by each process in the chain are applied by the next process at the same time

(3) Elimination of deadlock

A: Deadlock prevention

  • One off distribution
  • Sequential allocation of resources
  • Release before application

B: Deadlock avoidance:

  • Dynamic prevention: the system predicts the possibility of deadlock when allocating resources dynamically according to some algorithm, and prevents it

C: Detection and release of deadlock

  • A given process resource graph is ultimately reducible
  • Deprivation of resources
  • Cancel advance

(9) End

If there are any deficiencies in the article, welcome to leave a message and exchange, thank you for your support!

If you can help, then pay attention to me! If you love the way WeChat reads, you can pay attention to my official account.

We don’t know each other here, but we are working hard for our dreams Mei

An official account of the article that persists in pushing original development technology: ideal two days

Process mutually exclusive synchronization and communication deadlock [operating system]