You are on page 1of 7

CS4411 Operating Systems Exam 2 Solutions – Spring 2019 1

CS4411 Operating Systems Exam 2 Solutions


Spring 2019

1. [10 points] We discussed the following diagram of RCU reads and writes:

For each read in the above diagram, which version or versions of the shared state can the read observe?
Fill in the version or versions a read can read to column (i.e., Value Returned, and provide your reason
to the column 3.

Read Value Returned Reason


read1
read2
read3
read4
read5
read6
read7

Answer:
Read Value Returned Reason
read1 v0 or v1 Overlaps publish v1
read2 v2 After publish v2 , before publish v3
read3 v3 After publish v3
read4 v0 or v1 Overlaps publish v1
read5 v1 or v2 Overlaps publish v2
read6 v0 , v1 or v2 Overlaps publish v1 and v2
read7 v3 After publish v2

2. [15 points] You have learned two multiprocessor locks, the MCS (Mellor-Crummey-Scott) lock and
RCU (Read-Copy-Update) lock. Discuss each of these two locks and provide a detailed account of
the differences between these two implementations.
Answer: The major difference between MCS and RCU is that MCS is optimized for the case when
there are a significant number of waiting threads, while RCU is basically a readers/writers lock with
which the overhead for read-only is reduced at a cost of increased for non-read-only (i.e., writers)
threads. Both locks rely on atomic read-modify-write instructions (e.g., Compare-and-Swap). Note
CS4411 Operating Systems Exam 2 Solutions – Spring 2019 2

that in the MCS lock implementation the releasing loops while that thread is the only one in the list
until a new waiting thread arrives. If the lock is contended (i.e., having many waiting threads), this
loop will break almost instantly, as there is always some threads in the list. On the other hand, if the
lock is lightly used (i.e., thread waiting infrequently), the use of this loop is inefficient. See slides
13–18 of 06-Multi-Object-Sync.pdf for more details.
The RCU lock is more complex than the MCS lock. It requires the help from the thread scheduler to
determine when a grace period ends. However, the MCS locks can be implemented as a stand-alone
object without the help from other operating system components.

3. [15 points] Consider the following snapshot of a system. There are no out standing unsatisfied re-
quests for resources.

Allocation Max Need Available


R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4
P0 0 0 1 2 0 0 1 2 2 1 0 0
P1 2 0 0 0 2 7 5 0
P2 0 0 3 4 6 6 5 6
P3 2 3 5 4 4 3 5 6
P4 0 3 3 2 0 6 5 2

Answer the following questions:

• [1 points] Compute the Need array.


• [7 points] Is the system currently in a safe or unsafe state? Provide a step-by-step calculation
and use a safe sequence to justify your claim.
• [7 points] If a new request fro P2 arrives for [0, 1, 0, 0], can this request be safely granted imme-
diately? Again, use a step-by-step calculation and use a safe sequence to justify your claim.

Provide a step-by-step calculation. Otherwise, you risk a lower grade.


Answer:
Here is the Need array:

Allocation Max Need Available


R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4
P0 0 0 1 2 0 0 1 2 0 0 0 0 2 1 0 0
P1 2 0 0 0 2 7 5 0 0 7 5 0
P2 0 0 3 4 6 6 5 6 6 6 2 2
P3 2 3 5 4 4 3 5 6 2 0 0 2
P4 0 3 3 2 0 6 5 2 0 3 2 0

...........................................................................................
The following finds a safe sequence < p0 , p3 , p4 , p1 , p2 >:

(a) Because Available = [2, 1, 0, 0] ≥ p00 s Need = [0, 0, 0, 0], we run p0 and reclaim its resource.
The new Available = [2, 1, 0, 0] + p00 s Allocation = [0, 0, 1, 2] = [2, 1, 1, 2]. The possible safe
sequence is < p0 >.
(b) Then, because Available = [2, 1, 1, 2] ≥ p03 s Need = [2, 0, 0, 2], we run p3 and reclaim its re-
source. The new Available = [2, 1, 1, 2] + p03s Allocation = [2, 3, 5, 4] = [4, 4, 6, 6]. So far, the
possible safe sequence is < p0 , p3 >.
CS4411 Operating Systems Exam 2 Solutions – Spring 2019 3

(c) Because Available = [4, 4, 6, 6] ≥ p04 s Need = [0, 3, 2, 0], we run p4 and reclaim its resource. The
new Available = [4, 4, 6, 6] + p04s Allocation = [0, 3, 3, 2] = [4, 7, 9, 8]. So far, the possible safe
sequence is < p0 , p3 , p4 >.
(d) Because Available = [4, 7, 9, 8] ≥ p01 s Need = [0, 7, 5, 0], we run p1 and reclaim its resource. The
new Available = [4, 7, 9, 8] + p01s Allocation = [2, 0, 0, 0] = [6, 7, 9, 8]. So far, the possible safe
sequence is < p0 , p3 , p4 , p1 >.
(e) Because Available = [6, 7, 9, 8] ≥ p02 s Need = [6, 6, 2, 2], we run p2 and reclaim all of p2 ’s re-
sources. Now the safe sequence is < p0 , p3 , p4 , p1 , p2 >

Because all processes finish and because we have a safe sequence from Banker’s algorithm, the system
is in a safe state.
...........................................................................................
Because Request = [0, 1, 0, 0] ≤ Available = [2, 1, 0, 0], we assume that this request can be allo-
cated. The new Available = [2, 1, 0, 0] − Request = [0, 1, 0, 0] = [2, 0, 0, 0] and P2 ’s Allocation and
Need are updated as P20 s Allocation = [0, 0, 3, 4] + [0, 1,0, 0] = [0, 1, 3, 4] and P20 s Need = [6, 6, 2, 2] −
[0, 1, 0, 0] = [6, 5, 2, 2]. The updated table is the following:

Allocation Max Need Available


R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4
P0 0 0 1 2 0 0 1 2 0 0 0 0 2 0 0 0
P1 2 0 0 0 2 7 5 0 0 7 5 0
P2 0 1 3 4 6 6 5 6 6 5 2 2
P3 2 3 5 4 4 3 5 6 2 0 0 2
P4 0 3 3 2 0 6 5 2 0 3 2 0

(a) Because Available = [2, 0, 0, 0] ≥ p00 s Need = [0, 0, 0, 0], we run p0 and reclaim its resource.
The new Available = [2, 0, 0, 0] + p00 s Allocation = [0, 0, 1, 2] = [2, 0, 1, 2]. The possible safe
sequence is < p0 >.
(b) Then, because Available = [2, 0, 1, 2] ≥ p03 s Need = [2, 0, 0, 2], we run p3 and reclaim its re-
source. The new Available = [2, 0, 1, 2] + p03s Allocation = [2, 3, 5, 4] = [4, 3, 6, 6]. So far, the
possible safe sequence is < p0 , p3 >.
(c) Because Available = [4, 3, 6, 6] ≥ p04 s Need = [0, 3, 2, 0], we run p4 and reclaim its resource. The
new Available = [4, 3, 6, 6] + p04s Allocation = [0, 3, 3, 2] = [4, 6, 9, 8]. So far, the possible safe
sequence is < p0 , p3 , p4 >.
(d) Now, the remaining processes are P1 and P2 and their Request arrays are [0, 7, 5, 0] and [6, 5, 2, 2],
respectively. P1 cannot run because its Request = [0, 7, 5, 0] 6≤ Available = [4, 6, 9, 8]. P2 cannot
run either because its Request = [6, 5, 2, 2] 6≤ Available = [4, 6, 9, 8].

Because P1 and P2 cannot run, there is no safe sequence if this request is actually allocated. As a
result, P2 ’s Request = [0, 1, 0, 0] cannot be granted immediately.

4. [15 points] Consider the following snapshot of a system with five resource types R1 , R2 , R3 , R4 and
R5 , and four processes A, B, C and D:
CS4411 Operating Systems Exam 2 Solutions – Spring 2019 4

Allocation Request Available


R1 R2 R3 R4 R5 R1 R2 R3 R4 R5 R1 R2 R3 R4 R5
A 1 0 1 1 0 0 1 0 0 1 0 0 0 0 1
B 1 1 0 0 0 0 0 1 0 1
C 0 0 0 1 0 0 0 0 0 1
D 0 0 0 0 0 1 0 1 0 1

Answer the following questions:

• [5 points] Draw its resource allocation graph.


• [10 points] Is the system in a deadlock state? If the system is in a deadlock state, list all processes
that involve in a deadlock.

Provide a step-by-step calculation. Otherwise, you risk a lower grade.


Answer: The following is its resource allocation graph.

A C

R1 R2 R3 R4 R5

B D

...........................................................................................
Because Available = [0, 0, 0, 0, 1] ≥ C0 s Request = [0, 0, 0, 0, 1], we run C and reclaim its resources,
and the new Available = [0, 0, 0, 0, 1] + [0, 0, 0,1, 0] = [0, 0, 0, 1, 1]. Because this new Available is not
larger than or equal to the Request arrays of A, B, and D, we have a deadlock. The involved processes
are A, B and D.

5. [15 points] Four jobs A, B, C and D come into the system in the order as shown at the same time. The
subscript of each job is that job’s priority, where lower subscript means higher priority.

Procces A2 B4 C1 D3
CPU-Burst 7 2 3 4

Use non-preemptive scheduling applied to First-In-First-Out (FIFO), Short-Job-Next (SJB) and Pri-
ority scheduling to run these processes. You should provide the following as clear as possible:

• [12 points] For each process, calculate its waiting time and turnaround time.
• [3 points] Calculate average waiting time and average turnaround time.

Answer:
CS4411 Operating Systems Exam 2 Solutions – Spring 2019 5

(a) FIFO – First-In-First-Out

Process Start Time Running Time Turnaround Time


A 0 7 7
B 7 2 9
C 9 3 12
D 12 4 16

Therefore, Average Waiting Time = (0+7+9+12)/4 = 28/4 = 7 and Average Turnaround Time =
(7+9+12+16)/4 = 44/4 = 11.
(b) SJN – Shortest Job Next

Process Start Time Running Time Turnaround Time


B 0 2 2
C 2 3 5
D 5 4 9
A 9 7 16

Therefore, Average Waiting Time = (0+2+5+9)/4 = 16/4 = 4 and Average Turnaround Time =
(2+5+9+16)/4 = 32/4 = 8.
(c) Priority
Process Start Time Running Time Turnaround Time
C1 0 3 3
A2 3 7 10
D3 10 4 14
B4 14 2 16
Therefore, Average Waiting Time = (0+3+10+14)/4 = 27/4 = 6.75 and Average Turnaround Time
= (3+10+14+16)/4 = 43/4 = 10.75.

6. [15 points] Three jobs A, B and C come into the system in the order as shown in the table below.

Poccess Arrival Time CPU Burst Waiting Time Turnarround Time


A 0 4
B 2 5
C 4 6

The CPU Burst column has the number of time quanta that process requires. Suppose we always
in favor of the current running process. Use preemptive short-job-next (SJN) and round-robin (RR)
scheduling algorithm to process the above table. The time quantum used by the scheduling is 1. You
should (1) fill in the Waiting Time and Turnaround Time in the above table, and calculate the average
waiting time and average turnaround time. Note that you should provide a detailed step-by-step
calculation. Only providing a few numbers will receive a zero!
Answer: The following diagram shows the “history” of the execution of A, B and C under shortest
job next. A starts at zero and runs for 2 time quanta and then B arrives. At this point, A’s remaining
time quantum is 2, and B’s is 5 as shown in the diagram by A(2) and B(5). Therefore, A runs again
for 2 more time quanta. At this point, A finishes (i.e., A(0)), B still has 5 time quanta to run, and C is
a newcomer that has 6 time quanta to run. Because B has a shorter time quanta, it runs. AT the end of
each time quantum, B’s remaining time gets shorter but C’s stays same (i.e., C(6). Once B finishes, C
starts to run to finish.
CS4411 Operating Systems Exam 2 Solutions – Spring 2019 6

Therefore, the wait time and turnaround time are shown in the table below:

Poccess Arrival Time CPU Burst Waiting Time Turnarround Time


A 0 4 0 4
B 2 5 2 7
C 4 6 5 11

Average Waiting Time = (0+2+5)/3 = 7/3 = 2.3 and Average Turnaround Time = (4+7+11) = 22/3 =
7.3.
Then, we use Round-Robin and the execution “history” diagram is shown below. In this diagram, A
runs for 2 time quanta and B arrives. Because we are in favor of the running process, A is given 1
more time quantum, At the end of the 3rd time quantum, B runs. At the end of the 4th time quantum,
C arrives. Because we are in favor the running process, the CPU is given to the running processes,
which is either A or B. In this case, B just finishes its time quantum and A is the next. At the end of
the 5th time quantum, A completes. The remaining is trivial because B and C take turns, each of these
two processes uses 1 time quantum and gives the CPU to the other.

Note that the dashed line segments in the above diagram indicate waiting, and solid line segments are
running. We have the following:

Poccess Arrival Time CPU Burst Waiting Time Turnarround Time


A 0 4 1 5
B 2 5 5 10
C 4 6 5 11

Average Waiting Time = (1+5+5)/3 = 11/3 = 3.7 and Average Turnaround Time = (5+10+11) = 26/3 =
8.7.
CS4411 Operating Systems Exam 2 Solutions – Spring 2019 7

7. [15 points] Answer the following question as accurate and as detail as possible:

(a) What is Hard Real-Time Scheduling?


(b) What is Soft Real-Time Scheduling?
(c) What is Priority Inversion?

Answer:

(a) Hard Real-Time Scheduling is a real-time scheduling technique with which critical tasks must
be completed with a guaranteed amount of time.
(b) Soft Real-Time Scheduling is a real-time scheduling technique with which critical tasks re-
ceives higher priority over other processes.
(c) Priority Inversion means a high-priority process needs to access a resource that is currently
being held by a low-priority process. As a result, the high-priority process is blocked by the
low-priority process.

You might also like