Home CS439

# CS439: Principles of Computer Systems

## Discussion Section Problem Set 4

### Due in Section on Friday, September 29, 2017

The problem set must be completed before section and brought to section. To ensure that your problem set is turned in correctly and that you receive credit for discussion section, you must follow these guidelines exactly.
```Discussion Section Problem Set #:
Student Name:
EID:
Unique Number:
```
• You may either type your answers or write them by hand, but you must bring a hard copy to discussion section.
• In discussion section, your TA will check that you make a good effort on the problem set and that you are participating in the discussions in section and ready and able to answer when called upon. If all of those are true, you will receive credit for discussion section that day. Note that a "good effort" on the problem set includes that all answers are in your own words and not copied from anywhere else, including the slide deck.

1. Consider a system using a multilevel feedback queue scheduler. Its scheduler is configured to have four queues, which are, in order of highest priority to lowest priority: Q1, Q2, Q3, and Q4. The queues have quantums sized 5s, 10s, 20s, and 40s, respectively.

For each of the following three processes, determine which queue it is in when it begins its final quantum.

• Process A, runtime 40s, blocks every 2s.
• Process B, runtime 42s, never blocks.
• Process C, runtime 70s, blocks at 7s, 11s, 25s, 42s, 48s, where those times are points in its execution.

2. Given the following pseudocode:
```int i = 0;

int main(void)
{
fork();

if(/*CHILD*/)
{
i = i - 1;
return SUCCESS;
}
if(/*PARENT*/)
{
i = i + 1;
wait for child to terminate;

printf("i = %d\n", i);
}
}
```

• What is the output? Describe in detail why it is that way.
• Are there other possible outputs? Why or why not? If so, give another example of what the printed values of ``i'' might be and explain how to force the program to execute the same way each time.
3. Now consider this pseudocode involving kernel threads:
```int i = 0;

{
i = i - 1;
}

{
i = i + 1;
}

int main(void)
{

printf("i = %d\n", i);
}
```
• What is the output? Describe in detail why it is that way.
• Are there other possible outputs? Why or why not? If so, give another example of what the printed values of ``i'' might be and explain how to force the program to execute the same way each time.
4. Consider the following program fragment:
```if(a > 0)
sema_down(s1);
else
sema_down(s2);

b++;

sema_down(s3);

if(b < 0 && a <= 0)
sema_down(s1);
else if(b >= 0 && a > 0)
sema_down(s2);
else
sema_down(s4);

a++;

sema_up(s4);
sema_up(s3);
sema_up(s2);
sema_up(s1);

```

s1, s2, s3 and s4 are semaphores initialized to 1. All variables are automatic (on the stack). Now, consider two threads running this fragment of code simultaneously, can there be a deadlock? Why, or why not?

5. You need two Hydrogen atoms and one Oxygen atom to make water. Using semaphores, write a pseudocode solution that generates water whenever the right resources are available. You must have three functions: one to generate Oxygen, one to generate Hydrogen, and one to generate water. Assume the three methods may be executed by any number of threads concurrently.

6. Solve the water problem using monitors (pseudocode).

7. A standardized C language threads programming interface has been developed for UNIX systems, IEEE POSIX 1003.1c. Thread implementations that adhere to this standard are known as POSIX threads, or Pthreads. Using Pthreads, write a C program that creates three new threads and assigns each of them a number. Each thread should print the numbers 1-10, one per line, with its number beginning the line, so that the beginning output from thread 1 would look like the following:
```1: 1
1: 2
1: 3
```
The original thread should wait for the new threads to finish and then print a statement notifying the user that the application is finished.

Explain the output. Turn in your code, output, and explanation.