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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899589
Joined: Mon Aug 02, 2021 8:13 am

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

Post by answerhappygod »

Problem A The Pseudocode As Below Illustrates The Basic Push And Pop Operations Of An Array Based Stack Where Top 1
Problem A The Pseudocode As Below Illustrates The Basic Push And Pop Operations Of An Array Based Stack Where Top 1 (391.26 KiB) Viewed 37 times
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 PO is about to call push() and process P1 is about to call pop() concurrently. a. Which variable has a race condition? LIST all the items on stack when top is equal to 8 (hint: items on stack are stack[0], stack[1] ..., stack[???]) b. In which order LINES A, B, X, and Y are executed such that an item is correctly pushed onto the stack as stack by PO first and then popped out by P1? What is the value of i? What is the value of top when PO is done with pushing? What is the value of top when P1 is done with popping? c. In which order LINES A, B, X, and Y are executed such that an item stack [j] is correctly popped out of the stack by P1 first and then PO pushes an item onto the stack as stack [k]? What are the values of j and k? What is the value of top when P1 is done with popping? What is the value of top when PO is done with pushing? d. In which order LINES A, B, X, and Y are executed such that PO adds an item onto the stack as stack [8] first, then P1 reads stack [7], finally top still remains as 8 after both PO and P1 are done? This is a case where PO's push operation is voided. e. In which order LINES A, B, X, and Y are executed such that PO first incorrectly replaced the original item stack [7] on stack with its new item, then P1 reads stack [8] with a random value, finally top still remains as 8 after both PO and P1 are done? This is a case where P1's pop operation is voided. void push (int item) { if (top SIZE) { stack [top] = item; top++; } int pop() { } else ERROR //no return or exit if (top > 0) { top--; return stack [top]; // LINE A // LINE B } // LINE X // LINE Y else { ERROR //no return or exit return -1;
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply