Signal volume and PV operation

Source: Internet
Author: User
Tags semaphore

There are often mutually exclusive and synchronous relationships between processes in the operating system. In order to deal with this situation effectively, W.dijskra proposed the concept of signal volume and PV operation in 1965.
(1) Semaphore: A special variable, expressed as an integral type S and a queue
(2) P operation: also becomes "down () and wait () operation", so that s=s-1, if s<0, the process pauses execution and puts in the semaphore waiting queue.
(3) v operation, also known as "Up () and signal () operation", enables S=s+1, if s<=0, to wake up a process in the waiting queue.

The PV operation belongs to the low level communication of the process.

The general model for realizing process mutex using semaphores and PV operations is:

Process P1 Process P2 ... Process pn... .... .......

P (s); P (s); P (s);
Critical zone; critical zone; critical area;
V (s); V (s); V (s);
......      ......         ...... ......

Where the semaphore s is used for mutual exclusion, the initial value is 1.


The implementation of the process mutex using the PV operation should be noted:
(1) Each program with the user to achieve mutual exclusion of p, v operation must be in pairs appear, first do p operation, into the critical area, after doing v operation, out of the critical area. If you have more than one branch, carefully check its alignment.
(2) P, v operation should be close to the critical section of the head of the tail, the critical section of the code should be as short as possible, there can not be a dead cycle.
(3) The initial value of the mutex signal is generally 1.

Producer and consumer issues
Classic examples
Example 1 there is an empty plate on the table that allows for the storage of a fruit. Dad can put apples to the plate, you can also put oranges on the plate, son, such as eating in the dish of oranges, daughter, especially to eat apples in the dish. It is stipulated that when the disk is empty, only one fruit can be placed at a time for the eater to take, please use P, V to achieve synchronization of the three concurrent processes of father, son and daughter.


Analysis in the subject, father, son, daughter shared a plate, the plate can only put one fruit at a time. When the plate is empty, dad can put a fruit into the fruit plate. If an orange is placed in the fruit plate, the son is allowed to eat, and the daughter must wait; If the apple is placed in the fruit plate, the daughter is allowed to eat, and the son must wait. The subject is actually a variant of the producer-consumer problem. Here, there are two types of products that producers put into the buffer zone, and there are two types of consumers, each of which only consumes a fixed category of products.
Solution: In the subject, should be set three semaphore s, so, Sa, the semaphore s indicates whether the plate is empty, its initial value is L; The semaphore so indicates whether there are oranges in the disk, the initial value is 0; The semaphore Sa indicates whether there is an apple in the disk and its initial value is 0. The synchronization is described as follows:
int S=1;
int sa=0;
int so=0;
Main () {
Cobegin
Father (); /* Father Process */
Son (); /* Son process */
Daughter (); /* Daughter Process */
Coend

Father () {
while (1) {
P (S);
Put the fruit into the plate;
if (put in an orange) V (SO);
else V (Sa);
}
}
Son () {
while (1) {
P (SO);
Remove an orange from the plate;
V (S);
Eat oranges;

}
Daughter () {
while (1) {
P (Sa);
Remove the apples from the plate;
V (S);
Eat apples;


Father for the producer, daughter, son for the consumer, father and daughter, son of the relationship for the corresponding synchronization relationship.
And when something exists, and another thing cannot, it is mutually exclusive.

As follows
Study Questions

Four processes A, B, C, D all read a shared file F, and the system allows multiple processes to read the file F simultaneously. However, the limit is that process a and process C cannot read the file F at the same time, and neither process B nor process D can read the file at the same time. In order for these four processes to execute concurrently while the files are being used as required by the system and are now managed with the PV operation, please answer the following questions:
(1) The semaphore and the initial value should be defined:.
(2) in the following procedures to fill in the appropriate p, v operation, to ensure that they can work correctly concurrently:
A () B () C () D ()
{      {       { {
[1];      [3];      [5]; [7];
Read F;     Read F;    Read F; Read F;
[2];        [4];      [6]; [8];
}      }       }       }


Study Questions Solutions:

(1) Define two semaphore S1, S2, the initial value is 1, namely: S1=1,s2=1. Where processes A and C use semaphores S1, processes B and D use semaphores S2.
(2) from [1] to [8] respectively: P (S1) v (S1) p (S2) v (S2) p (S1) v (S1) p (S2) v (S2)

Process A and process C cannot read the file at the same time F, neither process B nor process D can read the file at the same time F this is mutually exclusive


Another example of resolving process synchronization and mutex issues with PV primitives:
Classic IPC Issues such as: producers-consumers, readers-writers, philosophers dining, sleeping hairdressers, etc. can refer to the relevant teaching materials.

Signal volume and PV operation

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.