Understand mutual exclusion and synchronization through a topic

Preface

In the process of learning, mutual exclusion and synchronization are the most important content, but due to its excessive abstraction, the use of complex semaphores makes students daunting. The purpose of this blog is to explore mutual exclusion and The most essential feature of synchronization. Due to the limited level of bloggers, I also urge everyone to criticize and correct me.


Topic: Is the algorithm for determining the following questions correct? If it is not correct, how to correct it?

Answer and analysis

The first question: synchronization issues


answer

The algorithm in the figure above is wrong. If process A runs first and the amount of information to be stored in the buffer is large, then every time new information is stored, the previously stored information will be lost (only one message can be stored at a time), so When you find a way to store a message in process A, process B will continue to run and take away the message stored in process A.
Then what we need to do is clear. In the question, process A " writes " and process B " reads ", indicating that there is a cooperative relationship between them. Then this is a synchronization problem.

The problems to be solved are as follows:
① Process A must be run first, so as to ensure that there is information available when process B is running;
② Process A can only store one message at a time.

correct answer:

So why is the correct answer like this? Please see the analysis.

Anatomy

First of all, setting empty=1,full=0, why, can be understood like this: empty=1 means the buffer is 空的, full=0 means the buffer 不是满的. (Assumed in this example 非空=满)

When process A is executed, empty becomes 0and full becomes 1, and then process B can be executed (full=0 when process A is not executed, at this time the code P(full) in the process cannot be executed, so the buffer cannot be accessed ).

We can see that in synchronization , mutual exclusion is actually included , and the same semaphore is generally placed in different processes, so that a certain task can be completed cooperatively through common access to a shared resource .

Therefore, when implementing synchronization, pay attention to at least one semaphore is placed in different processes, because at least two people must achieve the so-called collaboration , and since they want to cooperate, they must all have something in common. (Here refers to the semaphore), through the operation of this kind of thing to achieve collaborative completion of a certain work.

The second small question: mutual exclusion problem

answer

The algorithm in the figure above is wrong. The question stem mentioned that A and B are concurrent processes, and they share a critical resource, indicating that A and B are competitive and there is no cooperative relationship between them, so it can be judged that this is a mutually exclusive problem.

correct answer:

Anatomy

First, set the amount of information mutex(you can name it arbitrarily), the default value is 1(the most important point, unless special circumstances, the amount of mutually exclusive information is set to1 ).

At this time, because A and B are executed concurrently, the order of execution of A and B is uncertain due to uncertainty. No matter which process is executed first, assume that A is executed first. When A executes P (mutex) , mutex becomes 0. In this way, process B will not be able to access critical resources. When the code of process A is executed, execute V (mutex) , mutex becomes 1, at this time, all processes can compete for access to critical resources again (process A can also continue to compete, because of the uncertainty of the operating system, the execution order of each process is uncertain).


Finish.

Guess you like

Origin blog.csdn.net/weixin_43655282/article/details/106153237