Process PV ..

Source: Internet
Author: User

Reprinted from: http://blog.sina.com.cn/s/blog_6fe3efa3010178hl.html

In computer operating systems, PV operations are difficult in process management.
First, we should clarifyPV operation meaningThe PV operation consists of the P operation primitive and the V Operation primitive (the primitive is an uninterrupted process). The operation on the semaphore is defined as follows:
P (S): ① reduce the semaphore s value by 1, that is, S = S-1;
② If s reaches 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.
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 are set to 0, s indicates the number of available resources. Executing a P operation means that one unit resource is allocated to the request. Therefore, the value of S is reduced by 1. When S <0, no resources are available. The requester must wait for other processes to release such resources, it can run. Executing a V operation means releasing a unit resource. Therefore, the value of S is increased by 1. If s £0, some processes are waiting for this resource, therefore, you need to 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; critical section;
V (S); V (s );
...... ...... ...... ......

semaphores are used for mutual exclusion. The initial value is 1.
when using the PV operation to achieve process mutex, note the following:
(1) in each Program , mutually exclusive P and V operations must be performed in pairs. P operations are performed first, P operations are performed in the critical section, and V operations are performed later, output critical section. If multiple branches exist, check their pairs carefully.
(2) 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 endless loops.
(3) the initial value of mutex semaphores is generally 1.

Process synchronization using semaphores and PV operations
PVAn operation is 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-The Consumer problem is a typical 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 null. 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-1, which defines two pointers: In and out. Which are used by the producer process and the consumer process respectively.
, Pointing 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 );
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-1 defines two pointers, in and out, which are the pointers used by the producer process and consumer process, pointing 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) ;
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 an empty disk on the table that can store a fruit. 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.

AnalysisIn this question, Dad, 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 is an orange, the son is allowed to eat, and the daughter must wait. If the fruit is an apple, the daughter is allowed to eat, and the son must wait. This question is actually a variant of the producer-consumer issue. There are two types of products that the producer puts into the buffer, and two types of consumers. Each type of consumer consumes only one fixed type of products.

Solution: In this question, we should set three semaphores s, so, and SA. semaphores s indicate whether the plate is empty, and their initial values are L. semaphores so indicate whether there are oranges in the disk, the initial value is 0; the semaphore SA indicates whether an apple exists in the disk. The initial value is 0. 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 );
Put the fruit into the dish;
If (In orange) V (so );
Else V (SA );
}
}
Son ()
{
While (1)
{
P (so );
Retrieve oranges from the tray;
V (s );
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) semaphore 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];
}}}}

Question and Answer:
( 1) defines 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)

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.