+ All Categories
Home > Documents > CS3211 (1)

CS3211 (1)

Date post: 03-Jun-2018
Category:
Upload: adin
View: 221 times
Download: 0 times
Share this document with a friend

of 11

Transcript
  • 8/12/2019 CS3211 (1)

    1/11

    CS3211 - Parallel and Concurrent Programming - April 2009

    NATIONAL UNIVERSITY OF SINGAPORESCHOOL OF COMPUTING

    EXAMINATION FORSemester 2, 2008/2009

    CS3211 - Parallel and Concurrent ProgrammingApril 2009 Time Allowed: 2 Hours

    INSTRUCTIONS TO CANDIDATES1 The examination paper contains FOUR (4) questions and comprises ELEVEN (11) pages.2 The maximum attainable score is 40.3. This is an OPEN BOOK exam.4. Wlite all your answers in the space provided in this booklet.5. Please write your matriculation number below.

    MATRICULATION NUMBER _

    (this portion is for the examiner's use only)

    Question Marks RemarkQlQ2Q3Q4

    Total

    1

  • 8/12/2019 CS3211 (1)

    2/11

    CS3211 - Parallel nd Concurrent Programming - April 2009

    Question 1 [6 marks]A. Consider the following SR program:int x = 0 ;co < w it x != 0) x = x - 2 >

    < w it x != 0) x = x - 3 >< w it x == 0) x = x + 5 >oc

    Does this program tenninate or will it deadlock? What is the final value of x? (Even if theprogram deadlocks, there is such a value). Justify your answers. [2 marks]

    B. Consider the following program:co < w it x >= 3 x = x - 3 >

    < w it x >= 2 x = x - 2 >< w it x == 1 x = x + 5 >oc

    For what values of x does the program terminate, assuming scheduling is weakly fair? What arethe corresponding final values? Justify your answer. [2 marks]

    2

  • 8/12/2019 CS3211 (1)

    3/11

    CS32 Parallel and Concurrent Programming April 2009

    c Consider the following program:boolean flagl = true flag2 = trueco while ( flagl ) { flagl = true ; flag2 = true flag2 false

    # another process comes hereocDevise a second process in the co oc block such that the program is guaranteed to terminatein the presence of a strongly fair scheduler. [ marks]

    3

  • 8/12/2019 CS3211 (1)

    4/11

    CS32 Parallel and Concurrent Programming April 2009

    Question 2 [8 marks]The following code is an attempt at solving the critical section problem.int wantp 0, wantq = 0 ;process P {

    while (true) {i f ( wantq -1wantp - 1 ;else

    wantp 1 ;< await ( wantq != wantp ; ># crit ical sectionwantp = 0 ;# non-critical section

    process Q {while (true) {

    i f ( wantp -1 )wantq = 1 ;elsewantq = -1 ;

    < await ( wantp = -wantq ) ; ># crit ical sectionwantq = 0 ;# non-critical section

    4

  • 8/12/2019 CS3211 (1)

    5/11

    CS3211 - Parallel nd Concurrent Programming - April 2009

    A. Does the code work? f not, explain how both processes may get simultaneously in thecritical section. [ marks]

    B. Find a simple modification to the above code that would fix the bug. [2 marks]

    5

  • 8/12/2019 CS3211 (1)

    6/11

    CS32 Parallel and Concurrent Programming April 2009

    c Does the code have the eventual entry property Justify your answer [3 marks]

    6

  • 8/12/2019 CS3211 (1)

    7/11

    CS32 Parallel and Concurrent Programming April 2009

    Question 3 [14 marks]Assume one producer process and n consumer processes share a buffer. The producer depositsmessages into the buffer, consumers fetch them. Every message deposited by the producer has tobe fetched by all n consumers before the producer can deposit another message into the buffer.A. Develop a solution for this problem using semaphores for synchronization. [6 marks]

    7

  • 8/12/2019 CS3211 (1)

    8/11

    CS3211 Parallel and Concurrent Programming April 2 9

    B. Now assume the buffer has b slots. The producer can deposit messages only into emptyslots and every message has to be received by all n consumers before the slot can be reused.Furthermore each consumer is to receive messages in the order they were deposited. Howeverdifferent consumers can receive messages at different times. For example one consumer couldreceive up to b more messages than another i the second consumer is slow. Extend your answerto Q3A to solve this more general problem. [ marks]

    8

  • 8/12/2019 CS3211 (1)

    9/11

    CS3211 - Parallel and Concurrent Programming - April 2009

    Question 4 [12 marks]Write a Java class that allows the exchange of three values between three threads. That is,

    the first thread to arrive passes its value to the second thread, which passes its value to the thirdthread, which n tum, passes its value to the first thread. The class implements a method calledexchange with the following prototypevoid exchange Object [] x)The argument x is an array of on element, such that x [0] acts s a reference that allows valuesto be swapped between threads. The value exchanged by the threads is x [0] (and not x). Theinstance of your class must be reusable, in the sense that once the first three threads have exchanged values, the instance will allow the next three threads that call the exchange method toexchange values, and so on.A. Develop a solution using only synchronized methods/statements, and the wai t andnotify /notifyAll primitives. [4 marks]

    9

  • 8/12/2019 CS3211 (1)

    10/11

    CS3211 Parallel and Concurrent Programming April 2 9

    B. Assume now that the threads calling the method exchange may be of two types: type 1,and type 2. Threads of type 1 may initiate an exchange with any other kind of thread. However,threads of type 2 may only initiate exchanges with threads of the same type. That is, if at thebeginning of an exchange cycle, the first thread to arrive is of type 2, it will only be allowedto exchange values with threads of type 2, and the threads of type 1 that may arrive before theexchange is completed will have to wait till the exchange is completed.

    A thread will state its type via a second parameter to the method exchange. Devise a solutionto this new problem using ReentrantLocks and ondition variables. Use the technique ofpassing the condition (solutions not using this technique will incur a 2-mark penalty). [ marks]

    10

  • 8/12/2019 CS3211 (1)

    11/11

    CS32 Parallel and Concurrent Programming April 2009

    c Consider the following Java class, that creates a thread-safe set data structure.public class SynchronizedSet {

    private final Set set = new HashSet();public synchronized void add(Integer i) { set.add(i); }public synchronized void remove (Integer i) { set.remove(i);public void addTenThings() { for testing purposes

    Random r = new Random();for (int i = ; i < 10; i t t )

    add(r.nextInt());IIystem.out.println("DEBUG: added ten elements to t set);

    Various threads call the three methods of an instance of this class. Occasionally, some of themethods throw a NullPointerException. Explain how that may happen. [2 marks]

    EN OF P P R -

    11


Recommended