Process Mutex and synchronization

Source: Internet
Author: User
Tags mutex semaphore

One, process mutual exclusion

First of all, we need to know that in order to ensure the correctness of the final results of the program execution, the concurrent execution of the various processes must be constrained to control their speed of execution and competition for resources. Need to return to a question, which parts of the program need to be constrained in order to ensure the correctness of its execution results? such as: Process PA and PB shared memory MS, process PA and PB each execute two statements, may be executed in the following order: When the process PA executes the first statement, the execution process PB, and then executes the second statement of the process Pa. This can result in an error. How is the correctness guaranteed? We can abstract each of the two statements into two sequential execution units in a single action, so that the correctness of the execution results can be guaranteed. This also leads to a concept.

Critical section (critical region): The extent to which multiple concurrent processes are not allowed to cross-execute is called a critical section. A critical section is caused by shared common data or public data variables that are part of a program segment that belongs to a different concurrent process.

The phenomenon that the concurrent process is not allowed to cross execution in a critical region due to sharing of a public resource is called indirect restriction of the execution speed of concurrent process, which is caused by shared public resources .

1. What is mutual exclusion?

One or more program segments in a set of concurrent processes that cause them to be executed in a unit that does not allow cross-execution because they share a public resource . That is, not allowing more than two concurrent processes that share this resource to enter the critical zone at the same time is called mutex.

The following guidelines must be met when a set of concurrent processes mutex execution:

i) the relative execution speed of each concurrent process cannot be assumed. That is, the concurrent processes have the right to compete equally and independently of the public resources.

II) When a process in a concurrent process is not in a critical section, it does not prevent other processes from entering the critical section;

III) When a number of processes in the concurrent process request entry into the critical section, only one process can be allowed to enter;

IV) a process in the concurrent process starts when the application enters the critical section and should be able to enter the critical section within a limited time.

Guideline iv) is an important guarantee that a concurrent process does not deadlock.

2, mutual exclusion of lock implementation

Is it just a matter of arranging the individual processes in the critical section at different times? Not really. Because the randomness of the user program's execution start is unknowable.

One possible approach is to lock the critical section to achieve mutual exclusion. When a process enters the critical section, it locks the critical section until it exits the critical section. When the concurrent process requests entry into the critical section, the first test is whether the critical section is locked. If the critical section is locked, the process waits for the critical section to be unlocked before it is possible to obtain a critical section.

3. Signal volume and P, v primitives

3.1 Using the lock to implement an issue of mutual exclusion between processes:

i) if a set of concurrent processes has a large number of processes, and because each process has to test the lock location when it requests to enter the critical section, the overhead is significant .

II) In addition to the use of lock to achieve inter-process mutex, will also lead to unfair phenomena in some cases, such as: to the Process PA and Pb, the process pa after execution and unlock, immediately after a turn statement call Process PA, because the steering dispatch statement instantaneous execution, so that the process PA The possibility of transferring the processor to process PB is very small. As a result, process PB will be in permanent starvation state.

For the II) Why does this problem occur? Because in the locking method to solve the problem of mutual exclusion, a process can enter the critical section is dependent on the process to call the lock process to test the corresponding lock positioning, that is, each process can enter the critical area is determined by their own test, so that there is no chance of implementation of the process of course can not be judged, resulting in unfair phenomenon.

We can use semaphores to manage the public resources of the corresponding critical sections, which represent the available resource entities. The number of signals can only have p, v primitive operation change, SEM is the signal volume, a p primitive operation makes the signal volume SEM minus 1, a v primitive operation will make the signal volume SEM plus 1.

Note: When a process is executing in a critical section, if another process performs a p primitive operation, the process does not want to move lock when it returns to the lock starting point because it cannot enter the critical section, waiting for the test to be re-executed later. Instead of waiting in the queue to wait for other processes to do the V element to release the resource, enter the critical section, at which point the execution of the P primitive is really over.

The main actions of 3.2 P primitives are as follows:

A) SEM minus 1;

b) If the SEM is still greater than or equal to 0 after minus 1, the P primitive returns and the process continues to execute;

c) If the SEM minus 1 is less than 0, then the process is blocked into the corresponding queue, and then the process scheduling.

The main actions of the V primitive are as follows:

A) SEM plus 1;

b) If the addition result is greater than the 0,v primitive, the process returns to the call point and continues execution;

c) If the sum result is less than or equal to 0, a wait process is awakened from the waiting queue of the signal and then returned to the original process to continue or go to process scheduling.

3.3 Why should the P and V processes be implemented in the original language?

If not, multiple processes invoke p or V operations at the same time, it is possible that the V operation starts executing when the p operation has just finished sem-1 and the corresponding process is not fed into the wait queue, so that the V operation will not be able to discover the wait process and return. Therefore, p, V operations must be implemented in the primitive language, and the P, V primitives are not allowed to occur during the execution of the interrupt.

4, using the P, V primitives to achieve the process of mutual exclusion

Signal SEM is used for mutual exclusion of the semaphore, and its initial value of 1 means that no concurrent program uses the critical section. When a process wants to enter a critical section, it must first perform a P primitive operation to reduce the amount of the semaphore SEM by 1. After a process completes the operation of the critical section, it must perform a V primitive operation to release the critical section it occupies.

Since the semaphore initial value is 1, any process will change the value of the SEM to 0 after performing the p primitive operation, indicating that the process can enter the critical section. If another process wants to enter the critical section before the process does not perform the V primitive operation, it should also perform the P primitive operation, so that the value of the SEM becomes-1, so the second process will be blocked. Until the first process performs a V primitive operation, the value of the SEM changes to 0, which wakes the second process into the ready queue and then enters the critical section after dispatch. After the second process performs the V primitive operation, the SEM reverts to the initial value if no other process has requested to enter the critical section.

Second, process synchronization

1. Process synchronization

A group of concurrent processes in an asynchronous environment, each execution result is mutually executing condition , thus restricting the process execution speed process is called the direct restriction between the concurrent processes. The asynchronous environment here mainly refers to the randomness of the execution start time of each concurrent process and the independence of execution speed.

One of the simplest and most intuitive methods is the direct restriction of the process to each other to send a signal that the execution condition already has.

The process of executing a set of concurrent processes in an asynchronous environment by directly restricting each other and sending messages to each other, which makes each process perform at a certain speed, is called process synchronization.

2. Private signal Volume

Unlike the process mutex, the signal volume here is only related to the process of restricting the process and the process of being constrained.

3. Producer-Consumer issues

Generalize the synchronization and mutex problems of concurrent processes, and get an abstract general model, that is, producer-consumer problems.

The process of using a class of resources in a system is called a consumer, and the process of releasing the same kind of resources is called the producer of that resource.

Process Mutex and synchronization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.