more review questions for the second midterm cosc 4330/6310

74
More review questions for the second midterm COSC 4330/6310

Upload: zoe-fletcher

Post on 05-Jan-2016

218 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: More review questions for the second midterm COSC 4330/6310

More review questions for the second midterm

COSC 4330/6310

Page 2: More review questions for the second midterm COSC 4330/6310

Note

This presentation contains questions on topics that are on thee second midterm.

Depending on the semester, some questions will overlap with the materials that were discussed in the review session.These materials are based on the questions

on the most recent second quiz.

Page 3: More review questions for the second midterm COSC 4330/6310

Scheduling

How would you simulate a round-robin policy with a time slice of 100 ms on a System V R 4 system?

#ts_quantum ts_tqexp ts_slpret ts_maxwait ts_lwait LVL

Page 4: More review questions for the second midterm COSC 4330/6310

Answer

How would you simulate a round-robin policy with a time slice of 100 ms on a System V R 4 system?

#ts_quantum ts_tqexp ts_slpret ts_maxwait ts_lwait LVL

100 0 0 100000 0 #0

Page 5: More review questions for the second midterm COSC 4330/6310

Scheduling

Consider the following System V Release 4 scheduler:

#ts_quantum ts_tqexp ts_slpret ts_maxwait ts_lwait LVL 1000 0 1 8000 1 # 0 500 0 2 4000 2 # 1 200 1 3 2000 3 # 2 100 2 3 1000 3 # 3

and assume that a process at priority level 2 receives 800 ms of CPU time before doing an I/O.

Page 6: More review questions for the second midterm COSC 4330/6310

Answer

1. Start at priority 2 and get 200 ms of CPU time before returning to the ready queue

2. New priority given by __________

Page 7: More review questions for the second midterm COSC 4330/6310

Answer

1. Start at priority 2 and get 200 ms of CPU time before returning to the ready queue

2. New priority given by ts_tqexp

3. Move to priority 1 and get 500 ms of CPU time before returning to the ready queue

4. New priority given by _______

Page 8: More review questions for the second midterm COSC 4330/6310

Answer

1. Start at priority 2 and get 200 ms of CPU time before returning to the ready queue

2. New priority given by ts_tqexp

3. Move to priority 1 and get 500 ms of CPU time before returning to the ready queue

4. New priority given by ts_tqexp

5. Move to priority 0 and get 100ms of CPU time before doing an I/O

6. New priority given by _______

Page 9: More review questions for the second midterm COSC 4330/6310

Answer

1. Start at priority 2 and get 200 ms of CPU time before returning to the ready queue

2. New priority given by ts_tqexp

3. Move to priority 1 and get 500 ms of CPU time before returning to the ready queue

4. New priority given by ts_tqexp

5. Move to priority 0 and get 200ms of CPU time before doing an I/O

6. New priority given by ts_slpret: level 1

Page 10: More review questions for the second midterm COSC 4330/6310

Another answer (I)

Step 1: Process is at priority level 2#ts_quantum ts_tqexp ts_slpret ts_maxwait ts_lwait LVL 1000 0 1 8000 1 # 0 500 0 2 4000 2 # 1 200 1 3 2000 3 # 2 100 2 3 1000 3 # 3

and requests 800 ms of CPU time It gets only 200 ms and returns to the CPU queue at the level

indicated by ts_tqexp

Page 11: More review questions for the second midterm COSC 4330/6310

Another answer (II)

Step 2: Process is now at priority level 1#ts_quantum ts_tqexp ts_slpret ts_maxwait ts_lwait LVL 1000 0 1 8000 1 # 0 500 0 2 4000 2 # 1 200 1 3 2000 3 # 2 100 2 3 1000 3 # 3

and requests 600 ms of CPU time It gets only 500 ms and returns to the CPU queue at the level

indicated by ts_tqexp

Page 12: More review questions for the second midterm COSC 4330/6310

Another answer (III)

Step 3: Process is now at priority level 0#ts_quantum ts_tqexp ts_slpret ts_maxwait ts_lwait LVL 1000 0 1 8000 1 # 0 500 0 2 4000 2 # 1 200 1 3 2000 3 # 2 100 2 3 1000 3 # 3

and requests 100 ms of CPU time It gets only the 100 ms then goes to the waiting state

Page 13: More review questions for the second midterm COSC 4330/6310

Another answer (IV)

Step 4: Process is still at priority level 0#ts_quantum ts_tqexp ts_slpret ts_maxwait ts_lwait LVL 1000 0 1 8000 1 # 0 500 0 2 4000 2 # 1 200 1 3 2000 3 # 2 100 2 3 1000 3 # 3

When it returns to the ready queue, it gets upgraded to the priority level specified by ts_slpret Goes to priority level 1

Page 14: More review questions for the second midterm COSC 4330/6310

Short question

What is the major limitation of non-preemptive scheduling policies?

Page 15: More review questions for the second midterm COSC 4330/6310

Answer

Non-preemptive policies allow processes to monopolize the CPU.

Page 16: More review questions for the second midterm COSC 4330/6310

Short question

What does the System V Release 4 scheduler do to avoid process starvation?

Page 17: More review questions for the second midterm COSC 4330/6310

Answer

It increases the priority of processes that have waited more than ts_maxwait time units in the ready queue.

Page 18: More review questions for the second midterm COSC 4330/6310

Short question

What is the idea behind penalizing processes that have already got their share of the CPU?

Page 19: More review questions for the second midterm COSC 4330/6310

Answer

To ensure that the other processes can get their fair share of the CPU.

Page 20: More review questions for the second midterm COSC 4330/6310

Short question

What is the difference between blocking sends and non-blocking sends?

Page 21: More review questions for the second midterm COSC 4330/6310

Answer

A blocking send waits until the message has been received by the process to which it was addressed

A non-blocking send returns as soon as the message has been accepted for delivery.Like a letter dropped in a mailbox.

Page 22: More review questions for the second midterm COSC 4330/6310

Short question

Can you simulate a non-blocking send—or receive—using only blocking sends and receives?

Page 23: More review questions for the second midterm COSC 4330/6310

Answer

No, but you can simulate blocking sends—or receives—using only non-blocking sends and receives.

Page 24: More review questions for the second midterm COSC 4330/6310

Short question

What is the difference between virtual circuits and datagrams?

Page 25: More review questions for the second midterm COSC 4330/6310

Answer

Virtual circuits are connection-oriented and ensure that all messages will arrive in the right order without any of them being lost, damaged or duplicated.

Datagrams are sent individually.

Page 26: More review questions for the second midterm COSC 4330/6310

Short question

How would you pass a linked list to a remote procedure?

Page 27: More review questions for the second midterm COSC 4330/6310

Answer

By storing it in array along with unpacking instructions.

A B C D

LL 4 A B C D

Page 28: More review questions for the second midterm COSC 4330/6310

Short question

How can we solve the big-endian/little endian issue?

Page 29: More review questions for the second midterm COSC 4330/6310

Answer

We can require all transfers to use an arbitrary network order, the same for all computers.

Page 30: More review questions for the second midterm COSC 4330/6310

Short question

What is the major advantage of idempotent procedures?

Page 31: More review questions for the second midterm COSC 4330/6310

Answer

Idempotent procedures can be repeated an arbitrary number of times without causing any harm.

Hence, we do not have to worry about incomplete executions. We restart the procedure.

Page 32: More review questions for the second midterm COSC 4330/6310

Short question

What is the main disadvantage of atomic transactions?

Page 33: More review questions for the second midterm COSC 4330/6310

Answer

Their cost.

At the same time they remain indispensable in financial transactions.

Page 34: More review questions for the second midterm COSC 4330/6310

Short question

What is the difference between the at most once and the all or nothing semantics in RPCs?

Page 35: More review questions for the second midterm COSC 4330/6310

Answer

The at most once semantics guarantees that no RPC call will be executed more than once but does not prevent partial executions, which the all or nothing semantics does.

Page 36: More review questions for the second midterm COSC 4330/6310

Busy waits

Why should we avoid busy waits?

Is this always true?

Page 37: More review questions for the second midterm COSC 4330/6310

Answer

Busy waits waste computing cycles. There are especially bad on single processor architectures because the process doing the busy wait will repeatedly interrupt the process on which it is waiting.

They are the best solution for short waits on multiprocessor/multicore architectures as long as the expected wait time is less than to context switches.

Page 38: More review questions for the second midterm COSC 4330/6310

Problem Consider the function

transfer(int *from, int *to, int amount) {*from -= amount;*to += amount;

} // transfer

and assume the calling sequence:

alpha = 100; beta = 200;transfer (&alpha, &alpha, 10)

Page 39: More review questions for the second midterm COSC 4330/6310

Problem (continued)

What will be the value of alpha after the call assuming that

(a) the call was a regular procedure call?

(b) the call was a remote procedure call?

Page 40: More review questions for the second midterm COSC 4330/6310

Solution

(a) If the call was a regular procedure call, a = 100

(b) If the call was a remote procedure call, a = 90 or 110

Page 41: More review questions for the second midterm COSC 4330/6310

Explanation With a regular procedure call

Calling Program

&alpha, &alpha, 10

Debits &alpha

Credits same amount to &alpha

alpha = 100

doubletrouble

Page 42: More review questions for the second midterm COSC 4330/6310

Explanation (cont'd) With a remote procedure call

Calling Program

doubletrouble

100, 100, 10

alpha = 90 or 110

90, 110

Page 43: More review questions for the second midterm COSC 4330/6310

What is wrong?

shared int reserved[2] = {0, 0};

void enter_region(int pid) { while (reserved[1-pid]); // busy wait reserved[pid] = 1; } // enter_region

void leave_region(int pid) { reserved[pid] = 0;} // leave_region

Page 44: More review questions for the second midterm COSC 4330/6310

Answer

The functions do not guarantee mutual exclusion when processes 0 and 1 enter in lockstep.The two critical steps are:

while (reserved[1-pid]); // test lock reserved[pid] = 1; // set lock

Page 45: More review questions for the second midterm COSC 4330/6310

The ice-cream parlor An ice-cream parlor has two employees selling

ice cream and six seats for its customers. Each employee can only serve one customer at a time and each seat can only accommodate one customer at a time. Add the required semaphores to the following program skeleton to guarantee that customers will never have to wait for a chair with a melting ice-cream in their hand.

Page 46: More review questions for the second midterm COSC 4330/6310

The ice-cream parlor (cont'd)

semaphore _______________ = ______;semaphore _______________ = ______;customer (int who) {

______________________________order_ice_cream();______________________________eat_it();______________________________

} // customer

Page 47: More review questions for the second midterm COSC 4330/6310

Sketching a solution Two resources are shared by all customers

Six seatsTwo employees

Questions to ask areWhen should we request a resource? In which order? (very important)When should we release it?

Page 48: More review questions for the second midterm COSC 4330/6310

Solution

semaphore _seats__________ = __6___;semaphore _employees______ = _ 2___;customer (int who) {

______________________________order_ice_cream();______________________________eat_it();______________________________

} // customer

Page 49: More review questions for the second midterm COSC 4330/6310

Solution (cont'd)

semaphore _seats__________ = __6___;semaphore _employees_____ = __2___;customer (int who) {

P(&seats); P(&employees);________;order_ice_cream();______________________________;eat_it();______________________________;

// customer

Get seat first

Page 50: More review questions for the second midterm COSC 4330/6310

Solution (cont'd)

semaphore _seats__________ = __6___;semaphore _employees______ = __2___;customer (int who) {

P(&seats); P(&employees);________order_ice_cream();V(&employees);_________________eat_it();______________________________

} // customer What is missing?

Page 51: More review questions for the second midterm COSC 4330/6310

Solution (cont'd)

semaphore _seats__________ = __6___;semaphore _employees______ = __2___;customer (int who) {

P(&seats); P(&employees);________order_ice_cream();V(&employees);_________________eat_it();V(&seat);______________________

} // customer

Page 52: More review questions for the second midterm COSC 4330/6310

The pizza oven

A pizza oven can contain nine pizzas but the oven narrow opening allows only one cook at a time to either put a pizza in the oven or to take one out. Given that there will be more than one cook preparing pizzas at any given time, complete the missing lines in the following C procedure.

Page 53: More review questions for the second midterm COSC 4330/6310

The pizza oven (cont'd)

semaphore oven = ______; semaphore access = _______;make_pizza(int size, int toppings) {

prepare_pizza(size, toppings);______________________________________put_into_oven();______________________________________wait_until_done();______________________________________take_from_oven();______________________________________

} // make_pizza

Page 54: More review questions for the second midterm COSC 4330/6310

Sketching a solution The two resources are already identified

The ovenAccess to the oven (mutex)

We ask the usual questionsAnd take care of avoiding

mutex-induced deadlocks

Page 55: More review questions for the second midterm COSC 4330/6310

Solution

semaphore oven = ___9__; semaphore access = __1__; // the mutexmake_pizza(int size, int toppings) {

prepare_pizza(size, toppings);______________________________________put_into_oven();______________________________________wait_until_done();______________________________________take_from_oven();______________________________________

} // make_pizza

Page 56: More review questions for the second midterm COSC 4330/6310

Solution (cont'd)

semaphore oven = ___9__; semaphore access = __1__; // the mutexmake_pizza(int size, int toppings) {

prepare_pizza(size, toppings);P(&oven); P(&access);__________________put_into_oven();______________________________________wait_until_done();______________________________________take_from_oven();______________________________________

} // make_pizza

Order matters!

Page 57: More review questions for the second midterm COSC 4330/6310

Solution (cont'd)

semaphore oven = ___9__; semaphore access = __ 1__; // the mutexmake_pizza(int size, int toppings) {

prepare_pizza(size, toppings);P(&oven); P(&access); ____________________put_into_oven();V(&access);____________________________wait_until_done();P(&access);____________________________take_from_oven();V(&oven); V(&access); // IN ANY ORDER!____

} // make_pizza

Page 58: More review questions for the second midterm COSC 4330/6310

The pizza oven (again) Redo problem 2 using monitors.

Page 59: More review questions for the second midterm COSC 4330/6310

The pizza oven (again)

Class oven {private int n_pizzas; // in ovenprivate condition not_full;public void synchronized put_a_pizza() {

______________________________________________________________put_a_pizza_in_the_oven();____________________________________________________________

} // put_a_pizza()

Page 60: More review questions for the second midterm COSC 4330/6310

The pizza oven (again)

(Class oven continued)

public void synchronized remove_a_pizza() {____________________________________________________________take_a_pizza_from_the_oven();____________________________________________________________

} // remove_a_pizza()

Page 61: More review questions for the second midterm COSC 4330/6310

The pizza oven (again)(Class oven continued)

oven() {n_pizzas = 0;

} // constructor

} // Class oven

Page 62: More review questions for the second midterm COSC 4330/6310

Sketching a solution

Two monitor procedures instead of a C functionEach procedure handles a step that must

be performed in mutual exclusion Questions to ask are

When do we have to wait on a condition? Must be specific

Which conditions do we need to signal?

Page 63: More review questions for the second midterm COSC 4330/6310

Solution

Class oven {private int n_pizzas; // in ovenprivate condition not_full;public void synchronized put_a_pizza() {

if (n_pizzas == 9) notfull.wait; _____n_pizzas++;____________________put_a_pizza_in_the_oven();____________________________________________________________

} // put_a_pizza()

Page 64: More review questions for the second midterm COSC 4330/6310

Solution (cont'd)(Class oven continued)

public void synchronized remove_a_pizza() {

________________________________________________________

take_a_pizza_from_the_oven();n_pizzas--;___________________notfull.signal;_________________

} // remove_a_pizza()

Page 65: More review questions for the second midterm COSC 4330/6310

Solution (cont'd)(Class oven continued)

oven() {n_pizzas = 0;

} // constructor

} // Class oven

Page 66: More review questions for the second midterm COSC 4330/6310

More on monitors How should you modify a monitor when you

replace its signal calls by notify calls?

Page 67: More review questions for the second midterm COSC 4330/6310

More on monitors

How should you modify a monitor when you replace its signal calls by notify calls?

Page 68: More review questions for the second midterm COSC 4330/6310

Answer

How should you modify a monitor when you replace its signal calls by notify calls?

All if(…) clauses preceding waits should be replaced by while(…) clauses.

Page 69: More review questions for the second midterm COSC 4330/6310

True or false

You should always initialize mutex semaphores to one.

Page 70: More review questions for the second midterm COSC 4330/6310

Answer

You should always initialize mutex semaphores to one.

TRUE

Page 71: More review questions for the second midterm COSC 4330/6310

True or false

You should always initialize monitor conditions to zero.

Page 72: More review questions for the second midterm COSC 4330/6310

Answer

You should always initialize monitor conditions to zero.

FALSEmonitor conditions have NO VALUE

Page 73: More review questions for the second midterm COSC 4330/6310

True or false

Many good programmers prefer to put all their signal operations at the end of their monitor procedures.

Page 74: More review questions for the second midterm COSC 4330/6310

Answer

Many good programmers prefer to put all their signal operations at the end of their monitor procedures.

TRUEIssuing a signal means risking to be interrupted in the middle of its critical section