0% found this document useful (0 votes)
15 views8 pages

COS226 2020 - Semester Test 2

The document outlines the instructions and structure for the Semester Test 2 (ST2) in Concurrent Systems (COS 226) at the University of Pretoria, scheduled for October 12, 2020. It includes details on the number of questions, time allocation, integrity statement, and submission guidelines, as well as multiple-choice and long questions related to concurrency and consensus in computer science. The test emphasizes academic integrity and prohibits discussion of questions during the assessment.

Uploaded by

mvanheerdentuks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

COS226 2020 - Semester Test 2

The document outlines the instructions and structure for the Semester Test 2 (ST2) in Concurrent Systems (COS 226) at the University of Pretoria, scheduled for October 12, 2020. It includes details on the number of questions, time allocation, integrity statement, and submission guidelines, as well as multiple-choice and long questions related to concurrency and consensus in computer science. The test emphasizes academic integrity and prohibits discussion of questions during the assessment.

Uploaded by

mvanheerdentuks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Engineering, Built Environment and IT

Department of Computer Science


Concurrent Systems
COS 226
Semester Test 2 (ST2)

12 October 2020 (17:30 to 19:00)

Examiners
Internal: Ms Ntombikayise Banda and Mr Theo Naidu

Instructions
1. Read the question paper carefully and answer all the questions.
2. The assessment opportunity comprises of 7 questions on 8 pages.
3. 1.5 hours have been allocated for you to complete this paper. An additional 30 min has been allocated for you to
download the paper and upload your answer document.
4. This paper is take home and is subject to the University of Pretoria Integrity statement provided below.
5. You are allowed to consult any literature.
6. You are not allowed to discuss the questions with anyone.
7. If you have any queries when writing the paper, post them in good time on the Semester Test 2 Discord channel.
You are also allowed to direct message Ms Banda or Mr Naidu on Discord should you wish for privacy. NO emails
during the test will receive a response. A Collaborate LIVE session will also be available to answer queries.
8. Write your answers in a separate document (eg. using a word processor or handwritten and scanned) and submit
the document as a single PDF attachment. An upload slot will be open on the ClickUP module page under the
Semester Test 2 - Question paper and upload menu option for the duration of the test (17:30 to 19:00), and then
for an additional 30 minutes to give you enough time to download this paper and upload your PDF. The submission
deadline is therefore 19:30. No late submissions will be accepted.
ˆ Please make sure that your name and student number are clearly visible in the document you upload.
ˆ You need to provide photographic evidence of yourself in the form of a student card, identity document, passport
document, or driver’s licence. This only needs to be shown on the first page of your answer script.
9. Marks per question

Question: 1 2 3 4 5 6 7 Total
Marks: 12 4 11 8 4 5 16 60

1
Integrity statement:
The University of Pretoria commits itself to produce academic work of integrity. I affirm that I am aware of
and have read the Rules and Policies of the University, more specifically the Disciplinary Procedure and the Tests
and Examinations Rules, which prohibit any unethical, dishonest or improper conduct during tests, assignments,
examinations and/or any other forms of assessment. I am aware that no student or any other person may assist
or attempt to assist another student, or obtain help, or attempt to obtain help from another student or any other
person during tests, assessments, assignments, examinations and/or any other forms of assessment.

Multiple choice questions


1. For each of the questions that follow, choose the most appropriate option.
(Note: possible answers are denoted by letters A, B, C, etc.)
(a) Which correctness condition does a regular register follow? (1)
A. Sequential consistency
B. Quiescent consistency
C. Linearizability
D. Mutual exclusion
(b) Given a multi-reader multi-writer (MRMW) atomic register r with a capacity of 4 and the following array (1)
table state:

stamp value
2 670
a table = 0 0
1 510
0 0

What would be the result of a r.write(720) method call by a writer thread with id 2?
2 670
0 0
A.
1 720
0 0

2 720
0 720
B.
1 720
0 720

2 670
0 0
C.
3 720
0 0

2 670
0 720
D.
1 510
0 0

(c) Given a multi-reader multi-writer (MRMW) atomic register r with a capacity of 4 and the following array (1)
table state:
stamp value
3 670
a table = 2 110
1 510
3 320
What would be the result of an r.read() method call by a reader thread with id 1?
A. 670
B. 110
C. 510
D. 320

Page 2
(d) A regular M-valued MRSW register (with M = 4) represents its values using unary notation. Which of (1)
the following options shows the correct representation of the value 3?
A. 0 0 1 1
B. 0 0 0 1
C. 0 0 0 1 0
D. 0 0 1 0
(e) Which of the following is NOT an example of synchronization primitives? (1)
A. CLH lock
B. MRSW atomic register
C. Binary semaphore
D. Condition object
(f) Consider an atomic register r with an initial value of 0. What is the output of the following code snippet? (1)
r.compareAndSet(1, 2);
r.compareAndSet(0, 1);
System.out.println(r.get());
A. 0
B. 1
C. 2
D. Compiler error
(g) Consider an atomic register r with an initial value of 1. What is the output of the following code snippet? (1)
r.compareAndSet(1, 0);
r.compareAndSet(0, 2);
System.out.println(r.get());
A. 0
B. 1
C. 2
D. Compiler error
(h) Which of the following RMW (read-modify-write) register methods is an identity function? (1)
A. getAndSet(...)
B. getAndIncrement()
C. set(...)
D. get()
(i) Suppose that thread C executes line 4 in the code snippet below and suspends itself. After some time, an (1)
earlier thread A modifies the property value to true and signals all waiting threads. What is the next
line that thread C will execute when it is awakened?
1 mutex . lock () ;
2 try {
3 while (! property )
4 condition . await () ;
5 count ++; // some i m p l e m e n t a t i o n
6 } finally {
7 mutex . unlock () ;
8 }

A. line 1
B. line 3
C. line 5
D. line 7
(j) The processors’ respective caches in the diagram below are empty. Suppose that Processor 2 executes an (1)
operation that requires it to read data from some memory location. Which of the following options will
supply Processor 2 with the required data?

Page 3
A. Processor 1’s cache
B. Processor 2’s cache
C. Input/Output processor
D. Main memory
(k) Suppose that the main memory and Processor 1’s cache memory have the latest version of data x. (1)

If Processor 2 executes an instruction to read data x, but cannot find it in its cache, which of the following
options will supply Processor 2 with data x?
A. Processor 1’s cache
B. Processor 2’s cache
C. Input/Output processor
D. Main memory
(l) The figure below shows the execution tree of a binary consensus protocol for two threads. Thread A (in (1)
blue) proposes the value 1 and Thread B (in purple) proposes the value 0.

Which of the following options contains a list of all protocol states that are applicable to node s.
A. initial state
B. initial state, bivalent state
C. initial state, univalent state
D. initial state, bivalent state, critical state

Long questions
Chapter 4
2. Consider the implementation of a regular MRSW register class below.

Page 4
1 public c l a s s R eg MR SW R eg is t er implements Register < Byte > {
2 private s t a t i c i n t RANGE = Byte . MAX_VALUE - Byte . MIN_VALUE + 1;
3 boolean [] r_bit = new boolean [ RANGE ];
4
5 public R eg MR SW R eg is te r ( i n t capacity ) {
6 f o r ( i n t i =1; i < r_bit . length ; i ++)
7 r_bit [ i ] = f a l s e ;
8 r_bit [0] = true ;
9 }
10
11 public void write ( Byte x ) {
12 r_bit [ x ] = true ;
13 f o r ( i n t i =x -1; i >= 0; i - -)
14 r_bit [ i ] = f a l s e ;
15 }
16
17 public Byte read () {
18 f o r ( i n t i =0; i < RANGE ; i ++)
19 i f ( r_bit [ i ])
20 return i ;
21 return -1; // i m p o s s i b l e
22 }
23 }

(a) Suppose the lines of the write(...) function are reordered as follows: (2)
public void write ( Byte x ) {
f o r ( i n t i =x -1; i >= 0; i - -)
r_bit [ i ] = f a l s e ;
r_bit [ x ] = true ;
}

Provide a brief analysis of what could go wrong during a read() call that overlaps with a write() call?
(b) If the write(...) function were to be changed to the following: (2)
public void write ( Byte x ) {
r_bit [ x ] = true ;
f o r ( i n t i =0; i <= x -1; i ++)
r_bit [ i ] = f a l s e ;
}

Provide a brief analysis of what could go wrong during a read() call that overlaps with a write() call?

Chapter 5
3. Concurrency and Consensus
(a) What are the advantages of wait-free concurrent objects (e.g. WaitFreeQueue) over lock-based concurrency (2)
objects (e.g. LockedQueue)?
(b) Study the code below of an adapted queue consensus protocol. (3)
1 public c l a s s AdaptedQueueConsensus <T > extends ConsensusProtocol <T > {
2 Queue queue ;
3 private s t a t i c f i n a l i n t WIN = 0;
4 private s t a t i c f i n a l i n t LOSE = 1;
5 private v o l a t i l e i n t winner = -1;
6
7 public A d a p t e d Q u e u e C o n s e n s u s () {
8 queue = new Queue () ;
9 queue . enq ( WIN ) ;
10 queue . enq ( LOSE ) ;
11 queue . enq ( LOSE ) ;
12 }
13
14 public propose ( T value ) {
15 super. propose ( value ) ;
16 }
17
18 public T decide ( T value ) {
19 i n t status = queue . deq () ;
20 i n t i = ThreadID . get () ;
21
22 i f ( status == WIN ) {
23 winner = i ;
24 return proposed [ i ];
25 }
26 else

Page 5
27 return proposed [ winner ];
28 }
29 }

Discuss whether this protocol successfully solves a three-thread consensus.


(c) Consider the following hypothetical scenario. In its drive to building a “smart city”, the Gautrain man- (6)
agement board upgrades its train system into a distributed automatic driverless system. The automatic
driving system uses defined time rules to control departures and movements between stations. Each train
has its own clock, which may suffer from significant clock drifts after a period of time (i.e. the clock gradu-
ally desynchronizes from a reference clock). To reduce chances of collisions, the trains need to synchronize
their clocks periodically though a consensus protocol.
Implement a consensus protocol called ClockConcensus (with a capacity of n concurrent threads) with
the following rules:
ˆ Each train (represented by a thread) proposes its current clock’s time through the propose(...)
method call. Assume that time is represented as a long integer value.
ˆ The agreed-upon time is determined through:
◦ The “majority-wins” decision, with the most frequently occurring proposed time amongst con-
tending threads being the deciding / winning value.
◦ If all trains propose distinct values, the first train (thread) to call decide(...) wins.
ˆ Assume that all threads would have proposed their values before any thread calls decide(...).
ˆ To aid with your implementation, assume that the function
public static Long computeMode(Long[] arr);
has been defined elsewhere and can be called directly from your class implementation. The computeMode
function computes and returns the most common number in the given input array. If the input array
contains no duplicate data points, the function returns −1.
ˆ There is no need to display output or logging statements.

Chapter 7
4. Spin Locks
(a) Suppose that a Counter object with an initial value of 0 uses a Peterson lock to enforce mutual exclusion on (3)
the Counter’s getAndIncrement() method. After 5000 concurrent executions of the getAndIncrement()
method, it is possible for the Counter object’s final value to be below 5000. Explain the cause of this
discrepancy and what measures can be implemented to prevent this?
(b) Discuss the concept of an “invalidation storm” with respect to the TAS and TTAS locks (i.e. what it is and (5)
how it occurs)? In your discussion, explain the effect that the invalidation storm has on the performance
of the two locks, and mention the mechanisms that the TTAS lock has in place to reduce the effect of
invalidation storms.

Chapter 8
5. MongoDB uses a readers-writer lock that allows concurrent readers shared access to a database or collection, (4)
and an exclusive locking mode for write operations. The code below shows a hypothetical implementation of
MongoDB’s insert(...) and count() functions.
public c l a s s Collection {
ArrayList < Document > documents ;
ReadWriteLock rwLock ;

public Collection () {
documents = new ArrayList < Document >() ;
rwLock = new S i m p l e R e a d W r i t e L o c k () ;
}

// Inserts a new d o c u m e n t into the d a t a b a s e c o l l e c t i o n


public void insert ( Document newDoc ) {
documents . add ( newDoc ) ;
}

// Returns the number of d o c u m e n t s in this c o l l e c t i o n


public i n t count () {
return documents . size () ;
}
}

Page 6
Make use of the Readers-Writers lock object rwLock to enforce appropriate thread access to the insert(...)
and count() functions. (Note: you only need to write code for the two functions)
6. The code segment below is of the inner read lock of the SimpleReadWriteLock class.
1 private c l a s s ReadLock implements Lock {
2 public void lock () {
3 lock . lock () ;
4 try {
5 while ( writer ) {
6 condition . await () ;
7 }
8 readers ++;
9 } finally {
10 lock . unlock () ;
11 }
12 }
13
14 public void unlock () {
15 lock . lock () ;
16 try {
17 readers - -;
18 i f ( readers == 0) {
19 condition . signalAll () ;
20 }
21 } finally {
22 lock . unlock () ;
23 }
24 }
25 }

(a) If the ReadLock class allows multiple reader threads to access the critical section, what is the purpose of (1)
the lock() method call in line 3?
(b) Use semaphore concepts to restrict the number of readers that can access the critical section at the same (4)
time to some capacity value. You only need to adapt the lock() function of the ReadLock class. If
you define extra class variables, state the scope of the variables (in other words, where the variables are
declared).
7. Consider the code below for the inner ReadLock and WriteLock classes of the Fair Readers-Writers Lock
and answer the questions that follow.
1 private c l a s s ReadLock implements Lock {
2 public void lock () {
3 lock . lock () ;
4 try {
5 while ( writer ) {
6 condition . await () ;
7 }
8 readAcquires ++;
9 } finally {
10 lock . unlock () ;
11 }
12 }
13
14 public void unlock () {
15 lock . lock () ;
16 try {
17 readReleases ++;
18 i f ( readAcquires == readReleases ) {
19 condition . signalAll () ;
20 }
21 } finally {
22 lock . unlock () ;
23 }
24 }
25 }
26
27 private c l a s s WriteLock implements Lock {
28 public void lock () {
29 lock . lock () ;
30 try {
31 while ( writer ) {
32 condition . await () ;
33 }
34 writer = true ;
35 while ( readAcquires != readReleases ) {
36 condition . await () ;

Page 7
37 }
38 } finally {
39 lock . unlock () ;
40 }
41 }
42
43 public void unlock () {
44 writer = f a l s e ;
45 condition . signalAll () ;
46 }
47 }

(a) What “fairness” does the Fair Readers-Writers Lock provide? (2)
(b) Explain the role of the readAcquires and readReleases variables in providing fairness. That is, how are (2)
the variables used to help achieve fairness?
(c) Why is a lock not used in the WriteLock’s unlock() function? (2)
(d) In order to reduce unnecessary contention from awakening both reader and writer threads when there is (10)
a waiting writer thread, how would you go about adapting the ReadLock and WriteLock classes to allow
reader and writer threads to only awaken the necessary threads?
If you define extra class variables, state the scope of the variables (in other words, where the variables are
declared). Give meaningful names to any variables that you declare.

Page 8

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy