Professional Documents
Culture Documents
Assignment 1 COSC 5302 Fall 2022 - Tagged
Assignment 1 COSC 5302 Fall 2022 - Tagged
Systems
Assignment 1
Due: 10:00am, Feb. 9, 2022 (Friday)
Name: ______________________
LUID: ______________________
Date: ______________________
Question 1. Dijkstra posed each of the following solutions as a potential software solution to the
critical section problem and then explained why they failed. Provide your explanation about why
they failed. (30 points)
Solution a:
proc (int i) {
while (TRUE){
compute;
while (turn != i);
critical_section;
turn = (i+1) mod 2;
}
}
shared int turn;
turn = 1;
fork(proc, 1, 0);
fork(proc, 1, 1);
Solution b:
proc(int i){
while (TRUE) {
compute;
while (flag[(i+1) mod 2]);
flag[i] = TRUE;
critical_section;
flag[i] = FALSE;
}
}
shared boolean flag[2];
flag[0] = flag[1] = FALSE;
fork(proc, 1, 0);
fork(proc, 1, 1);
Solution c:
proc(int i){
while (TRUE) {
compute;
flag[i] = TRUE;
while (flag[(i+1) mod 2]);
critical_section;
flag[i] = FALSE;
}
}
shared Boolean flag[2];
flag[0] = flag[1] = FALSE;
fork(proc, 1, 0);
fork(proc, 1, 1);
Question 2. (20 points)
a. Please use one figure to illustrate what a Test-and-Set (TS) instruction is. Please include the
change of register values and memory values in your figure.
int main ()
{
Create a thread my_thread using pthread_create;
Wait until my_thread terminates, using pthread_join;
}
void *compute_sum()
{
Compute the sum from 1 to 100;
Print out the result;
}
For information about pthread_create and pthread_join:
man pthread_create
man pthread_join
Softcopy: Please use BlackBoard to turn in your homework solutions. For programming
questions, please do NOT use MS WORD. Please send .c files.