PV operation in the operating system

Whether it is computer postgraduate entrance exams, computer software proficiency exams, computer operating system final exams or other computer post exams, the operation of P and V primitives is a common test site. The following author summarizes some knowledge about P, ​​V operation.

Semaphore is the earliest mechanism to solve the problem of process synchronization and mutual exclusion (it can also realize process communication), including a variable called semaphore and two primitive operations on it. The semaphore is an integer, we set this semaphore as: sem. Obviously, we stipulate that when sem is greater than or equal to zero, it represents the number of resource entities available for concurrent processes, and when sem is less than zero, it represents the number of processes waiting to use the critical section. According to this principle, when attaching an initial value to a semaphore, we obviously need to set the initial value to be greater than zero.

p-operations and v-operations are uninterruptible program segments called primitives. In the primitives of P and V, P is Passeren in Dutch, which is equivalent to pass in English, and V is Verhoog in Dutch, which is equivalent to incremnet in English.

And no interruption is allowed during the execution of P, V primitives.

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

First of all , the meaning of PV operation should be clarified: PV operation consists of P operation primitive and V operation primitive (primitive is an uninterruptible process), and operates on semaphore. The specific definitions are as follows:

 

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

                    ② If S>=0, the process continues to execute; otherwise, the process is placed in a waiting state and placed in the waiting queue.

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

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

The meaning of PV operation : We use semaphore and PV operation to achieve synchronization and mutual exclusion of processes. PV operations belong to the low-level communication of processes.

What is a semaphore? The data structure of a semaphore is a value and a pointer to the next process waiting for the semaphore. The value of the semaphore 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 the resource. Note that the value of a semaphore can only be changed by PV operations .

Generally speaking, when the semaphore S>=0, S represents the number of available resources. Performing a P operation means requesting the allocation of a unit resource, so the value of S is decremented by 1;

When S<0, it means that there are no available resources, and the requester must wait for other processes to release such resources before it can run. And performing a V operation means releasing a unit of resource, so the value of S is incremented by 1;

If S<=0, it means that some processes are waiting for the resource, so it is necessary to wake up a process in the waiting state and make it run.

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

Process P1 Process P2 ...... Process Pn

……                     ……                  ……

P(S);                 P(S);                                 P(S);

critical section; critical section; critical section;

V (S) ; V (S) ; V (S) ;

……                     ……                  ……               ……

The semaphore S is used for mutual exclusion, and the initial value is 1

The following should be noted when using PV operations to achieve process mutual exclusion:

(1) In each program, the mutually exclusive P and V operations must appear in pairs, first perform the P operation, enter the critical section, and then perform the V operation to exit the critical section. If there are multiple branches, carefully check the pairing.

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

(3) The initial value of the mutual exclusion semaphore is generally 1.

Process synchronization using semaphores and PV operations

PV operation is one of the typical synchronization mechanisms. A semaphore is associated with a message. When the value of the semaphore is 0, it means that the expected message has not yet been generated; when the value of the semaphore is non-zero, it means that the expected message already exists. When the PV operation is used to achieve process synchronization, the P operation is called to test whether the message arrives, and the V operation is called to send the message.

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

Process A Process B

  ....                            ....

L: P (semaphore) L2: V (semaphore)

  ....                            ....

The following should be noted when using PV operations to achieve process synchronization:

(1) Analyze the constraints between processes and determine the type of semaphore. In the case of maintaining the correct synchronization relationship between processes, which process is executed first, which process is executed later, and what resources (semaphores) are used to coordinate with each other, so that it is clear which semaphores to set.

(2) The initial value of the semaphore is related to the number of corresponding resources, and is also related to the position where the P and V operations appear in the program code.

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

[Example 1] Producer-Consumer Problem

In the multiprogramming environment, process synchronization is a very important and interesting problem, and the producer-consumer problem is one of the representative process synchronization problems. Below we give the producer-consumer problem in various situations. In-depth analysis and thorough understanding of this example will be of great help to comprehensively solve the synchronization and mutual exclusion problems in the operating system .

(1) One producer, one consumer, and one common buffer.

Define two synchronization semaphores:

empty - indicates whether the buffer is empty, the initial value is 1.

full - Indicates whether the buffer is full, 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);

              Take a product from Buffer;

              V(empty);

              consume the product;

              }

(2) One producer and one consumer share n ring buffers.

Define two synchronization semaphores:

empty - indicates whether the buffer is empty, the initial value is n.

full - Indicates whether the buffer is full, the initial value is 0.

             Let the number of the buffer be 1~n&61485;1, define two pointers in and out, which are the pointers used by the producer process and the consumer process respectively, and point to the next available buffer.

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);

    Remove the product from buffer(out);

    out=(out+1)mod n;

    V(empty);

    consume the product;

    }

(3) A set of producers, a set of consumers, and n ring buffers in common

             In this problem, not only should the producer and the consumer be synchronized, but also the buffers must be accessed mutually exclusive between each producer and each consumer.

Define four semaphores:

empty - indicates whether the buffer is empty, the initial value is n.

full - Indicates whether the buffer is full, the initial value is 0.

mutex1 - Mutex semaphore between producers, the initial value is 1.

mutex2 - Mutex semaphore between consumers, the initial value is 1.

             Let the number of the buffer be 1~n&61485;1, define two pointers in and out, which are the pointers used by the producer process and the consumer process respectively, and point to the next available buffer.

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);

    Remove the product from buffer(out);

    out=(out+1)mod n;

    V (mutex2);

    V(empty);

    consume the product;

    }

It should be noted that the order of the two P operations cannot be reversed, whether in the producer process or in the consumer process. The P operation of the synchronous semaphore should be performed first, and then the P operation of the mutex semaphore should be performed, otherwise it may cause a process deadlock.

【Example 2】There is an empty plate on the table, allowing one fruit to be stored. Dad can put apples on the plate, or oranges can be put on the plate, the son waits to eat the oranges in the plate, and the daughter waits to eat the apples in the plate. It is stipulated that when the plate is empty, only one fruit can be placed for the eater at a time. Please use the P and V primitives to synchronize the three concurrent processes of father, son, and daughter.

Analysis In this question, father, son, and daughter share a plate, and only one fruit can be placed on the plate at a time. When the plate is empty, Dad can put a piece of fruit into the bowl. If the oranges are put in the fruit bowl, the son is allowed to eat it, and the daughter must wait; if the fruit bowl is apples, the daughter is allowed to eat it, and the son must wait. This problem is actually a variant of the producer-consumer problem. Here, there are two types of products that producers put into the buffer, and two types of consumers, and each type of consumer only consumes a fixed type of product.

             Solution: In this question, three semaphores S, So, Sa should be set. The semaphore S indicates whether the plate is empty, and its initial value is 1; the semaphore So indicates whether there is an orange in the plate, and its initial value is 0; The quantity Sa indicates whether there is an apple on the plate, and its initial value is 0. Synchronization is described as follows:

int S=1;

int Sa=0;

int So=0;

               main()

               {

                 cobegin

                     father();              

                     son();                

                     daughter();            

                 coend

             }

             father()

             {

                 while(1)

                   {

                     P(S);

                     Put the fruit in a plate;

                     if (the orange is put in) V(So);

                     else V (Sa);

                    }

              }

             son()

             {

                 while(1)

                   {

                      P(So);

                      Remove the oranges from the plate;

                      V (S);

                      eat oranges;

                    }

             }

             daughter()

             {

                  while(1)

                     {

                       P (Sa);

                       Remove the apples from the pan;

                       V (S);

                       eat apples;

                    }

             }

Example 3 On the bus, the activities of the driver and the conductor are as follows: driver; start the vehicle; run normally, stop at the stop. conductor; close the door, sell the ticket and open the door. What are the two activities during the process of the car constantly stopping at the stop Synchronization relationship? Implemented with semaphores and pv operations.

Let the semaphore be s1 (whether to drive) and s2 (whether to stop), s1=1, s2=0;

Driver process: Conductor process:

start start

L1: L2:

P(S1); close the door;

start the vehicle; V(s1);  

normal driving; ticket sales;

V(s2);                        P(s2);

goto L1; open the door;

end ; goto L2;

                                end;

Thinking questions:

Four processes A, B, C, and D all need to read a shared file F, and the system allows multiple processes to read file F at the same time. But the limitation is that process A and process C cannot read file F at the same time, and process B and process D cannot read file F at the same time. In order to use the files according to the system requirements when these four processes are executed concurrently, PV operations are used to manage them. Please answer the following questions:

(1) The semaphore and initial value that should be defined: .

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

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

  {              {               {                  {

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

   read F;         read F;        read F;            read F;

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

   }              }              }                   }

Thought Question Answers:

(1) Define two semaphores S1 and S2, the initial values ​​are 1, namely: S1=1, S2=1. The processes A and C use the semaphore S1, and the processes B and D use the semaphore S2.

(2) From [1] to [8] are: 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 hidden synchronization and mutual exclusion problems when doing questions. These problems can generally be grouped into the producer-consumer problem and the reader-writer problem.

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

★ In a mutually exclusive relationship, PV operations must appear in pairs within a process. Moreover, the signal must be greater than 0, the specific amount depends on the situation. For a synchronous relationship, a pair of PV operations occurs in two or more processes.

★ For the synchronization relationship, the semaphore may or may not be 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 problem, three semaphores need to be set: empty - the number of free buffers, the initial value is n; full - the number of filled buffers, the initial value is 0; mutex - to ensure that there is only one process In the write buffer, the initial value is 1.

 ★ In the reader-writer problem, two semaphores are set: semaphore access-controls write mutual exclusion, the initial value is 1; semaphore rc-controls mutually exclusive access to the shared variable ReadCount (reader statistics value) .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325866989&siteId=291194637