Semaphore is one of the earliest mechanisms used to solve the problem of process synchronization and mutual exclusion.
A semaphore consists of a value and a pointer to the process waiting for the semaphore. The value of the semaphore represents the usage of the corresponding resource. When the semaphore S>=0, S represents the number of available resources. Performing a P operation means that a resource is requested, so the value of S is subtracted by 1; When S<0, the resource is no longer available, and the absolute value of S represents the number of processes currently waiting for the resource. The requester must wait for another process to release the resource before continuing. Performing a V operation means freeing a resource, so the value of S increases by 1; If S<0, some process is waiting for the resource, so you need to wake up a waiting process to run.
In computer operating system,
PV operation is the difficulty in process management.
PV operation is composed of P operation primitives and V operation primitives (primitives are non-interruptible processes), and it operates on semaphores, specifically defined as follows:
P (S) : Reduce the value of semaphore S by 1, that is, S= s-1;
If S 0, the process continues to execute; Otherwise, the process is placed in a wait state and queued.
V (S) : Add the value of semaphore S by 1, that is, S=S+1;
If S>0, then the process continues to execute; Otherwise, release the first process in the queue waiting for the semaphore.
What PV operations mean: We use semaphores and PV operations to achieve process synchronization and mutual exclusion. PV operations are low-level communications of the process.
example:
Problem descr iption: There is an empty plate on the table, and one fruit is allowed. Dad can put an apple in the plate, also can put an orange in the plate, the son to eat the orange in the plate, the daughter to eat the apple in the plate. It is stipulated that when the dish is empty, only one fruit can be put for the eater to take at a time. Please realize the synchronization of three concurrent processes of father, son and daughter with P and V primitive.
Problem analysis::The father, son and daughter share a plate and can only put one fruit on it at a time. When the plate is empty, dad can put a fruit into the bowl. If it was an orange, the son was allowed to eat it, but the daughter had to wait. If an apple is placed in the bowl, the daughter is allowed to eat it and the son must wait. The problem is actually a distortion of the producer-consumer problem. Here, there are two types of products that producers put into the buffer, and there are two types of consumers, each of whom consumes only one set of products.
answers:
