2018329621184_边凯昂_homework6 Version 0 |
|
👤 Author: by 244766935qqcom 2020-11-06 16:33:28 |
Semaphore is one of the earliest mechanisms used to solve the problem of process synchronization and mutual exclusion.
A Saphore 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.
Note that the value of the semaphore can only be changed by the PV operation.
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.
There are empty plates on the table and a 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.
Father, son and daughter share a plate and can only put one piece of 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.
Solution: In this question, three semaphore S, So and Sa should be set. Semaphore S represents whether the plate is empty and its initial value is L.Semaphore So represents whether there is an orange in the dish, and its initial value is 0;The semaphore Sa indicates whether there are apples 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(); /* Father process */
son(); /* Son process */
daughter(); /* Daughter progress */
coend
}
father()
{
while(1)
{
P(S);
Put the fruit on a plate;
If (put an orange) 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 plate;
V(S);
Eat an apple;
}
}