COS3721 Assignment 2 2025
Unique Number:
Due date: 23 June 2025
Chapter 6 – Question 01
a. What data have a race condition?
In the given pseudocode for an array-based stack in a concurrent environment, the variable top is subject to a
race condition. This variable is accessed and modified in both the push() and pop() operations. If two
threads execute either of these functions at the same time without proper synchronization, they may read and
update the top value simultaneously, leading to inconsistent or corrupted stack state (e.g. data loss or
overwriting).
b. How could the race condition be fixed?
The pseudocode already attempts to use acquire() and release() to create a critical section, suggesting
some kind of locking mechanism (like a mutex or semaphore). However, to properly fix the race condition:
Ensure that acquire() is implemented using a thread-safe mutual exclusion mechanism like a mutex.
Only one thread should be allowed to enter the critical section at a time where top and stack[top]
are accessed or modified.
Also, make sure release() is always called after acquire() (including in error branches) to avoid
DISCLAIMER & TERMS OF USE
Educational Aid: These study notes are intended to be used as educational resources and should not be seen as a
replacement for individual research, critical analysis, or professional consultation. Students are encouraged to perform
their own research and seek advice from their instructors or academic advisors for specific assignment guidelines.
Personal Responsibility: While every effort has been made to ensure the accuracy and reliability of the information in
these study notes, the seller does not guarantee the completeness or correctness of all content. The buyer is
responsible for verifying the accuracy of the information and exercising their own judgment when applying it to their
assignments.
Academic Integrity: It is essential for students to maintain academic integrity and follow their institution's policies
regarding plagiarism, citation, and referencing. These study notes should be used as learning tools and sources of
inspiration. Any direct reproduction of the content without proper citation and acknowledgment may be considered
academic misconduct.
Limited Liability: The seller shall not be liable for any direct or indirect damages, losses, or consequences arising from
the use of these notes. This includes, but is not limited to, poor academic performance, penalties, or any other negative
consequences resulting from the application or misuse of the information provided.
Unique Number:
Due date: 23 June 2025
Chapter 6 – Question 01
a. What data have a race condition?
In the given pseudocode for an array-based stack in a concurrent environment, the variable top is subject to a
race condition. This variable is accessed and modified in both the push() and pop() operations. If two
threads execute either of these functions at the same time without proper synchronization, they may read and
update the top value simultaneously, leading to inconsistent or corrupted stack state (e.g. data loss or
overwriting).
b. How could the race condition be fixed?
The pseudocode already attempts to use acquire() and release() to create a critical section, suggesting
some kind of locking mechanism (like a mutex or semaphore). However, to properly fix the race condition:
Ensure that acquire() is implemented using a thread-safe mutual exclusion mechanism like a mutex.
Only one thread should be allowed to enter the critical section at a time where top and stack[top]
are accessed or modified.
Also, make sure release() is always called after acquire() (including in error branches) to avoid
DISCLAIMER & TERMS OF USE
Educational Aid: These study notes are intended to be used as educational resources and should not be seen as a
replacement for individual research, critical analysis, or professional consultation. Students are encouraged to perform
their own research and seek advice from their instructors or academic advisors for specific assignment guidelines.
Personal Responsibility: While every effort has been made to ensure the accuracy and reliability of the information in
these study notes, the seller does not guarantee the completeness or correctness of all content. The buyer is
responsible for verifying the accuracy of the information and exercising their own judgment when applying it to their
assignments.
Academic Integrity: It is essential for students to maintain academic integrity and follow their institution's policies
regarding plagiarism, citation, and referencing. These study notes should be used as learning tools and sources of
inspiration. Any direct reproduction of the content without proper citation and acknowledgment may be considered
academic misconduct.
Limited Liability: The seller shall not be liable for any direct or indirect damages, losses, or consequences arising from
the use of these notes. This includes, but is not limited to, poor academic performance, penalties, or any other negative
consequences resulting from the application or misuse of the information provided.