ALL > Computer and Education > courses > university courses > undergraduate courses > Operating System > ZSTU class(2019-2020-1) > student directories > >
homework 4 Version 0
👤 Author: by minhaynqqcom 2019-10-09 11:11:18

think about your possible case about process synchronization, and give out a solution for the problem.


Problem:

There is only one single-plank bridge on a river for pedestrians to cross. There are people crossing the bridge on both sides of the river now. In order to be safe, people in the same direction can cross the bridge continuously. People in one direction should wait while others in the other direction cross the bridge.

Solution:
Mark the two directions of single-plank bridge as A and B respectively. Use Integer variables peopleCountA and peopleCountB to represent the number of rows on the single-plank bridge in the direction of A and B respectively, and the initial value is 0. Then set three mutex semaphore with initial value of 1: SA for mutual exclusive access to peopleCountA, SB for mutual exclusive access to peopleCountB, and mutex for mutual exclusive access to single-plank bridge for pedestrians in both directions.
//semaphore SA,SB,mutex
SA=1;
SB=1;
mutex=1;
int peopleCountA=0;
int peopleCountB=0;

void Process_A()
{
wait(SA);
if(peopleCountA==0)
{
wait(mutex);
peopleCountA+=1;
}
signal(SA);
//across single-plank bridge
wait(SA);
peopleCountA-=1;
if(peopleCountA==0)
{
signal(mutex);
}
signal(SA);

}

void Process_B()
{
wait(SB);
if(peopleCountB==0)
{
wait(mutex);
peopleCountB+=1;
}
signal(SB);
// across single-plank bridge
wait(SB);
peopleCountB-=1;
if (peopleCountB==0)
{
signal(mutex);
}
signal(SB);

}

Please login to reply. Login

Reversion History

Loading...
No reversions found.