Presentation is loading. Please wait.

Presentation is loading. Please wait.

Solutions to Second 4330/6310 Quiz Spring 2014. First question Which of the following statements are true or false (2 points) and why? (3 points)

Similar presentations


Presentation on theme: "Solutions to Second 4330/6310 Quiz Spring 2014. First question Which of the following statements are true or false (2 points) and why? (3 points)"— Presentation transcript:

1 Solutions to Second 4330/6310 Quiz Spring 2014

2 First question Which of the following statements are true or false (2 points) and why? (3 points)

3 True or false? You cannot combine non-blocking sends with blocking receives.

4 Answer You cannot combine non-blocking sends with blocking receives.  False, this is the default for BSD socket calls send() and receive().

5 True or false? Peterson’s algorithm for mutual exclusion assumes the existence of a test-and-set instruction.

6 Answer Peterson’s algorithm for mutual exclusion assumes the existence of a test-and-set instruction.  False, Peterson’s algorithm makes no assumption about the hardware on which it will run.

7 True or false? Good programmers always put their notify operations at the end of their monitor procedures.

8 Answer Good programmers always put their notify operations at the end of their monitor procedures.  False, notify operations can be put anywhere in your code as they never release the monitor.

9 True or false? In a RPC package, one of the tasks of the user stub is to exchange messages with the user program.

10 Answer In a RPC package, one of the tasks of the user stub is to exchange messages with the user program.  False, the user stub exchanges messages with the server stub. User Program User Stub Server Stub Server Function Messages

11 True or false? Messages are reusable resources since they can be forwarded to other computers.

12 Answer Messages are reusable resources since they can be forwarded to other computers.  False, they are consumed as soon as they have been received.

13 True or false? Datagrams preserve message boundaries.

14 Answer Datagrams preserve message boundaries.  True, messages are sent and received individually.

15 Second question Consider the following solution to the mutual exclusion problem and explain when it fails (5 points) and what happens then. (5 points)

16 The code shared int reserved[2] = {0, 0};void enter_region(int pid) { int other; other = 1 - pid; // pid of other process reserved[pid] = 1; // reserve while (reserved[other] && reserved[pid]); // busy wait } // enter_region void leave_region(int pid) { reserved[pid] = 0; } // leave_region

17 The code analyzed shared int reserved[2] = {0, 0};void enter_region(int pid) { int other; other = 1 - pid; // pid of other process reserved[pid] = 1; // reserve while (reserved[other] && reserved[pid]); // busy wait } // enter_region void leave_region(int pid) { reserved[pid] = 0; } // leave_region Reserve first then check Where is the tiebreaker?

18 Answer When two processes try to enter the critical section in lockstep, a deadlock occurs

19 Third question Consider the function  void doublesquare(int *pa, int *pb) { *pa *= *pa; *pb *= *pb; } // doublesquare and assume the following calling sequence: alpha = 3; doublesquare (&alpha, &alpha);

20 Third question (I) Consider the function  void doublesquare(int *pa, int *pb) { *pa *= *pa; *pb *= *pb; } // doublesquare and assume the following calling sequence: alpha = 3; doublesquare (&alpha, &alpha);

21 Third question (II) What will be the value of alpha after the call assuming that  the call was a conventional procedure call? (5 points)  the call was a remote procedure call? (5 points)

22 Answer What will be the value of alpha after the call assuming that  the call was a conventional procedure call? (5 points) Answer: alpha = eighty-one  the call was a remote procedure call? (5 points) Answer: alpha = nine

23 Explanation In a conventional procedure call, the calling program passes to the function doublesquare the two addresses &alpha and &alpha  The function executes in sequence: *(&alpha) *= *(&alpha) // from 3 to 9 *(&alpha) *= *(&alpha) // from 9 to 81  As a result, alpha is squared twice

24 Explanation In a remote procedure call, the calling program passes to the remove function transfer the two values 3 and 3  The function executes in sequence: 3*3 = 9 3*3 = 9  These two values are returned to the calling program and assigned to alpha in an unspecified order. As a result, alpha is only squared once

25 Fourth question Give an example of an application where  Datagrams are more indicated than streams. (5 points)  Streams are more indicated than datagrams. (5 points)

26 Answer Give an example of an application where  Datagrams are more indicated than streams. (5 points) Short requests from a client to a server  Streams are more indicated than datagrams. (5 points) Transferring large files, HTTP (the web)

27 Fifth question What is the major disadvantage of busy waits? (5 points) What can we do to eliminate them? (5 points) Are there cases where it is better to keep them? (5 points)

28 Answers  Busy waits waste CPU cycles and cause unnecessary context switches.  We should use instead kernel supported solutions that move the waiting processes to the waiting state.  It is better to keep them in multicore architectures for short waits.

29 Sixth question A classroom has two doors and one switch at each door to turn the lights on and off. Complete the following template to ensure that a)the room will never contain more than 40 people and b) the light switches will always be on when there are people in the room and off when the room is empty. (5 points per correct line for a total of 25 points)

30 The function template shared int people_count = 0; semaphore mutex = 1; semaphore room = ______; room(){ _________________________________; people_count++; if(people_count == 1) toggle_switch(); ___________________________________; stay_in_room(); ___________________________________; people_count--; if(people_count == 0) toggle_switch(); ___________________________________ ; } // room

31 The main idea The problem is a variant of the readers and writers problem discussed in class. We use a mutex to ensure that people enter or leave the classroom one by one. The big difference is that we do here a P(&room) each time a person enter and a V() each time a person leaves the room.

32 The answer shared int people_count = 0; semaphore mutex = 1; semaphore room = 40 ; room(){ P(&room); P(&mutex); // order does matter people_count++; if(people_count == 1) toggle_switch(); V(&mutex) ; // could have put P(&room) here stay_in_room(); P(&mutex); // could have put V(&room) here people_count--; if(people_count == 0) toggle_switch(); V(&mutex); V(&room); // order does not matter } // room

33 A last word Your answers may be correct even though they look quite different from the ones that were presented here There are several ways to express the same ideas


Download ppt "Solutions to Second 4330/6310 Quiz Spring 2014. First question Which of the following statements are true or false (2 points) and why? (3 points)"

Similar presentations


Ads by Google