Process management (nine) - mutual exclusion

Process management (nine) - mutual exclusion

Critical section

What is the critical area

A program does not allow multiple concurrent processes is called the critical part of the implementation of the cross (critical section) or critical region (critical region)

The critical area is not possible with additional hardware method to solve. Thus, the critical area may also be referred to that part of the program to access common data.

Why should we set the critical region

Time to prevent the execution of concurrent processes, conflict, or because the two concurrent processes in a competitive state, leading to both the execution order is not fixed, resulting in program execution error.

example

Design study process PA, PB shared memory MS. MS system is divided into zones, work processes and data areas. The data area is divided into equal size blocks, each block may have both data, there may be no data. The system area is mainly stack S, the data block storage address space.

Getspace order to process the data block emptied, release (ad) for the release of the data blocks process. If the stack S is not empty, the process PA or PB can be in any order release and retrieve data blocks. When the process is executed concurrently, getspace or release (ad) will likely able to fulfill the required function

getspace: begin local g
g←stack[top]
top←top-1
end

release(ad): begin
top ← top+1
stack[top]←ad
end

When the set time t0, top = h0

First release (ad) of an execution:
T0: ← Top Top Top = H0 + +. 1. 1;

Getspace then performed:
T1: G ← Stack [Top] G = Stack [H0 +. 1];
T2: ← Top Top Top. 1-= H0;

Longer release (ad) performing a second sentence:
T3: Stack [Top] ← AD Stack [H0 +. 1] ← AD;

Getspace calling process is to take an undefined values ​​h0 + 1; call Release (ad) of the process of releasing the empty block address into a repeated ad h0.

Indirect constraints

class

Those not allowed to cross the critical section performed by different public data into different sets.

In the embodiment, the critical region common to the divided data stack S is set {getspace, release}. The set is called the classes (class)

Description class

Provided class {getspace, release} class named sp, getspace and release (ad) may be restated as:

getspace:   
when sp 
   do 
       getspce←stack[top]
        top←top-1 
   od
release(ad): 
when sp 
   do 
      top← top+1
      stack [top]← ad 
   od

What is indirect constraints

The share of public resources caused by the speed of execution of concurrent processes indirect constraints , referred to as indirect constraints.

What caused the indirect constraints

Phenomenon in the critical region does not allow concurrent processes cross execution of a due share of public resources caused by

Exclusive

What are mutually exclusive

More than two share the resources do not allow concurrent processes simultaneously enter the critical section called mutually exclusive

Pure Process

Pure Class refers to the process does not change during a process of its own code during execution.

Advantages and disadvantages of the process of pure

Advantages:
The process of creating a pure process may facilitate a plurality of processes share

Cons:
However, due to the preparation of pure process must be handled accordingly the relevant variables and work area, so that the efficiency is often affected to some degree

To meet the requirements of mutual exclusion

1. Each concurrent processes equal, independent of the rights of common resources to compete, and without taking any measures conditions in the critical area of any of the end of a command, other concurrent process can enter the critical region.
2. concurrent processes in a process not in the critical region, it does not prevent other processes enter the critical section
3. concurrent processes in a number of processes apply to enter the critical section, only allowing one process to enter
a 4 concurrent process start, should be for a limited time to enter the critical region when a process application to enter the critical zone

Implementation mutually exclusive

Lock

When a process enters a critical section, it will lock the critical section until it exits the critical region so far. Concurrent processes when applying to enter the critical region, first test whether the critical section is locked. If the critical section has been locked, the process will it be possible to wait until after the critical region critical region unlock

Locking problems arising:
1. locking method can achieve mutual exclusion between processes, but there are issues affecting the reliability and efficiency of the system.

For example, if a group of large number of concurrent processes, each process needs to be tested in a locked position when applying to enter the critical region. The loss test cycle lock bit more CPU time.

2. interprocess mutex locking method, will result in injustice, in some cases

Reason: a process can enter the critical region rely on their own process of calling lock procedure to test the corresponding lock bit. Each process can enter the critical section of the test is to rely on their own judgment!

signal

put forward

Semaphore and concepts described below P, V primitive is proposed by Dutch scientists EWDijkstra

What is the semaphore

Sem semaphore is an integer

1.sem greater than zero when the number of resources available to the entity on behalf of concurrent processes used;
2.sem is less than zero indicate the number of processes waiting to use the critical section.
3. The initial value for the mutex semaphore sem should be greater than zero

The process of establishing the semaphore

Establishing a semaphore following steps:
1. The amount of built descriptive sense signal represented;
2. Semaphore initial value;
3. Create a data structure to point to a respective process that is waiting to use the critical section.

PV primitives

What are the PV primitives

P and V are the initials of Verhoog and Passeren Dutch, English equivalent pass and increment

sem is a critical area of public resources used related to the amount of signal . P primitive operations that once the semaphore sem minus 1, and V a primitive operation such that 1 plus sem semaphore.

V primitive

The main operation is a primitive operation V:
1.sem plus 1;
2. If the addition result is greater than zero, the process continues;
3. If the addition result is smaller than or equal to zero, from the queue waiting for a wake-up signal process and then return to the original process to continue or transfer process scheduling

P primitives

1.sem plus 1;
2. If the addition result is greater than zero, the process continues;
3. If the addition result is smaller than or equal to zero, the signal from the waiting queue waiting process in a wake-up, and then returns to the original process to continue or transfer process scheduling

PV primitives to solve the problem

The use of P, V primitive and semaphores, mutual exclusion can easily solve the problem of concurrent processes, but no problem using the locking method that appears when a mutex to solve the problem.

PV mutual exclusion primitive conditions

The use of P, V primitive and semaphores, mutual exclusion can easily solve the problem of concurrent processes, but no problem using the locking method that appears when a mutex to solve the problem.

PV primitives describe mutual exclusion

Sem mutex is provided, which is in the range of (0, -1).
sem = 1 represents none processes PA and PB into the class called the critical area S;
SEM = 0 that the process has entered the PA or PB class called the critical area S;
SEM = -1 indicates that the process PA and PB, a process It has entered the critical zone, while another process is waiting to enter the critical section.

Guess you like

Origin www.cnblogs.com/mengxiaoleng/p/11619222.html