Use PV operations to resolve synchronization mutex issues between processes

Source: Internet
Author: User
Tags mutex semaphore

No1, the concept of process synchronization:

In the multi-channel program environment, the concurrent execution process exists in the different mutual restriction relations, for example, calculates the 1-1*1 the formula, the system will produce two processes, one is the addition process one is the multiplication process. But in order for the result to be correct, the multiplication process is preceded by the addition process, but there is asynchrony in the actual operation, and we cannot ensure that the multiplication is performed before the addition. Therefore, a certain mechanism must be established to constrain

Synchronization, also known as a direct constraint relationship, refers to the multiple processes that are established to accomplish a certain task, and these processes need to coordinate their work order in some locations to wait for and transmit information, stemming from mutual cooperation.

Mutual exclusion is called indirect constraint, which means that when a process enters a critical section using a critical resource, another process must wait for the process that occupies the critical resource to exit before it is allowed to access this critical resource.


No2, Classic sync problem

1, producer-consumer issues.

Description: A set of producer processes and a set of consumer processes that share an initially empty, size n buffer, and only when the buffer is not full can the producer put the message into the buffer, otherwise it must wait. Only when the buffer is not empty can the consumer remove the message from it, and the person must wait. Since the buffer is a critical resource, it allows only one producer to put messages or a consumer out of the message.

Analysis:① producers and consumers are mutually exclusive access to the buffer zone, and it is a collaborative process that can only be taken out by the producer in the consumer. ② arrangement idea, two processes exist mutually exclusive and synchronous relationship, need to resolve mutually exclusive and synchronous PV operation location. ③ Semaphore set, the mutex as a mutex, used to control the mutex access buffer, the semaphore initial value is 1, set the semaphore full to record the current buffer "fill" the number of buffers, the initial value of 0, set the semaphore empty to record "null" buffer number. The initial value is N.

Semaphore Mutex=1;
Semaphore Empty=n;
Semaphore full=0
producer () {
      while (1) {
          produce a item in NEXTP;
          P (empty);     Gets the buffer unit
          p (mutex);      Enter the critical section
          add nextp to buffer;
          V (mutex);      Leave the critical section and release the mutex signal
          V (full);   Full buffer number plus 1
       }
}
consumer () {
      while (1) {
            p];       Gets the full buffer unit
            p (mutex);  Enter the critical section
            remove an item from buffer;
            V (mutex);  Leave the critical section and release the mutex signal
            V (empty);  Number of empty buffers plus 1
            consume the item;
       }
}    


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.