Modeling the Synchronization by Shared Variables. Two users Aand B share the use of a printer in the following way: for user A: if turn = A, then printA; for user B: if turn = B, then printB, while turn is a shared variable between A and B who takes the turn to use the printer.
Design a transition system that precisely models the behaviour of sharing printer between A and B without falling into a situation that A or B has no chance or has to wait for a long time to use the printer
This task can be achieved by peterson's solution in process synchronization which guarantees the limited waiting time between processors/users and mutual exclusion (i.e, two users can't access printer at the same time).
Program (Psuedo code) :
#define N = 2; // No. of users
#define TRUE 1;
#define FALSE 0;
int flag[N]= {FALSE}; // For both the users keep flag as FALSE in the beginning.
char i = random('A','B'); // Randomly select the users into i
char turn;
Void USER (char i ) {
while ( 1 ) {
char j = Not ( i ); // Take the other user which is not in i into j
flag[ i ] = TRUE ; // Assume the value of i is A and the flag value of A will be TRUE
turn = i ; // Now turn to access printer will be A's
while (flag[ j ] == TRUE && turn == i ); // waiting time to access the printer (User waits)
print i ; // prints A ( if B comes then prints B )
flag[ i ] = FALSE;
}
}
Get Answers For Free
Most questions answered within 1 hours.