COS226 2022 - Semester Test 1
COS226 2022 - Semester Test 1
19 August 2022
Instructions
1. Read the question paper carefully and answer all the questions below.
2. Answer all question is the answer book provided.
3. The assessment opportunity comprises of 16 questions on 4 pages.
4. You have 1 hour to complete the paper.
5. You are not allowed to copy from any source. Use your own insight and opinions when answering the questions.
6. This paper is subject to the University of Pretoria Integrity statement provided below.
• You are not allowed to discuss the questions with anyone.
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.
Question: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Total
Marks: 2 2 2 1 2 2 2 4 11 3 4 2 4 3 4 2 50
1
1. Which one of the following is not a genuine conflict in a concurrent environment ? (2)
(a) reader-reader conflict
(b) reader-writer conflict
(c) write-write conflict
(d) None of the above.
9. Consider the following execution history for an 8-bit, unsigned register depicted below
(a) If you assume that this is a safe register, what will read2() return? (2)
Sol: Any value between 0 and 255 [2] - no marks for ”any value in range” without saying that
the range is 8 bits
(b) If you assume that this is a safe register, what will read1() return? (2)
Sol: 6 [2]
(c) If you assume that this is a regular register, what will read4() return? (2)
Sol: 6 or 3 [2] no part marks both have to be stated
Page 2
(d) If you assume that this is an atomic register, what will read2() return? (2)
Sol: 6 or 3 or 10 [2] no part marks both have to be stated
(e) If you assume that this is an atomic register, what will read3() return? (3)
Sol: 1 mark for saying ”3 or 10” - 2 marks for saying ”if read2 returns 12, then only 12”
(a) In the implementation of an atomic SRSW register, how can we make sure that read2() does not read an (3)
earlier value than was read by read1()? Justify your answer.
Timestamps [1], Compare with last read value [1] Will use last read value [1]
11. Assuming that a system consists of 10 processors and that 30% of a program can only be executed sequentially. (4)
Using this system, it takes 15 days to execute the program and produce a result. If we can reduce the amount of
work done sequentially to 10% how many days would it take the system to run the program?
Sol: 30% results in 2.7 [1] 10% results 5.26 [1] Answer = (2.27/5.26)*15 = 7.7days [2]
12. What is the difference between multitasking and multiprocessing? (2)
Multitasking simulates the concurrent execution of processes on a single processor [1]. Multi-
processing is the concurrent execution of processes by multiple processors [1].
13. Consider the following lock() method of the Bakery algorithm and answer the question that follows: (4)
What impact has the fourth line of the code have on mutual exclusion.
Mutual exclusion refers to dealing with controlling concurrent access thus when two threads execute
line 4 at the same time [1] they can both end up with the same label [1] and then both continue to
the critical section [1] and we lose mutual exclusion [1]
14. What may be considered as a weakness of the Bakery algorithm and how can it be resolved. (3)
When the maximum value in a variable is reached it may overflow into the smallest value stored
in that variable [1] this makes a late arriving thread acquire a smaller number(ticket) than those
who came earlier. With labels, this would mean that we do not have first-come-first-served fairness
anymore [1]. Timestamps [1] can be used to resolve the overflow problem.
15. Given that the following pseudo-code is used to control access to a critical section. (4)
.
.
\\ code for CS entry
Page 3
wait until flag == 0;
flag = 0;
.
.
\\ code for exiting the CS
flag = flag + 1;
.
.
.
Assuming flag is initialized to 0 would this approach guarantee mutual exclusion in a concurrent environment.
Justify your answer.
Two threads may both find flag = 0 , and both proceed to the second line, and then both set flag
= 0, and then both enter critical section[1]s. When they both exit, they increment flag by 1 , and
the final value of flag is now 1. Any threads wanting to enter the critical section subsequently will
wait at the beginning indefinitely[1], since flag is now 1. One thread [1] may also enter and set the
conditions such that any threads that follow will be unable to enter the CS the approach will only
work once[1]
16. How does an atomic register and a regular register differ with respect to “write order”. (2)
Write order is important to atomic registers as they have multiple writers which then influences
the rule that an earlier read cannot return a value later than that returned by a later read meaning
write order was preserved [1]. Regular registers do not consider write order as they have one
writer[1].
Page 4