You are on page 1of 5

1

Final Examination of Operating Systems
2009/01/08 (Thur.)

1. (5%) For deadlock prevention that a process requests all needed resources prior to commencement of
execution, what condition does the approach try to prevent?
(a) hold and wait (b) mutual exclusion
(c) no preemption (d) circular wait
 a
2. (5%) Which of the following components of program state are shared across threads in a
multithreaded process?
(a) register values (b) heap memory
(c) stack memory (d) global variables
 b,d
3. (5%) Which are correct for process states?
(a) If a process is created, it will enter the "running" state
(b) If the waiting event of a process occurs, the process will enter the "running" state
(c) If a running process executes I/O, it will enter the "waiting" state
(d) If a running process encounters an interrupt, it will enter the "ready" state
 c,d
4. (5%) Which are correct for process synchronizations?
(a) A semaphore, apart from initialization, is accessed only through two standard atomic operations:
wait and signal.
(b) Mutual-exclusion can be implemented with TestAndSet or Swap instructions.
(c) A spinlock is not useful when locks are expected to be held for short times.
(d) A counting semaphore can be implemented using binary semaphores.
 a,b,d
5. (10% )
(a) What is the critical section of a process?
(b) Which three requirements must be satisfied for a solution to the critical section problem?
 (a) The critical section of a process is a segment of code which changes common variables, updating
a table, writing a file, and so on.
 (b) mutual exclusion, progress, bounded waiting
6. (10%)
(a) What is the degree of multiprogramming?
(b) Which scheduler controls the degree of multiprogramming?
 (a) The number of processes in memory
 (b) The long-term and medium-term schedulers control the degree of multiprogramming.
2

7. (10% )
(a) What is starvation?
(b) What is the solution to the above problem?
 (a) A priority scheduling algorithm can leave some low-priority processes waiting indefinitely. Such
a problem is called starvation, or indefinite blocking.
(b) Aging is a solution to the starvation problem. It gradually increases the priority of processes that
wait in the system for a long time.
8. (10%) The sets of P, R and E ( P = Process, R = Resource, E = Edge ) are listed as follows:
P = { P
1
, P
2
, P
3
, P
4
}
R = { R
1
, R
2
, R
3
}
E = { P
1
->R
1
, P
2
->R
3
, P
3
->R
2
, P
4
->R
2
, R
1
->P
3
, R
3
->P
1
}
One instance of resource type R
1
and R
3

Two instances of resource type R
2
.
(a) Draw the resource allocation graph.
(b) Is there a deadlock? Why?
 (a)

 (b) No. Because there is no cycle.
9. (10%) Explain why spinlocks are not appropriate for uniprocessor systems yet may be suitable for
multiprocessors systems.
 In a uniprocessor system, if a thread of control uses a spinlock, and begins spinning on it because the
lock is already held, the lock must be held by a process that is not executing. Spinning on the lock is
thus a huge waste of time because the process spins through the loop continuously looking at a
semaphore variable that cannot change value until the spinning process gives up the CPU and
permits the process holding the lock to execute again, thus giving it a chance to finish using the lock
3
and to release it. In a multiprocessor system, a process spins could be “unlocked” by the process that
holds the lock running on another processor.
10. (10%) Suppose that the following processes arrive for execution at the times indicated. Each process
will run the listed amount of time. In answering the questions, use nonpreemptive scheduling and
base all decisions on the information you have at the time the decision must be made.
Process Arrival Time Burst Time
P1 0.0 8
P2 0.5 4
P3 1.0 1
(a) What is the average turnaround time for these processes with the FCFS scheduling algorithm?
(b) What is the average turnaround time for these processes with the SJF scheduling algorithm?
 (a)

 (b)

11. (10%) The semaphore can be used to solve the readers-writers problem. The reader processes share
the following data structure:
semaphore mutex, wrt;
int readcount;
The semaphores mutex and wrt are initialized to 1; readcount is initialized to 0. The semaphore wrt
is common to both reader and writer processes. The codes for both writer and reader processes are
shown in the following figures. Please fill in the blanks.
do { do {
wait(wrt) ; wait(mutex);
…… readcount ++;
// writing is performed if ( )
……
signal(wrt); signal(mutex);
} while (TRUE); ……
// reading is performed
(a)
(b)
(c)
4
……
wait(mutex);
readcount --;
if ( )

signal(mutex);
} while(TRUE);

The structure of a writer process The structure of a reader process
 (a) readcount == 1 (b) wait(wrt); (c) readcount==0 (d) signal(wrt);
12. (10%) Consider the following snapshot of a system:
Allocation Max Available
ABCD ABCD ABCD
P
0
0 0 1 2 0 0 1 2 1 6 2 0
P
1
1 0 0 0 1 7 5 0
P
2
1 3 5 4 2 3 5 6
P
3
0 6 3 2 0 6 5 2
P
4
0 0 1 4 0 6 5 6

Answer the following questions using the Banker’s algorithm:
(a) What is the content of the matrix Need ?
(b) Is the system in a safe state? Why?
(c) If a request from process P
1
arrives for (0, 4, 2, 0), can the request be granted immediately?
 (a) The values of Need for processes P
0
through P
4
respectively are (0, 0, 0, 0), (0, 7, 5, 0),
(1, 0, 0, 2), (0, 0, 2, 0), and (0, 6, 4, 2).
(b) Yes. One safe sequence: P
0
,P
2
,P
1
,P
3
,P
4

 (c) Suppose the request is granted to P1, then the snap shot becomes:
Allocation Max Available
ABCD ABCD ABCD
P0 0 0 1 2 0 0 1 2 1 2 0 0
P1 1 4 2 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
The values of Need for processes P
0
through P
4
respectively are (0, 0, 0, 0), (0, 3, 3, 0),
(1, 0, 0, 2), (0, 0, 2, 0), and (0, 6, 4, 2).
One safe sequence still exits: P
0
,P
2
,P
1
,P
3
,P
4
, so the request can be granted immediately.
13. (10%) Consider the following 3-process concurrent program which uses semaphores S1, S2, and S3.
The semaphore operation, which are sometimes called “wait” and “signal”, are denoted here with the
classical notation of "P" and "V".
5
Process 1 Process 2 Process 3
L1: P(S2); L2: P(S1); L3: P(S3);
print("U"); print("D"); print("Y");
V(S1); V(S3); V(S2);
goto L1; gotoL2; goto L3;
(a) Are there initial values that can be given to the semaphores so that the processes cooperate to
print the string DYUDYUDYU? If so, give the initial values (tell which value is to be used for
which semaphore) and explain how the string is printed.
(b) Suppose the initial values are S1=0, S2=0, S3=0. Is it possible for the processes to cooperate to
produce a string that begins with DDYYUU? Explain your answer.
 (a) Yes. S1 = 1, S2 = 0, S3 = 0
 (b) No. (Explanation is omitted.)

14. (10%) Please translate the following English/Chinese words into Chinese/English.
(a) 互相 (adj.) : mutual
(b) 同步化 (verb) : synchronize
(c) 多樣的 (adj.) : various
(d) short-term : 短期的; 短時間的
(e) interactive : 互動的