Semaphore PV operation

Source: Internet
Author: User
Tags semaphore

Reference

One who has to mention the theory of P and V primitive is the famous Dutch scientist E. W. Dijkstra. If you have no idea about this scientist, we should be familiar with the Dijkstra algorithm that solves the shortest path problem in graph theory. The concepts of P, V, and the semaphores to be used in P and V operations were all proposed by him in 1965.

Semaphores are the first mechanism to solve the problem of Process Synchronization and mutex (process communication can also be implemented), including a variable called the number of signals and two primitive operations on it. Semaphores are an integer. We set this semaphores to SEM. Obviously, when SEM is greater than or equal to zero, it indicates the number of resource entities available for concurrent processes. When SEM is smaller than zero, it indicates the number of processes waiting for the use of the critical zone. According to this principle, when the initial value is attached to the semaphore, we obviously need to set the initial value to be greater than zero.

P and V Operations are non-disruptive program segments, called primitives. In the P and V primitives, p is the passeren of Dutch, equivalent to the English pass, V is the verhoog of Dutch, and equivalent to the incremnet in English.

In addition, interruptions are not allowed during the execution of P and V.

There are many specific implementation methods, which can be implemented by hardware or software. This semaphore mechanism must have public memory and cannot be used in distributed operating systems. This is its biggest weakness.

 

 

My understanding of PV:

There are often P (empty) T (full), but the P operation is a pass, that is, a subtraction operation. Why is the parameter empty? This is a function that determines whether the empty value is 0 after being subtracted from the semaphore. If it is 0, the system will not continue, basically, PV is a function used to operate on a global variable (between individuals ).....

 

 

 

 

-------------------------------------------------------------

First, we should clarify the meaning of PV operations: PV operations are composed of P operation primitive and V Operation primitive (primitive is an uninterrupted process), and semaphore operations are defined as follows:

P (S): ① reduce the semaphore s value by 1, that is, S = S-1;

② If S> = 0, the process continues to run; otherwise, the process is set to the waiting status and is discharged into the waiting queue.

V (s): ① Add 1 to the semaphore s value, that is, S = S + 1;

② If S> 0, the process continues; otherwise, the first process in the queue is released waiting for the semaphore.

The significance of PV operations: We use semaphores and PV operations to synchronize and mutex processes. The PV operation is a low-level communication of processes.

What is semaphores? The data structure of semaphore is a value and a pointer. The Pointer Points to the next process waiting for the semaphore. The semaphore value is related to the usage of the corresponding resource. When its value is greater than 0, it indicates the number of currently available resources; when its value is less than 0, its absolute value indicates the number of processes waiting to use this resource. Note that the semaphore value can only be changed by the PV operation.

Generally, when semaphores S> = 0, s indicates the number of available resources. Executing a P operation means that the request is allocated a unit of resources, so the value of S is reduced by 1;

When S <0, it indicates there are no available resources. The requester must wait for other processes to release the resources before it can run. Executing a V operation means releasing a unit of resources, so the value of S is increased by 1;

If S <= 0, it indicates that some processes are waiting for the resource. Therefore, you must wake up a waiting process to run it.

The general model for mutual exclusion of processes using semaphores and PV operations is:

Process P1 process P2 ...... Process PN

...... ...... ......

P (S );

Critical section; critical section;

V (s );

...... ...... ...... ......

 

Semaphores are used for mutual exclusion. The initial value is 1.

Note the following when using the PV operation to achieve process mutex:

(1) The P and V operations that are mutually exclusive to the user in each program must appear in pairs. First, P operations are performed to enter the critical section, and then v operations are performed to output the critical section. If multiple branches exist, check their pairs carefully.

(2) P and V operations should be respectively close to the head and tail of the critical section. The code of the critical section should be as short as possible and there should be no endless loops.

(3) The initial value of mutex semaphores is generally 1.

Process synchronization using semaphores and PV operations

PV operations are one of the typical synchronization mechanisms. A semaphore is used to associate a message. When the semaphore value is 0, it indicates that the expected message has not been generated. If the semaphore value is not 0, it indicates that the expected message already exists. When processes are synchronized using the PV operation, call the P operation to test whether the message arrives, and call the V operation to send the message.

 

 

Note the following when using the PV operation to synchronize processes:

(1) analyze the control relationship between processes and determine the semaphore type. When there is a correct synchronization relationship between processes, which processes are executed first, which are then executed, and what resources (semaphores) are used to coordinate with each other to determine which semaphores should be set.

(2) The initial values of semaphores are related to the number of corresponding resources and the position where P and V Operations appear in the program code.

(3) P and V Operations of the same semaphore must appear in pairs, but they are in different process codes.

 

 

[Example 1] producer-consumer problems

In a multi-program environment, process synchronization is a very important and interesting issue, and the producer-consumer issue is a representative process synchronization problem. The following describes the producer-consumer problem in various situations. In-depth analysis and thorough understanding of this example will be of great help to solve the synchronization and mutex problems in the operating system.

(1) A producer, a consumer, and a buffer zone.

Define two synchronous semaphores:

Empty -- indicates whether the buffer is empty. The initial value is 1.

Full -- indicates whether the buffer is full and the initial value is 0.

Producer Process

While (true ){

Produce a product;

P (empty );

The product is sent to buffer;

V (full );

}

Consumer Process

While (true ){

P (full );

Extracts a product from the buffer;

V (empty );

Consume this product;

}

 

 

 

(2) A producer and a consumer share n circular buffers.

Define two synchronous semaphores:

Empty -- indicates whether the buffer is null and the initial value is N.

Full -- indicates whether the buffer is full and the initial value is 0.

Set the buffer number to 1 ~ N & 61485; 1, defines two pointers in and out, which are used by the producer process and the consumer process respectively, pointing to the next available buffer zone.

Producer Process

While (true ){

Produce a product;

P (empty );

The product is sent to buffer (in );

In = (in + 1) mod N;

V (full );

}

Consumer Process

While (true ){

P (full );

Extracts the product from buffer (out;

Out = (out + 1) mod N;

V (empty );

Consume this product;

}

 

 

 

(3) A group of producers and consumers share n circular buffers.

In this case, not only do producers and consumers need to be synchronized, but also each producer and consumer must access the buffer mutually.

Define four semaphores:

Empty -- indicates whether the buffer is null and the initial value is N.

Full -- indicates whether the buffer is full and the initial value is 0.

Mutex1: mutex semaphores between producers. The initial value is 1.

Mutex2: mutex semaphores between consumers. The initial value is 1.

Set the buffer number to 1 ~ N & 61485; 1, defines two pointers in and out, which are used by the producer process and the consumer process respectively, pointing to the next available buffer zone.

Producer Process

While (true ){

Produce a product;

P (empty );

P (mutex1 );

The product is sent to buffer (in );

In = (in + 1) mod N;

V (mutex1 );

V (full );

}

Consumer Process

While (true ){

P (full );

P (mutex2 );

Extracts the product from buffer (out;

Out = (out + 1) mod N;

V (mutex2 );

V (empty );

Consume this product;

}

Note that the order of two P operations cannot be reversed in both the producer process and the consumer process. Execute the P operation for synchronizing semaphores first, and then execute the P operation for mutex semaphores. Otherwise, the process may be deadlocked.

 

 

 

[Example 2] There is a blank disk on the table, allowing a fruit to be stored. Dad can put an apple in the disk, or an orange in the disk, and his son can eat the oranges in the disk, and his daughter can eat the apples in the disk. When the disk is empty, only one fruit can be put at a time for users to use. Use the P and V primitives to synchronize the three concurrent processes, Father, Son, and daughter.

In this question, my Father, Son, and daughter share one plate and can only put one fruit at a time. When the plate is empty, Dad can put a fruit into the fruit tray. If the fruit tray contains oranges, the son is allowed to eat, and the female must wait. If the fruit tray contains apples, the daughter is allowed to eat and the son must wait. This question is actually a variant of the producer-consumer issue. Here, there are two types of products that the producer puts into the buffer, and two types of consumers, each type of consumers only consume a fixed type of products.

Solution: in this question, three semaphores s, so, and SA should be set. semaphores s indicate whether the plate is empty. The initial value is L (critical value ); the semaphore so indicates whether there is an orange in the disk. Its initial value is 0 (critical value). The semaphore SA indicates whether there is an apple in the disk. Its initial value is 0 (critical value ). Synchronization is described as follows:

Int S = 1;

Int SA = 0;

Int so = 0;

Main ()

{

Cobegin

Father ();/* parent process */

Son ();/* son process */

Daughter ();/* daughter process */

Coend

}

Father ()

{

While (1)

{

P (S);/* P (S) Here s is reduced to 0 ---- minus indicates that the resource is reduced by one, and a blocking process is used to obtain the resource,

Here, there is a process in son () or Dau () to obtain resources brought by P (S ).

 

Therefore, P (S) is used to provide (reduce) one resource (Public ---- Here is the fruit) to block other resources.

The process is used. One of the blocked processes is released to use this resource.

 

In the image, the parent process puts a fruit, and both the son and daughter eat the fruit.

*/

 

Put the fruit into the dish;

If (in orange) V (so); // after a fruit is put, one of the semaphores of the son and daughter may add one

Else V (SA );

}

}

 

Son ()

{

While (1)

{

P (so); // The son minus so because he has eaten one. If so is not enough, it is not enough. At this time, it will be blocked, until the so V (SO) operation is performed.

Retrieve oranges from the tray;

V (s); // After the son eats one, set the semaphore of his father's plate (this is similar to 0/1)

Eat oranges;

}

}

 

Daughter ()

{

While (1)

{

P (SA );

Remove the apple from the drive;

V (s );

Eat apple;

}

}

 

 

 

Questions:

Four processes A, B, C, and D must read a shared file F. The system allows multiple processes to read the file F at the same time. But the restriction is that process a and process C cannot read the file F at the same time, and process B and process D cannot read the file F at the same time. To allow the four processes to use files as required by the System During Concurrent execution, the PV operation is used for management. Please answer the following questions:

(1) semaphores and initial values to be defined :.

(2) Fill in appropriate P and V Operations in the following programs to ensure that they can work correctly concurrently:

A () B () C () D ()

{{{{

[1]; [3]; [5]; [7];

Read F; read f; read F;

[2]; [4]; [6]; [8];

}}}}

 

Answers to questions:

(1) define two semaphores S1 and S2. The initial values are 1, that is, S1 = 1, S2 = 1. Process A and C use semaphores S1, and process B and D use semaphores S2.

(2) From [1] to [8]: P (S1) V (S1) P (S2) V (S2) P (S1) V (S1) P (S2) V (S2)

 

Semaphore and PV operations solve the problem of synchronization and mutual exclusion between processes.

 

★Pay special attention to the hidden synchronization and mutex issues during Question preparation. These problems can generally be classified into producer-consumer issues and reader-writer issues.

★PV operations must appear in pairs, but this does not mean they will appear in a process in pairs.

★In the mutex, PV operations must appear in pairs in a process. Moreover, the signal must be greater than 0, and the specific amount depends on the situation. For synchronization relationships, a pair of PV operations appear in two or more processes.

★For synchronization relationships, semaphores may be 0 or not 0. The number of signals used for synchronization may be one or more.

★If the semaphore is 1, the V operation should be performed first.

★In the producer-consumer issue, set three semaphores: the number of empty-idle cache areas, initial value: N; full-filled cache areas, initial value: 0; mutex-ensure that only one process is in the cache. The initial value is 1.
★In the reader-writer issue, set two semaphores: semaphore access-control write mutex; Initial Value: 1; semaphore RC-control mutex access to the shared variable readcount (Reader's statistical value.

 

 

 

 

 

 

 

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.