13.07.2015 Views

Concurrent Programming Written Exam

Concurrent Programming Written Exam

Concurrent Programming Written Exam

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Concurrent</strong> <strong>Programming</strong><strong>Written</strong> <strong>Exam</strong>Verónica Gaspes10-04-2007 09:00 - 13:00• Allowed tools: None.• Responsible: Verónica Gaspes, telephone +46 +35 167380.• Read carefully! Some exercises might include explanations, hints and/orsome code. What you have to do in each exercise is marked with the pointsthat you can get for solving it (as (X pts.)).• Write clearly! Text that is difficult to read and code that is not wellstructured will not be read by the teacher marking your exam.• Motivate your answers!• You may answer in english or swedish.• You can get up to 30 points. To pass with a 3 you need 12 points, for a 4you need 18 points, for a 5 you need 27 points.Good Luck!1


1. The package java.util.concurrent offers two mechanisms that can beused for mutual exclusion synchronization:• The class java.util.concurrent.Semaphore with constructorSemaphore(int n) and methods acquire() and release().• The interface java.util.concurrent.locks.Lock with methods lock()and unlock(). One class implementing this interface isjava.util.concurrent.locks.ReentrantLock.(a) (3 pts.) Explain how to use semaphores to protect critical sectionsof code. How many semaphores have to be declared? Where do theyhave to be known? To what values do they have to be initialized? Howis initialization done in Java? What conventions have to be followedby the threads that have to get synchronized?(b) (2 pts.) Explain how to use locks to solve the same problem.2. In many service organizations (post office, bank, train ticket office, etc.)customers are served at several desks. On arrival a customer takes a numberand waits for that number to be called at some desk. In large halls theremight be several number printing devices. When a desk becomes free, adisplay announces what customer (number) can be served in that desk.(5 pts.) In this exercise you will program a queue manager that is usedfor sending numbers to the printers and for sending cashier numbers andcustomer numbers to a display.The system is organized in such a way that when a customer pushes abutton on a printing device, this is communicated to your queue handler.For this the queue handler has to offer a method newCustomer(). Also,when a desk becomes free the employee will press a button telling yourqueue handler to announce that the next customer can be taken care of atthat desk. For this the queue handler has to offer a method freeDesk(k)where k is the desk number.Your program does not have to be concerned with the mechanism used tocall these methods, but it must be programmed in such a way that severalprocesses can use it concurrently.A printer device (what customers use when they arrive to get a number)offers a method print(int nextCustomer) that can be called by yourqueue manager. The display device offers a method show(int desk, intcustomer) that can be called by your queue manager. You do not need toprogram these!You can write your program as a class in Java or as a resource in mpd.2


3. The following MPD program should be familiar to you from the first laboration.It implements an interface to a hardware display. Many processesmight use a display adding rows by means of addRow and removing rowsby menas of deleteRow.resource JDisplay2import HWDisplaytype row = string[COLS]op clear()op addRow(row str)op deleteRow(int i)body JDisplay2()cap HWDisplay d = create HWDisplay()row text[0:99]int usedRows = 0const string[0] blank = ""procedure updateRow(int r, row str) {text[r] = strchar cs[length(str)] = chars(str)if (r < ROWS) {for [i=1 to length(str)]{d.write(r,i,cs[i])}for [i=length(str)+1 to COLS]{d.write(r,i,’ ’)}}}procedure flashRow(int r, int millisecs) {row txt = text[r]for [i= 1 to millisecs/200] {updateRow(r,blank)nap(70)updateRow(r,txt)nap(130)}}proc clear(){for [i=0 to ROWS-1]{updateRow(i,blank)}usedRows = 0}3


endproc addRow(str){updateRow(usedRows,str)flashRow(usedRows,1000)usedRows++}proc deleteRow(i){if (i < usedRows) {for [j = i+1 to usedRows-1]{updateRow(j-1,text[j])}usedRows--updateRow(usedRows,blank)if(usedRows >= ROWS){flashRow(ROWS,1000)}}}(a) (3 pts.) What problems can occur when several processes concurrentlycall addRow and deleteRow? If possible give an example.(b) (2 pts.) Modify the implementation of the display by adding semaphoresthat make the display thread safe in order to avoid the problemsyou identified above.4. (5 pts.) The programming language MPD provides rendezvous as a formof communication and synchronization. Explain what constructs of the languagehave to be used, what processes are involved and how communicationand synchronization takes place.5. The following specification should be familiar to you from the second laboration.It is an interface to sensors that can communicate.resource Sensorop deploy([*]cap Sensor nbgs)op chan(int id, double value)body Sensor(int identity, int interval, int netSize) separateThe operation deploy is called to provide the sensor with its neighboursin the network (the sensors it can communicate with). After that is done,the sensors work on their tasks and also communicate receiving messagesin their channel chan.Assume that the deploy method is already implemented and that sensorimplementations have a variable4


[netSize]cap Sensor neighboursinitialized by deploy in such a way that neighbours[i] is the sensor withidentity i (if i is a neighbour! Otherwise neighbours[i] is null)We now want sensors to repeatedly compute the maximum value amongitself and its neighbours. For this we add to the interface a channel forreceiving requests and an array of channels to receive values from eachneighbour:resource Sensorint MaxSize = 100op deploy([*]cap Sensor nbgs)op chan (int id, double value)op maxChan[MaxSize](double value)op request(int id)body Sensor(int identity, int interval, int netSize) separate(5 pts.) Your task is to complete the implementation of the sensors sothat, while meassuring and communicating they also repeatedly calculatethe maximum between their own value (recorded by the thread doing themeassurements in a variable value ) and those of the neighbours.6. In a control system a producer thread produces a stream of floating-pointnumbers. A consumer thread uses these values, but it uses running averagesof three consecutive values. Thus, if the six first values produced are1,3,5,6,6,7, the consumer will get four values, namely 3 (=(1+3+5)/3), 4.67(=(3+5+6)/3), 5.67 (=(5+6+6)/3) and 6.33 (=(6+6+7)/3).To handle differences in speed between the two threads a reactive object isused which acts both as a buffer for values and for computing the averages.This object has two operations, addValue, which has a float as a parameterand returns no result, and getAverage, which has no parameter andreturns a float. Values that has been produced but not yet consumed arestored in an array of size 100. Both operations may block; addValue blocksif the array is full and getAverage blocks if the array does not containenough values to compute a new average.(5 pts.) Implement the reactive object using the communication and synchronizationprimitives in MPD.5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!