33
Solutions to Second 4330/6310 Quiz Spring 2014

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)

Embed Size (px)

Citation preview

Page 1: 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)

Solutions to Second4330/6310 Quiz

Spring 2014

Page 2: 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)

First question

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

Page 3: 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)

True or false?

You cannot combine non-blocking sends with blocking receives.

Page 4: 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)

Answer

You cannot combine non-blocking sends with blocking receives.

False, this is the default for BSD socket calls send() and receive().

Page 5: 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)

True or false?

Peterson’s algorithm for mutual exclusion assumes the existence of a test-and-set instruction.

Page 6: 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)

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.

Page 7: 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)

True or false?

Good programmers always put their notify operations at the end of their monitor procedures.

Page 8: 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)

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.

Page 9: 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)

True or false?

In a RPC package, one of the tasks of the user stub is to exchange messages with the user program.

Page 10: 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)

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

ServerStub

ServerFunction

Messages

Page 11: 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)

True or false?

Messages are reusable resources since they can be forwarded to other computers.

Page 12: 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)

Answer

Messages are reusable resources since they can be forwarded to other computers.

False, they are consumed as soon as they have been received.

Page 13: 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)

True or false?

Datagrams preserve message boundaries.

Page 14: 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)

Answer

Datagrams preserve message boundaries.

True, messages are sent and received individually.

Page 15: 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)

Second question

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

Page 16: 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)

The code

shared int reserved[2] = {0, 0};void enter_region(int pid) { int other;

other = 1 - pid; // pid of other processreserved[pid] = 1; // reservewhile (reserved[other] && reserved[pid]); //

busy wait} // enter_region

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

} // leave_region

Page 17: 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)

The code analyzed

shared int reserved[2] = {0, 0};void enter_region(int pid) { int other;

other = 1 - pid; // pid of other processreserved[pid] = 1; // reservewhile (reserved[other] && reserved[pid]); //

busy wait} // enter_region

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

} // leave_region

Reserve first

then check

Where is thetiebreaker?

Page 18: 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)

Answer

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

Page 19: 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)

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);

Page 20: 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)

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);

Page 21: 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)

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)

Page 22: 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)

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  

Page 23: 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)

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

Page 24: 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)

Explanation

In a remote procedure call, the calling program passes to the remove function transfer the two values 3 and 3The function executes in sequence:

3*3 = 93*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

Page 25: 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)

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)

Page 26: 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)

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)

Page 27: 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)

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)

Page 28: 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)

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.

Page 29: 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)

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)

Page 30: 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)

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

Page 31: 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)

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.

Page 32: 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)

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

Page 33: 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)

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