Вы находитесь на странице: 1из 11

CS3211- ParallelandConcurrentProgramming- April2009

NATIONALUNIVERSITYOFSINGAPORE
SCHOOLOFCOMPUTING
EXAMINATIONFOR
Semester2,2008/2009
CS3211- ParallelandConcurrentProgramming
April2009 TimeAllowed: 2Hours
INSTRUCTIONSTOCANDIDATES
1. TheexaminationpapercontainsFOUR(4)questionsandcomprisesELEVEN(11)pages.
2. Themaximumattainablescoreis40.
3. ThisisanOPENBOOKexam.
4. Wliteallyouranswersinthespaceprovidedinthisbooklet.
5. Pleasewriteyourmatriculationnumberbelow.
MATRICULATIONNUMBER: _
(thisportionisfortheexaminer'suseonly)
Question Marks Remark
Ql
Q2
Q3
Q4
Total
1
CS3211- ParallelandConcurrentProgramming- April2009
Question1 [6marks]
A. ConsiderthefollowingSRprogram:
int x = 0 ;
co < await (x != 0) x = x - 2 >
II < await (x != 0) x = x - 3 >
II < await (x == 0) x = x + 5 >
oc
Does this programtenninate orwill itdeadlock? Whatis the final value ofx? (Even ifthe
programdeadlocks,thereissuchavalue). Justifyyouranswers. [2marks]
B. Considerthefollowingprogram:
co < await x >= 3 x = x - 3 >
II < await x >= 2 x = x - 2 >
II < await x == 1 x = x + 5 >
oc
Forwhatvaluesofxdoestheprogramterminate,assumingschedulingisweaklyfair? Whatare
thecorrespondingfinal values?Justifyyouranswer. [2marks]
2
CS3211 - Parallel and Concurrent Programming - April 2009
c. Considerthefollowingprogram:
boolean flagl =true, flag2 =true
co while ( flagl ) { flagl = true ; flag2 =true flag2 false
II # another process comes here
oc
Deviseasecondprocessin the co...oc blocksuchthattheprogramisguaranteedtoterminate
inthepresenceofastronglyfairscheduler. [2marks]
3
CS3211 - Parallel and Concurrent Programming - April 2009
Question 2 [8 marks]
Thefollowingcodeisanattemptatsolvingthecriticalsectionproblem.
int wantp 0, wantq =0 ;
process P {
while(true) {
if ( wantq == -1
wantp -1;
else
wantp 1;
< await ( wantq != wantp ; >
# critical section
wantp = 0 ;
# non-critical section
process Q {
while(true) {
if ( wantp == -1 )
wantq = 1 ;
else
wantq = -1 ;
< await ( wantp != -wantq ) ; >
# critical section
wantq = 0 ;
# non-critical section
4
CS3211- ParallelandConcurrentProgramming- April2009
A. Does the code work? If not, explain how both processes may getsimultaneously in the
criticalsection. [3 marks]
B. Findasimplemodificationtotheabovecodethatwouldfixthebug. [2marks]
5
CS3211 - Parallel and Concurrent Programming - April 2009
c. Does the code have the eventual entry property? Justify your answer. [3 marks]
6
CS3211 - Parallel and Concurrent Programming - April 2009
Question 3 [14 marks]
Assumeoneproducerprocess and nconsumerprocessesshareabuffer. Theproducerdeposits
messagesintothebuffer,consumersfetchthem. Everymessagedepositedbytheproducerhasto
befetchedbyallnconsumersbeforetheproducercandepositanothermessageintothebuffer.
A. Developasolutionforthisproblemusingsemaphoresforsynchronization. [6marks]
7
CS3211 - Parallel and Concurrent Programming - April 2009
B. Now assume the buffer has b slots. The producer can deposit messages only into empty
slots 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. However,
different consumers can receive messages at different times. For example, one consumer could
receive up to bmore messages than another if the second consumer is slow. Extend your answer
to Q3A to solve this more general problem. [8 marks]
8
CS3211- ParallelandConcurrentProgramming- April2009
Question4 [12marks]
WriteaJavaclassthatallows the exchangeofthree values betweenthree threads. Thatis,
thefirstthreadtoarrivepassesitsvaluetothesecondthread, whichpassesitsvaluetothethird
thread,whichintum,passesitsvaluetothefirstthread. Theclassimplementsamethodcalled
exchangewiththefollowingprototype
void exchange(Object[] x)
Theargumentxisanarrayofone element,suchthatx[0] actsas areferencethatallowsvalues
tobeswappedbetweenthreads. Thevalueexchangedbythe threadsis x[0] (andnotx). The
instanceofyourclassmustbereusable, in the sensethatoncethefirst three threads have ex-
changedvalues, theinstancewillallowthenextthreethreadsthatcallthe exchangemethodto
exchangevalues,andsoon.
A. Developasolutionusingonlysynchronizedmethods/statements,andthewai t and
notify/notifyAllprimitives. [4marks]
9
CS3211 - Parallel and Concurrent Programming - April 2009
B. Assumenow that the threads callingthe method exchange maybeoftwo types: type 1,
andtype2. Threadsoftype 1mayinitiateanexchangewithanyotherkindofthread. However,
threads oftype 2may onlyinitiate exchanges with threads ofthe same type. Thatis, ifat the
beginning ofan exchange cycle, thefirst threadto arrive is oftype 2, it will only be allowed
toexchangevalues with threadsoftype 2, andthethreadsoftype 1thatmayarrivebeforethe
exchangeiscompletedwillhavetowaittilltheexchangeiscompleted.
Athreadwillstateitstypeviaasecondparametertothemethodexchange.Deviseasolution
to this new problem using ReentrantLocks and Conditionvariables. Use the technique of
passingthecondition(solutionsnotusingthistechniquewillincura2-markpenalty). [6 marks]
10
CS3211 - Parallel and Concurrent Programming - April 2009
c. ConsiderthefollowingJavaclass,thatcreatesathread-safesetdatastructure.
public class SynchronizedSet {
private final Set<Integer> set =new HashSet<Integer>();
public synchronized void add(Integer i) { set.add(i); }
public synchronized void remove(Integer i) { set.remove(i);
public void addTenThings() { II for testing purposes
Random r =new Random();
for (int i = 0; i < 10; itt)
add(r.nextInt());
II System.out.println("DEBUG: added ten elements to t set);
Various threads call the three methods ofan instance ofthis class. Occasionally, some ofthe
methodsthrowaNullPointerException.Explainhowthatmayhappen. [2marks]
- END OF PAPER-
11