Operating system (i) The critical section of the collaborative process some code understanding--peterson ' s Solution

Source: Internet
Author: User
Process Synchronization The critical-section problem(Critical area problem) Peterson ' s solution
Concurrent access to shared data could result in data inconsistency.
Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes.
Suppose that we wanted to provide a solution to the consumer-producer problem this fills all the buffers. We can do so by have an integer count of this keeps track of the number of full buffers. Initially, Count is set to 0. It is incremented by the producer after it produces a new buffer and was decremented by the consumer after it consumes a bu Ffer.
while (true) {  /* Produce an item and put in nextproduced  * *
          while (count = = buffer_size);//Do nothing
  buffer [in] = nextproduced;
               in = (in + 1)% Buffer_size;
               count++;
while (true)  {
            while (count = = 0);//Does nothing
                nextconsumed =  Buffer[out];
                 Out = (out + 1)% Buffer_size;
                      count--;

            /*  consume the item in Nextconsumed
    }

Here it is clear that there is a producer and a consumer, that is, the producers constantly put the data produced into the buff inside, consumers constantly looking for food from the buff. Then you have to face this empty problem, when the buff is full of circumstances, the producers of products can not be skimmed, similarly, when the buff is empty, the consumer can only hungry belly, so is the hunger State.

So, in the first program, "while (count = = buffer_size); Do nothing "only count is dissatisfied, can go down, otherwise, is in blind wait." And the design of the buff is a loop queue "team tail into the head out", all have to modulo the line statement. When the data is successfully skimmed into the buff, Count adds one.

The consumer process is the same, the first to determine whether it is empty, if not empty, take the data away, and then count minus one.

But the question comes, have you ever thought count plus one minus one, who first who after the problem?

Race Condition (Competitive conditions)

Race condition:the situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends on the particular order in which, the access takes place.

To prevent race conditions, concurrent processes must is synchronized. (synchronous)

Count++ could be implemented as     
Register1 = count     
register1 = register1 + 1     
count = Register1
count--could be implemented as     
Register2 = count     
register2 = register2-1     
count = Register2
Consider this execution interleaving (interleaved) with "Count = 5" initially:
    s0:producer Execute Register1 = Count   {Regi Ster1 = 5}
    s1:producer Execute register1 = register1 + 1   {register1 = 6} 
    s2:consumer Execute register2 = cou NT   {register2 = 5} 
    s3:consumer Execute register2 = register2-1   {register2 = 4} 
    s4:producer execute cou NT = Register1   {count = 6} 
    S5:consumer Execute count = Register2   {count = 4}

So by the code three can be seen, the result of Count has 4,5,6 three kinds of situation. So you have to consider the execution of the code in succession. This introduces the idea of mutual exclusion. But with the mutual exclusion is not everything, but also must meet the limited waiting conditions, there is a free to enter, also said a program can not be indefinitely blocked, the next program is in the future, resulting in waste of resources.

Mutual Exclusion -If process Pi is executing in its critical sections, then no other processes can be executing In their critical sections.
mutually exclusive conditions. Assuming that the process PI executes within its critical section, any other process will be excluded from its own critical section.

Progress -If No process is executing in its critical sections and there exist some processes that wish to enter t Heir critical section, then the selection of the processes that would enter the critical section next cannot be postponed I Ndefinitely.
Be free to enter. There is no process execution in the critical section, but some processes need to enter the critical section and cannot indefinitely extend the next waiting time to enter the critical section process.

bounded waiting -A bound must exist on the number of times this other processes is allowed to enter their Criti Cal sections after a process have made a request to enter its critical sections and before that request is granted.
Limited waiting. The number of times that other processes enter the critical section must be limited within a process when a request for entry into the critical section is made and when the request has been replied to.

General structure of process Pi does

{
            entry       section/* (ingress area) */
                Critical section/* (critical section)/
            exit        section/* (Exit Area) */
                reminder section/* (remainder) */
        } while (1);
Peterson ' s solutionThe process solution. Assume that the LOAD and STORE instructions is atomic; That's, cannot be interrupted. The processes (P0, P1) share and variables:
Int Turn; Boolean Flag[2]The variable turn indicates whose turn it's to-enter the critical section. The FlagThe array is used to indicate if a process are ready-to-enter the critical section. Flag[i]= True implies that process PiIs ready!
while (true) {
               flag[i] = true;
               turn = J;
               while (Flag[j] && turn = =  j);
                       CRITICAL section
               flag[i] = FALSE;
                       Remainder Section
       }

The above procedure can be understood, because the process of the synergy, you can imagine there are many and it is the same as the same program in the ongoing concurrent execution. Flag can be understood as a global lock, only one person can open, turn is the individual requirement of each local program, when and only the authority program has the need to enter (TURN=J) and the hand holds the key to open the lock (flag[j]!=true) to successfully enter the critical area. Flag[i]=flase to release resources. Add a bit more code to make it easier to understand.

while (true) {
               flag[j] = true;
               turn = i;
               while (Flag[i] && turn = =  i);
                       CRITICAL section
               flag[j] = FALSE;
                       Remainder Section
       }

Flag is a global variable, compared by this program and the previous program, it can be more intuitive, when the program opened the lock, then Flag[j]=true is no doubt, at the same time, the last program will be in this line program busy waiting while (Flag[j] && turn = = j). That's a good idea.

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.