Computer operating system Notes (5)--synchronization problem of process Management classic process

Source: Internet
Author: User
Tags mutex semaphore
one producer – consumer issues

Both the producer process and the consumer process run asynchronously, but they must be kept in sync.
Synchronization Mode : The relationship between producer and consumer
mutually exclusive mode : The relationship between different producers, the relationship between different consumers

① using recorded semaphores to solve producer-consumer problems
The mutex can be used to realize the mutual exclusion of the buffer pool by mutual exclusion semaphore.
The amount of empty buffer pool and full buffer pool of buffer pool is represented by the volume of the semaphore.

Mutex: Inter-producer, inter-consumer mutex use buffer//empty: Free capacity of buffer//full: the occupied capacity Var mutex of the buffer, empty, full:semaphore: = 1, n, 0;
    Buffer:array[0, ..., n-1] of item;
In, Out:integer: = 0, 0;
                    Begin Parbegin producer://producer begin repeat ...
                    Production of a product into the NEXTP;
                    ...//Enter the area wait (empty);
                    Wait (mutex);
                    Critical zone buffer (in): = NEXTP;
                    In: = (in+1) mod n;
                    Exit area signal (mutex);
                Signal (full);
            until false; End consumer://Consumer begin repeat//Enter zone wait (FU
                    ll);
                    Wait (mutex);
                    Critical Zone MEXTC: = buffer (out);
                    Out: = (out+1) mod n; Exit Area Signal(mutex);
                    Signal (empty);
                The products in the surplus area consumption nextc;
            until false; End Parend End

Attention:
1) The wait (mutex) and signal (mutex) used to implement mutexes in each program must appear in pairs .
2) The wait and signal operations for the resource semaphore, empty and full, also need to appear in pairs, but in different programs .
3) The order of multiple wait operations in each program is not reversed . The resource semaphore should be applied first, then the mutex signal is applied , otherwise the process deadlock may be caused. SIGANL can have no order.

② using and semaphores to solve producer-consumer problems

Mutex: Inter-producer, inter-consumer mutex use buffer//empty: Free capacity of buffer//full: the occupied capacity Var mutex of the buffer, empty, full:semaphore: = 1, n, 0;
    Buffer:array[0, ..., n-1] of item;
In, Out:integer: = 0, 0;
                    Begin Parbegin producer://producer begin repeat ...
                    Production of a product into the NEXTP;
                    ...//Enter the area swait (empty, mutex);//Do not take into account the number of signal problems//critical area
                    Buffer (in): = NEXTP;
                    In: = (in+1) mod n;
                Exit area ssignal (mutex, full);
            until false; End consumer://Consumer begin repeat//entry area swait (f
                    ull, mutex);//Do not take into account the number of signal problems//Critical zone MEXTC: = buffer (out);
                    Out: = (out+1) mod n;
                    Exit area ssignal (mutex, empty);
        Remaining area            The products in the consumption nextc;
            until false; End Parend End
Two The question of the Philosopher's meal

The five philosophers shared a round table, seated in the surrounding five chairs, with five bowls and five chopsticks on the table, and their way of life was to think and dine alternately. At ordinary times, a philosopher thinks, and when he is hungry, he tries to fetch the chopsticks closest to him, only to dine when he gets two chopsticks. After dinner, put down the chopsticks and continue to think.
Visible:
Two adjacent can not dine at the same time;
A maximum of two people can dine at the same time.

① using recorded signals to solve philosophers ' time meal problems
The chopsticks placed on the table are a critical resource , allowing only one philosopher to use it for a period of time. To achieve exclusive use of chopsticks, a semaphore is used to represent a chopstick, and five semaphores constitute an array of semaphores.
Var chopstick:array[0, ..., 4] of semaphore: = 1
All semaphores are initialized to 1.

Var chopstick:array[0, ..., 4] of semaphore: = 1
//The activity of the philosopher of the first place can be described as:
    repeat
        //Enter the area
        wait (chopstick[i]);
        Wait (chopstick[(i+1) mod 5]);
        Critical section ...
        Eat;
        ...
        Exit Zone
        Signal (chopstick[i]);
        Signal (chopstick[(i+1) mod 5]);
        Remaining area ...
        Think;
    until false;

Although the above algorithm can ensure that the adjacent two will not eat at the same time, but may cause deadlock .
For example, when the five philosophers were hungry and each picked up the chopsticks on the left, they would make five semaphores chopstick all 0, and would wait indefinitely for no chopsticks to take.

Workaround :
1) At the same time allow up to 4 philosophers to take the left chopstick (to ensure that at least one philosopher can dine).

Var chopstick:array[0, ..., 4] of semaphore: = 1;
var Count:semaphore: = 4;//resource semaphore
repeat
    //Enter Zone
    wait (count);
    Wait (Chopstick[i]);
    Wait (chopstick[(i+1) mod 5]);
    Critical section ...
    Eat;
    ...
    Exit Zone
    Signal (chopstick[i]);
    Signal (chopstick[(i+1) mod 5]);
    Signal (count);
    Remaining area ...
    Think;
until false;

2) Philosophers must get two chopsticks (using and semaphores) or release the chopsticks they have got.

See below ②

3) The philosopher takes the left chopstick first, then the right chopstick, and the even number philosopher takes the right chopsticks first and then the chopsticks on the left.

Var chopstick:array[0, ..., 4] of semaphore: = 1;
Var I:integer;
Repeat
    //Enter the area
    if I mod 2=1 then
        begin
            Wait (chopstick[i]);//Take the left chopstick
            wait (chopstick[(i+1) mod 5]);// Then take the right chopstick
        end
    Else
        begin
            Wait (chopstick[(i+1) mod 5]);//Take the right chopstick
            wait (chopstick[i]);//Take the left chopsticks
        End
    End
    //critical section
    eat;
    Exit Zone
    Signal (chopstick[i]);
    Signal (chopstick[(i+1) mod 5]);
    Remaining area ...
    Think;
until false;

② using and semaphore mechanism to solve the problem of philosophers ' dining
In the philosopher's meal problem, each philosopher was asked to obtain two critical resources (chopsticks) before eating. is essentially an and synchronization problem.

Var chopstick:array[0, ..., 4] of semaphore: = (1, 1, 1, 1, 1);//denotes chopstick[i] usable
philosopher I;
Repeat
    think;
    Entry Area
    swait (chopstick[(i+1) mod 5], chopstick[i]);
    Critical zone
    eat;
    Exit Area
    ssignal (chopstick[(i+1) mod 5], chopstick[i]);
until false;
three reader – writer's question

A data file or record can be shared by multiple processes.
1) The process of only requiring read files is the "reader process", and other processes are called "writer processes".
2) allows multiple processes to read a shared object concurrently, but does not allow a writer process and other reader processes or writer processes to access the shared object at the same time.

Reader-writer problem is a synchronization issue that guarantees that a writer process must access the shared object with the other process in an exclusive way.
read – Read share; Write – write mutex; Write – Read mutex.

① using record-based semaphores to solve reader-writer problems
Mutex semaphore Wmutex: realizes mutual exclusion between reader and writer process when reading or writing.
Mutex semaphore Rmutex: The amount of signal that implements mutually exclusive access readcount between reader processes.
integer variable readcount: Indicates the number of processes being read.

The writer process is not allowed to write because only one reader process is reading. Therefore, reader needs to perform a wait (Wmutex) operation only if readcount=0 (that is, no reader process is reading). If the wait (Wmutex) operation succeeds, the reader process can read and, accordingly, do the readcount+1 operation.

Similarly, the signal (WMUTEX) operation needs to be performed only when the reader process executes the readcount-1=0, so that the writer process can write.

Wmutex: Read-write mutex, write-write mutex
//rmutex: Read access Readcount mutex
//readcount: Record number of reporters process
Var Wmutex, Rmutex:semaphore: = 1, 1;
    Readcount:integer: = 0;
Begin
    Parbegin
        reader:begin//Read process
            repeat
                //Enter Zone
                wait (Rmutex);//Sub-entry area
                if Readcount=0 then Wait (Wmutex);
                Readcount: = readcount + 1;//sub-critical Zone
                signal (RMUTEX);//Sub-exit
                //critical section ...
                Read;
                ...
                Exit Zone
                Wait (Rmutex);
                Readcount: = Readcount-1;
                If Readcount=0 then signal (WMUTEX);
                Signal (Rmutex);
            until false;
        End

        writer:begin//Write process
            repeat
                Wait (wnutex);
                Write;
                Signal (Wnutex);
            until false;
        End
    Parend
End

Evaluation: can achieve reader-writer problem, but read first, not fair to the writer.

② using semaphore set mechanism to realize reader-writer problem
By introducing the semaphore L and giving its initial value RN, the number of readers is controlled by performing swait (L, 1, 1), which allows the RN reader to read at the same time.

Whenever a reader enters, it is necessary to perform swait (L, 1, 1) operation, so that the value of L is reduced by 1. When an RN reader enters the reading, l will be reduced to 0, the first rn+1 reader to enter the reading, will inevitably be due to swait (L, 1, 1) operation failed and blocked.

L: Control the number of reader processes <=rn
//mx: Realize read-write mutex, write-write mutually exclusive
Var Rn:integer;
    L, Mx:semaphore:RN, 1;
Begin
    Parbegin
        reader:begin
            repeat
                swait (L, 1, 1);
                Swait (MX, 1, 0);//a ...
                Read;
                ...
                Ssignal (L, 1);
            until false;
        End

        Writer:begin
            repeat
                swait (MX, 1, 1; L, RN, 0);//b
                write;
                Ssignal (MX, 1);
            until false;
        End
    Parend
End

Note A
The swait (MX, 1, 0) statement acts as a switch. As long as no writer process enters the write, that is, the Mx=1,reader process can enter the read. But as soon as the writer process enters the write, that is mx=0, no reader process can enter the read.

Note B
swait (MX, 1, 1; L, RN, 0) The statement indicates that the writer process can enter the critical section only if there is no writer process in writing (that is, mx=1) and no reader process is reading (that is, L=RN).

Related Article

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.