Question: Safe Stack 1 ) The ( C ) program stack - ptr . c ( provided ) contains an implementation of a stack

Safe Stack
1) The \( C \) program stack-ptr.c (provided) contains an implementation of a stack using a linked list.
An example of its use is as follows:
```
StackNode *top = NULL;
...
push (5, &top);
push (10, &top);
push (15, &top);
...
int value = pop(&top);
value = pop(&top);
value = pop(&top);
```
`.\
This program currently has a race condition and is not appropriate for a concurrent environment. Using Pthreads mutex locks (section 7.3.1), fix the race conditions. Test your now-thread-safe stack by creating 200 concurrent threads in main() that intermix pushing and popping values.
Use a loop in main() to create all those threads. Apply all the things you've learned about creating and joining threads from previous chapters.
\(\square \) Write one testStack function, and use it as the entry point for each thread.
\(\square \) The testStack function should intermix 3 push operations with 3 pop operations in a loop that executes 500 times.
\(\square \) All threads use the same stack.
gcc -pthread stack-ptr.c -o stack-ptr is an example command to compile and link your program
If you're up for it, you may rewrite the provided C program into well-formed C++ code using proper Object Oriented Design concepts, but you must maintain the dynamically allocated linked list concept, and you must still use the Pthreads mutex locks.
Safe Stack 1 ) The \ ( C \ ) program stack - ptr

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 Programming Questions!