, COS3721 Assignment 2 (COMPLETE ANSWERS)
Semester 1 2025 - DUE 23 June 2025; 100%
TRUSTED Complete, trusted solutions and
explanations.
Chapter 6 – Question 01
The pseudocode below illustrates the basic push() and pop() operations of an
array-based stack.
a. What data have a race condition?
The race condition in this pseudocode arises primarily due to concurrent access
to shared data without proper synchronization. The shared data involved in the
stack operations are:
top – the index indicating the current top of the stack.
stack[] – the array holding the stack elements.
Race condition details:
top is both read and modified in both push() and pop(). If two threads
call push() or pop() simultaneously, they might:
o Overwrite each other's updates to top.
o Read an outdated value of top, leading to incorrect insertion or
deletion.
stack[top] can also be written or read at the same index by multiple
threads, leading to:
o Inconsistent or corrupted data being pushed or popped.
b. How could the race condition be fixed?
Race conditions can be fixed using mutual exclusion (mutex) mechanisms, such
as locks or semaphores, which the pseudocode tries to represent using
acquire() and release().
However, the issue here is incomplete or incorrectly scoped locking, especially
in is_empty().
Semester 1 2025 - DUE 23 June 2025; 100%
TRUSTED Complete, trusted solutions and
explanations.
Chapter 6 – Question 01
The pseudocode below illustrates the basic push() and pop() operations of an
array-based stack.
a. What data have a race condition?
The race condition in this pseudocode arises primarily due to concurrent access
to shared data without proper synchronization. The shared data involved in the
stack operations are:
top – the index indicating the current top of the stack.
stack[] – the array holding the stack elements.
Race condition details:
top is both read and modified in both push() and pop(). If two threads
call push() or pop() simultaneously, they might:
o Overwrite each other's updates to top.
o Read an outdated value of top, leading to incorrect insertion or
deletion.
stack[top] can also be written or read at the same index by multiple
threads, leading to:
o Inconsistent or corrupted data being pushed or popped.
b. How could the race condition be fixed?
Race conditions can be fixed using mutual exclusion (mutex) mechanisms, such
as locks or semaphores, which the pseudocode tries to represent using
acquire() and release().
However, the issue here is incomplete or incorrectly scoped locking, especially
in is_empty().