Sample Midterm 2008 | Operating Systems | ECS 150, Exams of Operating Systems

Material Type: Exam; Class: Operating Systems; Subject: Engineering Computer Science; University: University of California - Davis; Term: Fall 2008;

Typology: Exams

Pre 2010

Uploaded on 07/31/2009

koofers-user-mxk
koofers-user-mxk 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ECS 150, Operating Systems Fall Quarter 2008
Sample Midterm
1. What are the values of the integer variables xand ywhen the following program completes? (If either variable
could have more than one value, say why.)
y = 2 ;
parbegin
x=y2 ;
y = 6 ;
parend ;
Answer: By the Bernstein conditions, the value of xwill be undefined, since it uses a variable that is both read
and written concurrently. The value of ywill be 6, since it is written to only at one place in the concurrent
statements.
2. Is the following true or false? Justify your answer. “When several processes access shared information in
primary storage, mutual exclusion must be enforced to prevent the production of indeterminant results.
Answer: This statement is false because it is too general. If many processes are reading shared data, and not
writing it, mutual exclusion is unnecessary. If however any process does alter that data, then mutual exclusion
becomes necessary.
3. Process A should finish before process B starts, and process B should finish before either of processes C or D
start. Show how these processes may use two semaphores to provide the necessary synchronization.
Answer: Define two semaphores Adone and Bdone, both of which are initialized to 0. Have B, C, and D execute
as their first instructions down(Adone), down(Bdone), and down(Bdone), respectively. Have A execute as its
last instruction up(Adone); this enables B to proceed past its first instruction. Have B execute two up(Bdone)s as
its last two instructions; this enables C and D to proceed past their first instructions. This arrangement provides
the necessary synchronization.
4. A bounded semaphore s is a counting semaphore that cannot exceed a given value smax >0. The corresponding
up and down operations are:
up(s): wait until s < smax; then increment s by 1
down(s): wait until s > 0; then decrement s by 1
Write a monitor to implement bounded semaphores. (Hint: assume the semaphore is to be initialized to the
constant SINIT and the maximum value is SMAX.)
typedef mo ni to r {
/s c ou n t i s t he i n t e g e r v a l ue o f th e se ma p ho re ; i f t h i s i s to o bi g
an d t h e p r o c e s s t r i e s t o u p i t , t h e p r o c e s s w i l l b l o c k on t o o b i g ;
s i m i l a r l y , i f t h e v a l u e i s t o o s m a l l an d th e p r o c e s s t r i e s t o d own
i t , t h e p r o c e s s w i l l b l o c k on t o o s m a l l . /
int scount = 0;
condition t oo b i g , t o o s m a l l ;
/s t r a i g h f o r w a r d i m p l e m e n t a t i o n o f up /
v o i d e n t r y u p ( vo i d )
{
/i f t oo b i g , w a it u n t i l o k /
while ( scount >= SMAX)
toobig . w a i t ;
/i n c r e m e n t an d n o t i f y so m eo n e
w a i t i n g f o r t h e s e ma p ho r e t o be
n o n z e r o t h a t i t i s /
Version of October 28, 2008 at 7:44am Page 1 of 2
pf2

Partial preview of the text

Download Sample Midterm 2008 | Operating Systems | ECS 150 and more Exams Operating Systems in PDF only on Docsity!

ECS 150, Operating Systems Fall Quarter 2008

Sample Midterm

1. What are the values of the integer variables x and y when the following program completes? (If either variable

could have more than one value, say why.)

y = 2 ; p a r b e g i n x = y ∗ 2 ; y = 6 ; parend ;

Answer: By the Bernstein conditions, the value of x will be undefined, since it uses a variable that is both read

and written concurrently. The value of y will be 6, since it is written to only at one place in the concurrent

statements.

2. Is the following true or false? Justify your answer. “When several processes access shared information in

primary storage, mutual exclusion must be enforced to prevent the production of indeterminant results.”

Answer: This statement is false because it is too general. If many processes are reading shared data, and not

writing it, mutual exclusion is unnecessary. If however any process does alter that data, then mutual exclusion

becomes necessary.

3. Process A should finish before process B starts, and process B should finish before either of processes C or D

start. Show how these processes may use two semaphores to provide the necessary synchronization.

Answer: Define two semaphores Adone and Bdone, both of which are initialized to 0. Have B, C, and D execute

as their first instructions down(Adone), down(Bdone), and down(Bdone), respectively. Have A execute as its

last instruction up(Adone); this enables B to proceed past its first instruction. Have B execute two up(Bdone)s as

its last two instructions; this enables C and D to proceed past their first instructions. This arrangement provides

the necessary synchronization.

4. A bounded semaphore s is a counting semaphore that cannot exceed a given value smax > 0. The corresponding

up and down operations are:

up(s): wait until s < smax; then increment s by 1

down(s): wait until s > 0; then decrement s by 1

Write a monitor to implement bounded semaphores. (Hint: assume the semaphore is to be initialized to the

constant SINIT and the maximum value is SMAX .)

t y p e d e f monitor { / ∗ s c o u n t i s t h e i n t e g e r v a l u e o f t h e semaphore ; i f t h i s i s t o o b i g and t h e p r o c e s s t r i e s t o up i t , t h e p r o c e s s w i l l b l o c k on t o o b i g ; s i m i l a r l y , i f t h e v a l u e i s t o o s m a l l and t h e p r o c e s s t r i e s t o down i t , t h e p r o c e s s w i l l b l o c k on t o o s m a l l. ∗ / i n t s c o u n t = 0 ; c o n d i t i o n t o o b i g , t o o s m a l l ;

/ ∗ s t r a i g h f o r w a r d i m p l e m e n t a t i o n o f up ∗ / v o i d e n t r y up ( v o i d ) { / ∗ i f t o o b i g , w a i t u n t i l ok ∗ / w h i l e ( s c o u n t >= SMAX) t o o b i g. w a i t ; / ∗ i n c r e m e n t and n o t i f y someone w a i t i n g f o r t h e semaphore t o be n o n z e r o t h a t i t i s ∗ /

Version of October 28, 2008 at 7:44am Page 1 of 2

ECS 150, Operating Systems Fall Quarter 2008

s c o u n t += 1 ; t o o s m a l l. s i g n a l ; }

/ ∗ s t r a i g h f o r w a r d i m p l e m e n t a t i o n o f down ∗ / v o i d e n t r y down ( v o i d ) ; b e g i n / ∗ i f t o o s m a l l , w a i t u n t i l ok ∗ / w h i l e ( s c o u n t <= 0 ) t o o s m a l l. w a i t ; / ∗ d e c r e m e n t and n o t i f y someone w a i t i n g f o r t h e semaphore t o be l e s s t h a n SMAX t h a t i t i s ∗ / s c o u n t −= 1 ; t o o b i g. s i g n a l ; } b o u n d e d s e m a p h o r e ;

5. Suppose a scheduling algorithm (at the level of short-term scheduling) favors those programs which have used

little processor time in the recent past. Why will this algorithm favor I/O bound programs and yet not perma-

nently starve CPU bound programs?

Answer: Since I/O bound jobs use little processor time due to their blocking for I/O, they will be favored over

CPU bound jobs, which use large amounts of CPU time. However, if I/O bound jobs repeatedly use the processor

and thereby prevent CPU bound jobs from acquiring it, then the amount of processor time used by CPU bound

programs in the recent past drops; eventually it will be low enough so the CPU bound process gets the CPU.

Hence this algorithm will not starve CPU bound programs.

6. Assume you have been given the following jobs with the indicated arrival and service times:

name arrival time service time

A 0 3

B 2 5

C 4 2

D 6 1

E 8 4

(a) When, and in what order, would these jobs run if the scheduling algorithm were first come first serve?

(b) When, and in what order, would these jobs run if the scheduling algorithm were shortest job next?

(c) When, and in what order, would these jobs run if the scheduling algorithm were round robin with a quan-

tum of 2? Assume that if events are scheduled to happen at exactly the same time, that new arrivals precede

terminations, which precede quantum expirations.

Answer:

(a) A (from 0 to 3), B (from 3 to 8), C (from 8 to 10), D (from 10 to 11), E (from 11 to 15)

(b) A (from 0 to 3), B (from 3 to 8), D (from 8 to 9), C (from 9 to 11), E (from 11 to 15)

(c) A (from 0 to 2), B (from 2 to 4), A (from 4 to 5), C (from 5 to 7), B (from 7 to 9), D (from 9 to 10), E

(from 10 to 12), B (from 12 to 13), E (from 13 to 15)

Version of October 28, 2008 at 7:44am Page 2 of 2