Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

COSC 5302: Advanced Operating

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.

b. How can you use a TS instruction to implement a binary semaphore.

Question 3. (20 points)


Please briefly explain how to generate an executable program from source programs. Suppose C
programming language is used.

Programming question 4: (30 points)


Please use thread to complete the following program: one process creates a thread my_thread.
The job of the thread my_thread is to compute the sum of 1 to 100 (1+2+…+100), print out the
result, and then terminate. The process waits for the termination of the thread, then terminate.

Basically, you need to implement main_process.c and thread_function.c.

Basic structure of main_process.c:

int main ()
{
Create a thread my_thread using pthread_create;
Wait until my_thread terminates, using pthread_join;
}

Basic structure of thread_function.c:

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

How to compile and link your program:

gcc -o my_example main_process.c thread_function.c -lpthread


How to Turn In:

Softcopy: Please use BlackBoard to turn in your homework solutions. For programming
questions, please do NOT use MS WORD. Please send .c files.

You might also like