COS226 2020 - Semester Test 1
COS226 2020 - Semester Test 1
Examiners
Internal: Ms Ntombikayise Banda
Instructions
1. Read the question paper carefully and answer all the questions.
2. The assessment opportunity comprises of 8 questions on 6 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 ClickUP Discussion Board under the
appropriate thread, by replying to the thread, in the Semester Test 1 Forum. NO emails during the test will receive
a response, only discussion board posts will be responded to. 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 1 - 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 8 Total
Marks: 11 6 6 6 6 4 11 10 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.
A. ii and iii
B. i and ii
C. i and iv
D. iii and iv
(d) Which of the following statements is true? (1)
A. Starvation-freedom implies deadlock-freedom
B. Deadlock-freedom implies starvation-freedom
C. Mutual exclusion implies starvation-freedom
D. Mutual exclusion implies deadlock-freedom
j
(e) The notation IA = (aj0 , aj1 ) denotes a time interval in which thread A executes events a0 to a1 during the (1)
j-th iteration of the code segment. Assuming the following intervals are comprised of events in the critical
j
section: IA = (aj0 , aj1 ) and IB k
= (bk0 , bk1 ), which relations best describe mutual exclusion? Select all that
apply.
k j
i. IB → IA
j k
ii. IA → IB
Page 2
iii. aj0 → aj1
iv. (aj0 , aj1 ) → (bk0 , bk1 )
A. i and ii
B. ii and iii
C. ii and iv
D. i, ii and iv
(f) Given the following precedence graph, which thread has the earlier timestamp? In other words, which (1)
thread would potentially be served first?
A. Thread A at node 0
B. Thread B at node 2
(g) Which of the following statements is true? (1)
A. quiescent consistency is compositional and preserves program order
B. quiescent consistency is not compositional and preserves program order
C. sequential consistency is compositional and preserves program order
D. sequential consistency is not compositional and preserves program order
(h) Which of the following statements is true with regards to a non-blocking algorithm? (1)
A. A non-blocking algorithm is characterized by the starvation-free and wait-free progress conditions
B. A non-blocking algorithm is characterized by the deadlock-free and lock-free progress conditions
C. A non-blocking algorithm is characterized by the lock-free and wait-free progress conditions
D. A non-blocking algorithm is characterized by the starvation-free and deadlock-free progress
conditions
(i) Which of the following statements is true with regards to a blocking algorithm? (1)
A. A blocking algorithm is characterized by the starvation-free and wait-free progress conditions
B. A blocking algorithm is characterized by the deadlock-free and lock-free progress conditions
C. A blocking algorithm is characterized by the lock-free and wait-free progress conditions
D. A blocking algorithm is characterized by the starvation-free and deadlock-free progress conditions
(j) Indicate if the following statement is TRUE or FALSE. (1)
Volatile fields are linearizable.
A. TRUE
B. FALSE
(k) Indicate if the following statement is TRUE or FALSE. (1)
In Java, shared memory reads and writes are guaranteed to be linearizable.
A. TRUE
B. FALSE
Long questions
Chapter 1
2. You have been working on a program that you plan to deploy on a system with 20 cores. During your tests (6)
you notice that 10% of the runtime of the program is not parallelizable, and that the program takes 5 days to
return a result. While attempting to increase the program performance, one of the developers erroneously adds
a piece of code that doubles the sequential execution of the program. To see the effect of this error, compute
the number of extra days that it will take for the program to produce a result?
Note: Round off your program performance values to 3 decimal places, and round off the number of extra days
to the nearest integer (i.e. a whole number).
Page 3
Chapter 2
3. Suppose that a fellow developer has given you an implementation of a new lock protocol that he devised called
Secure Lock which is meant to provide n-thread mutual exclusion (where n > 1). The developer would like
your views on its validity. Study the code below and specify whether the lock satisfies the mutual exclusion,
starvation freedom, and deadlock freedom properties. Motivate your answers by means of thread execution
examples (do this for each property).
1 c l a s s Secure implements Lock {
2 private i n t turn ;
3 private boolean busy = f a l s e ;
4 public void lock () {
5 i n t me = ThreadID . get () ;
6 do {
7 do {
8 turn = me ;
9 } while ( busy ) ;
10 busy = true ;
11 } while ( turn != me ) ;
12 }
13 public void unlock () {
14 busy = f a l s e ;
15 }
16 }
(a) What could go wrong if victim = i was performed before flag[i] = true? (2)
(b) Would the Peterson lock implementation above still provide mutual exclusion if the spinning condition (2)
(line 10) was changed to the following: while (flag[j] && (victim == j))?
(c) Suppose that the Peterson lock is used to enforce mutual exclusion in the Counter’s getAndIncrement() (2)
function. Explain why the lock() method call (in line 4) should occur outside the try-finally block
enveloping the critical section.
1 c l a s s Counter {
2 // i n i t i a l i z a t i o n s left out
3 public i n t g et An dI n cr em en t () {
4 myLock . lock () ; // myLock is of data type P e t e r s o n
5 try {
6 return value ++;
7 } finally {
8 myLock . unlock () ;
9 }
10 }
11 }
Page 4
6 l a b e l [ i ] = max ( l a b e l [0] ,... , l a b e l [n -1]) + 1;
7 while ((∃k != i ) ( flag [ k ] && ( l a b e l [ k ] , k ) << ( l a b e l [ i ] , i ) ) ) ) {};
8 }
9 public void unlock () {
10 flag [ ThreadID . get () ] = f a l s e ;
11 }
Three threads (A, B, and C) want to acquire the Bakery lock so they can enter a critical section of some
function. Suppose that the three threads concurrently enter the lock() function after a number of iterations
and are all in the process of executing line 7. The table below shows the state of the threads at line 7.
(a) What is the purpose and meaning (or interpretation) of the statement (label[k],k) << (label[i],i) (3)
in line 7?
(b) Why is it possible for two concurrent threads to be assigned the same label, as in the case of threads B (2)
and C?
(c) Given the status of the three threads in the table above, which thread will enter the critical section first? (1)
6. The figure below shows the precedence graph T 3 of a bounded timestamping system. Answer the following
questions with respect to this figure.
(a) What is the maximum number of threads that this timestamping system can support? (1)
3
(b) How many digits are used to “address” a node in the T graph? (1)
(c) Order the following timestamp values of the nodes circled in red from lowest to highest (i.e. earliest to (2)
latest).
Chapter 3
7. Consider the execution histories of FIFO data structures and shared sequential registers as visualized through
the diagrams below. Answer the questions that follow with respect to each execution history diagram.
(a) What value(s) will object r read if the following execution is assumed to be quiescently consistent? (1)
r . write ( -9) r . read (??)
A - -|=============| - - - - - - - - - - - - - - - - - - - - - - - - - - -|===========| - - - - - - - >
r . write (2)
B - - - - - - - - - - - - - - - - - - - - - -|============| - - - - - - - - - - - - - - - - - - - - - - - - - - - - >
(b) If the execution history was changed to the following, what value(s) will object r now read assuming a (1)
quiescently consistent execution?
r . write ( -9)
A - - -|==============| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - >
(c) Is the following execution history sequentially consistent? Give reasons for your answer. (3)
q . enq ( x ) q . enq ( y )
A - - - - - - - - -|===========| - - - - - - - - - - - - - - - - - - - -|============| - - - - - - - - >
q . deq ( y ) q . deq ( x )
B - - - - - - - - - - - - - - -|===========| - - - - - - - - -|==============| - - - - - - - - - - - >
Page 5
(d) The diagram below shows the execution history of shared register objects s and t. Use the execution (4)
order of the events to discuss composability with respect to sequential consistency.
s . write (5) t . write (7) s . read (5)
A - - - - - -|===========| - - - - -|============| - - - -|============| - - - - - - - - >
(e) Explain the role of linearization points in determining if a concurrent object is linearizable. (2)
Chapter 4
8. The following questions pertain to shared registers.
(a) Given a SRSW safe register r with the capacity to hold an integer with a maximum value of 15 (represented (2)
by 4 unsigned bits), what is the result of the following read() call?
r . write (6)
A - - - - - - - - -|====================| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - >
r . read (??)
B - - - - - - - - - - - - - - - - - - -|=====================| - - - - - - - - - - - - - - - - - - - - - - - >
(b) Under what circumstance or condition do the rules of the safe, regular, and atomic registers differ? Briefly (4)
state the rule that each register must comply with under the identified circumstance or condition.
(c) Consider the following unsigned 8-bit SRSW register execution for threads A and B. The notation readi ()
refers to the i-th read of the register.
write (10) write (4)
A - - - -|============| - - - - - - - - - - - - - -|=================| - - - - - - - - - - - - - - >
Write down the value(s) returned by the read1 () and read2 () calls if the register is:
i. A regular register (2)
ii. An atomic register (2)
Page 6