Question: Problem A. The pseudocode as below illustrates the basic push() and pop() operations of an array-based stack, where top is initialized as 0 when the

Problem A. The pseudocode as below illustrates the basic push() and pop() operations of an array-based stack, where top is initialized as 0 when the stack is empty. Assuming that at a given moment, SIZE is equal to 20, top is equal to 8, this code is used in a concurrent environment (top and stack[] are in shared memory section), process P0 is about to call push() and process P1 is about to call pop() concurrently.

push(item) {

if (top

stack[top] = item; // LINE A

top++; // LINE B }

else ERROR }

pop() {

if (top > 0) {

top--; // LINE X

return stack[top]; // LINE Y }

else ERROR }

Problem C. Use the atomic compare_and_swap instruction to fix the race condition in Problem A.

a. Write a C statement to declare and initialize a shared variable lockTop, whose value is -1 when unlocked and 0 when locked

b. Re-write the push() function to call the compare_and_swap() function defined in the PPT Slide 3.18 for Section III

c. Re-write the pop() function to call the compare_and_swap() function defined in the PPT Slide 3.18 for Section III

d. Does this solution satisfy the mutual-exclusion requirement? Does it satisfy the bounded-waiting requirement?

Just problem C

Problem A. The pseudocode as below illustrates the basic push() and pop()

compare and swap Instruction Definition: int compare_and swap (int *value, int expected, int new_value) ( int temp - *value; if (*value expected) *value new_value; return temp; 1. Executed atomically 2. Returns the original value of passed parameter "value" 3. Set the variable "value" the value of the passed parameter "new_value" but only if "value"expected". That is, the swap takes place only under this condition. Operating System Concepts-10th Edition 3.18 Silberschatz, Galvin and Gagne 2018 compare and swap Instruction Definition: int compare_and swap (int *value, int expected, int new_value) ( int temp - *value; if (*value expected) *value new_value; return temp; 1. Executed atomically 2. Returns the original value of passed parameter "value" 3. Set the variable "value" the value of the passed parameter "new_value" but only if "value"expected". That is, the swap takes place only under this condition. Operating System Concepts-10th Edition 3.18 Silberschatz, Galvin and Gagne 2018

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!