You are on page 1of 6

CS 540 - Operating Systems - Homework 2 Name:_________________

Assigned: March 3 , 2004


Due: Monday, March 15, 2004 (in class)
Please show all work on a separate sheet attached to this
sheet.
(50 points = 5%)
1. What is a semaphore? Describe two operations of a
semaphore.
(6 points)
Ans: A semaphore S is a structure consisting of two parts:
(a) an integer counter, count
(b) a queue of pids of blocked processes, Q
struct sem_struct {
int count;
queue Q;
} semaphore;
It has two indivisible (atomic) operations:
down(S): if (S > 0)
S = S - 1;
else Q.enqueue(process);
up(S): S = S + 1;
if (Q != empty) Q.dequeue();

2. What is the priority inversion problem? What is a


nonpreemptive
scheduling? What is a preemptive scheduling?
(6 points)
Ans: (a) A priority inversion problem is a situation in
which a low-priority
process is blocking a high-priority process.
(b) A nonpreemptive scheduling algorithm picks a
process to run and

then just lets it run until it blocks or until it


voluntarily
releases the CPU.
(c) A preemptive scheduling algorithm picks a process
and lets it run
for a maximum of some fixed time.

3. What is a race condition? How do we avoid race


conditions? What are
four conditions for a good solution to the critical
region (section)
problem?
(8 points)
Ans: (a) A race condition is a situation where two or more
processes are
reading or writing some shared data and the final
result depends
on who runs precisely when.
(b) To prevent race conditions, concurrent processes
must be
synchronized. Only one process can access a shared
resource
at a time.
(c) Four conditions for a good solution to the
critical region are:
1) Mutual exclusion is guaranteed.
2) Progress is maintained. No process running outside
its critical
region may block other processes.
3) Bounded waiting is assured. No process should have
to wait
forever to enter its critical region.
4) No assumptions are made about the speeds of
processes or the
number of processors (CPUs).

4. Consider the following set of processes, with the


length of the
CPU burst time given in milliseconds:
Process

Burst Time

Priority

P1
P2
P3
P4
P5

5
3
1
2
4

3
5
2
1
4

The processes are assumed to have arrived in the order


P1, P2, P3,
P4, P5, all at time 0.
(a) Draw four Gantt charts illustrating the execution of
these processes
using FCFS, SJF, a nonpreemptive priority (a larger
priority number
implies a higher priority), and RR (quantum = 1)
scheduling.
(b) What is the turnaround time of each process for each
of the
scheduling algorithms in part a?
(c) What is the waiting time of each process for each of
the scheduling
algorithm s in part a?
(d) Which of the schedules in part a results in the
minimal average
waiting time (over all processes)?
(10 points)

Ans: (a) The four Gantt charts are


0
5
8 9 11
15
+-----------+----+--+---+--------+
|
P1
| P2 |P3|P4 |
P5 | FCFS
+-----------+----+--+---+--------+
0 1
3
6
10
15
+--+---+-----+--------+----------+
|P3| P4| P2 |
P5
|
P1
| SJF
+--+---+-----+--------+----------+
0
3
7
12 13 15
+----+---------+----------+--+---+
| P2 |
P5
|
P1
|P3| P4| Priority
+----+---------+----------+--+---+

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|P1|P2|P3|P4|P5|P1|P2|P4|P5|P1|P2|P5|P1|P5|P1| RR
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
(b) Turnaround time:
| FCFS | SJF | Priority | RR
--------+------+-----+----------+-----P1
|
5 | 15 |
12
| 15
P2
|
8 | 6 |
3
| 11
P3
|
9 | 1 |
13
|
3
P4
| 11 | 3 |
15
|
8
P5
| 15 | 10 |
7
| 14
--------+------+-----+----------+-----Average | 9.6 | 7 |
10
| 10.2
(c) Waiting time:
| FCFS | SJF | Priority | RR
--------+------+-----+----------+-----P1
|
0 | 10 |
7
| 10
P2
|
5 | 3 |
0
|
8
P3
|
8 | 0 |
12
|
2
P4
|
9 | 1 |
13
|
6
P5
| 11 | 6 |
3
| 10
--------+------+-----+----------+-----Average | 6.6 | 4 |
7
| 7.2
(d) Shortest Job First (SJF) has the minimal average
waiting time.

5. Consider the following proposed solution to the dining


philosophers
problem:
semaphore fork[5] = 1,1,1,1,1; /* each semaphore is
initialized to 1 */
philosopher (int i)
executes this code */
{
while(1) {

/* philosopher i=0,1,2,3,4

think();
if (i % 2 == 1) {
DOWN(fork[i]);
DOWN(fork[(i+1)%5]);
} else {
DOWN(fork[(i+1)%5]);
DOWN(fork[i]);
}
eat();
if (i % 2 == 1) {
UP(fork[(i+1)%5]);
UP(fork[i]);
} else {
UP(fork[i]);
UP(fork[(i+1)%5]);
}}
}
(a) May the above solution lead to deadlock?
(b) What problem could occur in the above solution?
(10 points)

Ans: (a) The above solution won't lead to deadlock.


(b) The starvation would occur in the above solution.

6. P is a set of processes. R is a set of resources. E is


a set of
request or assignment edges. The sets P, R, and E are
as follows:
P = {P1, P2, P3}, R = {R1, R2, R3},
E = {P1 -> R1, P2 -> R3, P3 -> R2, R1 -> P2, R2 -> P2,
R3 -> P1,
R3 -> P3}.
R1 has one instance. R2 has one instance. R3 has two
instances.
(a) Draw the resource-allocation graph.
(b) Is there any deadlock in this situation? Briefly
Explain.

(10 points)
Ans: (a) See the graph.

(b) Consider the resource-allocation graph. Two cycles


exist in the
system.
P1 -> R1 -> P2 -> R3 -> P3 -> P1
P2 -> R3 -> P3 -> R2 -> P2
P1, P2, and P3 cannot go further. P1, P2, and P3
are deadlocked.

You might also like