# CS372H Spring 2010 Homework 11 Solutions

## Problem 1

In class, we discussed the fact that, if messages can be lost, it is impossible to devise an algorithm that guarantees that two nodes can agree to do the same thing at the same time (the two generals problem). However, weaker forms of agreement may be possible.

Suppose two nodes, A and B, communicate via messages and that the probability of receiving any message that is sent is P (0 < P < 1 ). You need not consider any other types of failures.

1. Is it possible for A and B to agree with certainty to perform some action (but not necessarily perform it at the same time)? If not, explain why not. If so, describe a protocol that provides this guarantee.
Solution:
Needs solution.

2. Is it possible for both nodes to agree to do the same thing at the same time with >99.99999% certainty (e.g. guarantee that there is less than a 0.0000 1% risk that one or both will fail to make the appointment)? If not, explain why not. If so, describe a protocol that provides this guarantee.
Solution:
Needs solution.

3. Suppose that in addition to lost messages, either A or B may crash at any time and, once crashed, recover at some arbitrary time in the future. Is it possible for A and B to agree with certainty to perform some action (but not necessarily perform it at the same time)? If not, explain why not. If so, describe a protocol that provides this guarantee
Solution:
Needs solution.

## Problem 2

A system uses multi-partition allocation with swapping. The size of main memory is 128K, and there are 4 programs running with sizes 64K, 64K, 32K, and 16K respectively. Each program runs for 50msec, and then requests an I/O operation that requires 20msec. A program can be swapped at the rate of 8Kbyte/20msec. Compute the CPU utilization under round-robin scheduling. Devise a better scheduling strategy and show it has a better utilization.

Solution:
Under round-robin scheduling the utilization increases with the time quantum, so let us assume that the time quantum is larger than 50msec. After loading (and running) the first two processes, running the third and fourth require swapping out the first process to make room available in memory. After the first round, we need to bring in the 1st process from the swap space. Because process 4 is still performing an I/O, it cannot be swapped out, and therefore it is the second process that has enough space to allow the first process to get in, thus the second process gets swapped out. But then, it will get swapped in again soon, instead of processes 3 and 4 (process 1 cannot be swapped out because its address space has to remain in memory while the I/O operation takes place). So, in steady state a time window will look like:

Process 2 swapped out, process 1 swapped in, process 1 runs, processes 3 & 4 swapped out, process 2 swapped in, process 2 runs, process 1 swapped out, process 3 swapped in, process 3 runs, process 4 swapped in (note: we do not need to swap anybody out to get process 4 in), process 4 runs, and loop. The memory will look like:

Process 1: red, Process 2: blue, Process 3: yellow, Process 4: Green

Now, to compute the utilization under this model: In each iteration, we have each process running for 50msec. The time wasted for swapping
is: 64 * 20/8 (2 out) + 64 * 20/8 (1 in) + (32 + 16) * 20/8 (3 & 4 out) + 64 * 20/8 (2 in) + 64 * 20/8 (1 out) + 32 * 20/8 (3 in) + 16 * 20/8
(4 in) = 880 msec. Utilization is 200 / ( 200 + 880) = 18.5%

A better solution is to run the available processes in memory for a few time slices, then swap them out and swap in other processes, run them for a while, and so on.
For example: Micro-level scheduling: round-robin. Macro-level scheduling: each process is swapped out after 10 time slices. In this case, the scenario would be, in steady state Processes 3 & 4 swapped out, processes 1 & 2 swapped in, processes 1 & 2 run for 10 time slices each, get swapped out, processes 3 & 4 swapped in, processes 3 & 4 run for 10 time slices, and so on...

Time wasted for swapping: 48 * 20/8 + 128 * 20/8 + 128 * 20/8 + 48 * 20/8 = 880, while the utilization would be:
10 * 100 / (10 * 100 + 880) = 53.2%

This scheme could be optimized further by swapping out a process as soon as it finishes its 10th time slice, but the difference in utilization is not substantial.

## Problem 3

In problem two, compute the CPU utilization if we:
1. replace the CPU by one that runs 10 times faster
2. double the size of main memory

Solution:

1. Under round-robin, each process will run 5 msec before asking for I/O. Therefore, the resulting utilization is 20/(20+880) = 2.2%
2. If we double the size of main memory, we can accommodate all processes, which will result in 100% utilization assuming the time quantum is still at 50msec.

## Problem 4

Alice P. Hacker is a Head Guru in MacroHard, a software company known for operating systems with very sorry quality. She suggested a trick to reduce the pressure on the swap space. Instead of swapping out pages that belong to code texts into the swap area, the operating system could just take away the memory frames. If the code page is needed later, it could be paged directly from its binary file. Her argument is that the operating system will save time by not storing the code page into the swap space, and will save space in the swap area. The code text exists on disk anyway, and could be fetched from there. Would you approve of this design? Why or why not?

Solution:
This is a nice trick, but it ignores a small problem. What happens if the binary file changes while the program is running in main memory? If the operating system evicts the code pages (during swapping out) but does not save them on the swap space, and just brings them from the binary file (during swapping in), then we are loading a different program into the process. We can have the situation then that pages belonging to two different programs be in the process's text segment. The results of such a disastrous mix are undefined at best. Operating systems that wish to play this trick lock the binary file so that it cannot be modified while there is a process that runs the code. A user who is trying to recompile a program that is currently running in a process will get a message like "text file busy" or some such.