Homework6 Version 0 |
|
👤 Author: by 1079513613qqcom 2020-11-03 07:34:38 |
There is a plate on the table that can hold two fruits at most, and only one fruit can be put or taken out at a time.
Father put apples on the plate, mother put oranges on the plate, two sons
Waiting to eat the oranges on the plate, the two daughters waited to eat the apples on the plate. Please use P and V operations to achieve dad
Synchronization and mutual exclusion between mothers, sons, and daughters.
The plate is a critical resource, and the mutual exclusion semaphore mutex means that the four processes must mutually exclusive access the plate
. Since two fruits can be placed, the initial value of empty is 2; apple represents the number of apples on the plate
, Orange represents the number of orange in the plate, and the initial value is 0.
semaphore mutex=1;
int empty=2;
int apple=0;
int orange=0;
void father()
{
while(1)
{
p(empty);
p(mutex);
put an apple;
v(mutex);
v(apple);
}
}
void mother()
{
while(1)
{
p(empty);
p(mutex);
put an orange;
v(mutex);
v(orange);
}
}
void son()
{while(1)
{
p(orange);
p(mutex);
get an orange;
v(mutex);
v(empty);
}
}
void daughter()
{
while(1)
{
p(apple);
p(mutex);
get an apple;
v(mutex);
v(empty);
}
}