This is a review question so an explanation and answer would be tremendously hel
ID: 657201 • Letter: T
Question
This is a review question so an explanation and answer would be tremendously helpful, as I am studying for finals.
void select_seat(int passenger){
show_cabin();
seat = get_click();
//code to assign seat to passenger and update cabin
...
...
}
Suppose test_and_set can be used to ensure the above function is free of race conditions.
Illustrate how test_and_set is used by adding code either inside the function, or around the function call, assuming a global variable to all processes called exclusive is declared and initialized to 0.
I don't understand test_and_set method or how it works. Thanks!
Explanation / Answer
void select_seat(int passenger){
show_cabin();
seat = get_click();
//code to assign seat to passenger and update cabin
seatassign();
updatecabin() ;
}
semaphore seat_assigned= 0;
semaphore seat_unassigned = n;
volatile int exclusive = 0;
void updatecabin() {
while (TestAndSet(&exclusive) == 1);
critical section // only one process can be in this section at a time
seat_unassigned=seat_assigned+1 mod n;
exclusive = 0 ;// release lock when finished with the critical section
}
void seatassign()
{
while (TestAndSet(&exclusive) == 1);
critical section // only one process can be in this section at a time
seat_assigned=seat_assigned + 1 mod n;
exclusive = 0 ;// release lock when finished with the critical section
}
Test-and-set does two things atomically:
-> Test a lock (whose value is returned)
-> Set the lock
Lock can be obtained when the return value is 0.
If returned value is 1, someone already had the lock (and still has it)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.