Download - Handout Week5
Banker’s algorithm example:
4 lawyers (P1, P2, P3, P4)
4 chopsticks - single resource with Available[r] = 4
Each lawyer needs 2 chopsticks: Need[0..p][r] = 2
(1) Lawyer P1 has 2 chopsticks already
Alloc Max Avail Finish
P1 2 2 2 N
P2 0 2 N
P3 0 2 N
P4 0 2 N
(2) P2 asks for a chopstick � Go ahead and "allocate" it:
Alloc Max Avail Finish
P1 2 2 1 N
P2 1 2 N
P3 0 2 N
P4 0 2 N
Safety test: Available resources >= Max needed by ANY process
Work[0..r] = Available[0..r]
Finish[0..p] = false
Fill in the following matrices for a safety test:
Work =
Alloc Max Need Finish
P1 2 2
P2 1 2
P3 0 2
P4 0 2
Work =
Alloc Max Need Finish
P1 2 2
P2 1 2
P3 0 2
P4 0 2
Work =
Alloc Max Need Finish
P1 2 2
P2 1 2
P3 0 2
P4 0 2
Work =
Alloc Max Need Finish
P1 2 2
P2 1 2
P3 0 2
P4 0 2
Different Scenario:
(1) P1, P2, and P3 have grabbed and P4 is about to grab one.
Alloc Max Avail Finish
P1 1 2 1 N
P2 1 2 N
P3 1 2 N
P4 0 2 N
(2) P2 asks for a chopstick � Go ahead and "allocate" it:
Alloc Max Avail Finish
P1 1 2 0 N
P2 1 2 N
P3 1 2 N
P4 1 2 N
Check safe state again:
Example 2:
Suppose we have 4 resources (R1-4) and 5 threads (T1-5). Max resources are: R1 = 6, R2 = 7, R3 = 12, R4 =12.
Then we have the following situation:
Thread Current Allocation Max Allocation
R1 R2 R3 R4 R1 R2 R3 R4
T1 0 0 1 2 0 0 1 2
T2 2 0 0 0 2 7 5 0
T3 0 0 3 4 6 6 5 6
T4 2 3 5 4 4 3 5 6
T5 0 3 3 2 0 6 5 2
1) What two advantages do threads have over multiple processes?
A.
a. Threads have separate memory space;
b. Threads don't destroy shared data;
B.
a. Threads are inexpensive to create and destroy;
b. Threads use very little resources while they exist;
C.
a. Threads don't have to trust each other;
b. Threads don't have separate memory space;
2) A rendezvous requires
A. no buffering
B. double buffering
C. a buffer queue
D. none of the above
3) Assume that there are four queues in a multilevel queue scheduling system:
one first-come first-served queue and three round-robin queues with quantum = 8, 16, and 32 milliseconds respectively. What is the
quantum of the first-come first-served queue?
A. 8 milliseconds
B. 64 milliseconds
C. 32 milliseconds
D. none of the above
4) Consider the 2-process solution to the critical section problem (i refers to the current process and j is the other one)
repeat
flag[i] := true ;
while (flag[j]) do no-op ;
<critical-section>
flag[i] := false ;
<remainder-section>
until false ;
This solution does NOT satisfy :
A Mutual Exclusion
B Progress
C Bounded Wait
D both (b) and (c)
E none of the above
5) Which one of the following describes which allocation schemes (Contiguous, Indexed, Linked) suffer(s) from external
fragmentation?
A. Contiguous only
B. Indexed only
C. Linked only
D. Contiguous and Indexed
E. Indexed and Contiguous
6) “Only one process may use a resource at a time” best describes which one of the following?
A. Mutual Exclusion
B. Hold and wait
C. No preemption
D. Circular wait
E. Starvation
7) “A process may hold allocated resources while awaiting assignment of other resources” best describes which one of the following
conditions?
A. Mutual Exclusion
B. Hold and wait
C. No preemption
D. Circular wait
E. Starvation
8) “No resource can be forcibly removed from a process holding it” best describes which one of the following conditions?
A. Mutual Exclusion
B. Hold and wait
C. No preemption
D. Circular wait
E. Starvation
9) Which problem is avoided by defining a linear ordering of resource types that is followed by all processes?
A. Mutual Exclusion
B. Hold and wait
C. No preemption
D. Circular wait
E. Starvation
Consider a simple one-level paging system with the following parameters: page size of 16 Kbytes; 64 bit logical address space; each
page table entry takes 8 bytes.
10a) How many logical pages are there (expressed as a power of 2)?
A. 240
B. 248
C. 250
D. 252
E. 253
10b) In this system, how large is the page table size in bytes?
A. 240
B. 248
C. 250
D. 252
E. 253
11a) Consider the following three threads executing different paths of execution. Determine if and where deadlock can occur for each
thread. For example, “Thread 1 can deadlock on Line 2” would mean that Thread 1 can deadlock and wait forever to acquire mutex_e.
Thread 1 Thread 2 Thread 3
lock(mutex_d) lock(mutex_e) lock(mutex_f)
lock(mutex_e) lock(mutex_f) lock(mutex_d)
e = d * 2 f = f + e d = d * f
unlock(mutex_e) unlock(mutex_f) unlock(mutex_d)
unlock(mutex_d) unlock(mutex_e) unlock(mutex_f)
Complete the following, or write “deadlock cannot occur”
Thread 1 can deadlock on line ___
Thread 2 can deadlock on line ___
Thread 3 can deadlock on line ___
11b) Determine if and where deadlock can occur.
Thread 1 Thread 2
lock(mutex_h) lock(mutex_g)
lock(mutex_i) lock(mutex_i)
lock(mutex_j) i = g + g
j = i + h unlock(mutex_i)
unlock(mutex_j) unlock(mutex_g)
unlock(mutex_h)
lock(mutex_g)
g = g + i
unlock(mutex_g)
unlock(mutex_i)
Complete the following, or write “deadlock cannot occur”
Thread 1 can deadlock on line ___
Thread 2 can deadlock on line ___
12. What is the difference between an interrupt and an exception? Give one example of each.
13. Why do we need two modes of execution (kernel and user) in an operating system, even though some of the kernel functions run
in user mode?
14. How can you change a program without changing its process? How can you change a process keeping the same program?
15. What is the difference between a context switch and a process being dispatched? Do we require a context switch for the process to
be dispatched? Do we require a process to be dispatched for context switch?
16. What is the difference between deadlock avoidance and deadlock prevention?
17. One of the ways to prevent deadlocks was given as creating a total order on the resources and allocating resources in the order of
enumeration. Show by an example why a partial order will not be sufficient to achieve the same.
18. Consider the following program:
const int n = 10;
int tally;
void total() {
int count;
for ( count = 0; count < n; count++ )
tally++;
}
int main() {
tally = 0;
cobegin
total();
total();
coend
printf ( "%d\n", tally );
return ( 0 );
}
Determine the proper lower bound and upper bound on the final value of the shared variable tally output by this concurrent program.
Assume processes can execute at any relative speed and that a value can only be incremented after it has been loaded into a register by
a separate machine instruction.
19. Three processes share four resource units from the same resource class that can be reserved and released only one at a time. This
can be done in any order, that is, we do not have total order imposed on requests. Each process needs a maximum of two units. Can we
have a deadlock in the system? Explain your answer.
What is the difference between an unsafe state and a deadlock state?
20. Consider the workload in Table:
Process Burst Time(ms) Priority Arrival Time (ms)
P1 50 4 0
P2 20 1 0
P3 100 3 0
P4 40 2 0
a. Provide schedule using First Come First Serve (FCFS), Shortest-Remaining-Time-First (SRTF), Round-Robin (RR) with timeslice
quantum = 30 ms.
b. What is the average waiting time of the above specified scheduling policies?
21. Repeat above with following:
Process Burst Time(ms) Priority Arrival Time (ms)
P1 50 4 0
P2 20 1 20
P3 100 3 40
P4 40 2 60
a. Provide schedule using First Come First Serve (FCFS), Shortest-Remaining-Time-First (SRTF), Round-Robin (RR) with timeslice
quantum = 30 ms.
b. What is the average waiting time of the above specified scheduling policies?
• FCFS
o Keep the CPU until thread blocks
o Simple, but short jobs get stuck behind long ones (Convoy problem)
• RR
o Each thread runs for at most time quantum q, then preempted
o q >> context-switch time, else too much overhead
o Fair to short jobs (good response time)
• SJF (Shortest Job First) (optimal if no preemption, unfair)
o Shortest time to completion first
• SRTF (Shortest Remaining Time First) (optimal, unfair)
o Preemptive version of SJF
• Adaptive SRTF
o Estimate SRT based on previous bursts
• Multi-Level Feedback Scheduling
o Multiple queues, each with their own scheduling algorithm (e.g foreground - RR, background FCFS)
o Scheduling between queues: priority scheduling or time slice
• Lottery Scheduling
o At least one ticket assigned to each thread, more tickets to short running jobs
o Why at least one ticket? Prevent starvation
Solutions for previous week:
Reader() {
// First check self into system
lock.Acquire();
if (WW > 0){
WR++;
okToRead.wait(&lock);
WR--;
}
while ((AW) > 0) { // Is it safe to read?
WR++; // No. Writers exist
okToRead.wait(&lock); // Sleep on cond var
WR--; // No longer waiting
}
AR++; // Now we are active!
lock.release();
// Perform actual read-only access
AccessDatabase(ReadOnly);
// Now, check out of system
lock.Acquire();
AR--; // No longer active
if (AR == 0 && WW > 0) // No other active readers
okToWrite.signal(); // Wake up one writer lock.Release(); }
Writer() {
// First check self into system
lock.Acquire();
while ((AW + AR) > 0) { // Is it safe to write?
WW++; // No. Active users exist
okToWrite.wait(&lock); // Sleep on cond var
WW--; // No longer waiting
}
AW++; // Now we are active!
lock.release();
// Perform actual read/write access
AccessDatabase(ReadWrite);
// Now, check out of system
lock.Acquire();
AW--; // No longer active
if (WR > 0) { // Otherwise, wake reader
okToRead.broadcast(); // Wake all readers }
else if (WW > 0){ // Give priority to writers
okToWrite.signal(); // Wake up one writer }
lock.Release(); }
Alternative:
Reader() {
rlock.acquire()
counterlock.acquire()
counter++
if (counter == 1)
wlock.acquire()
counterlock.release()
rlock.release()
AccessDatabase(readOnly)
counterlock.acquire()
counter-
if (counter == 0)
wlock.release()
counterlock.release()
}
Writer() {
rlock.acquire()
wlock.acquire()
AccessDatabase(readWrite)
wlock.release()
rlock.release()}