Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write a program given the following instructions and goal. at the bottom is a skeleton code to get started. It may need to be changed

write a program given the following instructions and goal. at the bottom is a skeleton code to get started. It may need to be changed to implement threads. Its a starting point so modify for thread usage using C language

image text in transcribed

/* * Stack containing race conditions */

#include

#include

#include

// Linked list node

typedef int value_t;

typedef struct Node {

value_t data;

struct Node *next;

} StackNode;

// Stack function declarations

void push (value_t v, StackNode **top);

value_t pop ( StackNode **top);

int is_empty( StackNode *top);

int main(void) {

StackNode *top = NULL;

push(5, &top);

push(10,&top);

pop ( &top);

push(15,&top);

pop ( &top);

pop ( &top);

push(20,&top);

push(-5, &top);

pop ( &top);

push(-10,&top);

pop ( &top);

pop ( &top);

push(-15,&top);

pop ( &top);

push(-20,&top);

return 0;

}

// Stack function definitions

void push(value_t v, StackNode **top)

{

StackNode * new_node = malloc(sizeof(StackNode));

new_node->data = v;

new_node->next = *top;

*top = new_node;

}

value_t pop(StackNode **top)

{

if (is_empty(*top)) return (value_t)0;

value_t data = (*top)->data;

StackNode * temp = *top;

*top = (*top)->next;

free(temp);

return data;

}

int is_empty(StackNode *top) {

if (top == NULL) return 1;

else return 0;

}

(20 pts) 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: tackNode *top push (5, &top); &top) pus , &top); pus value pop (&top) pop (&top) pop (&top) value value 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 Write one testStack function, and use it as the entry point for each thread. The testStack function should intermix 3 push operations with 3 pop operations in a loop that executes 500 times. 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 (20 pts) 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: tackNode *top push (5, &top); &top) pus , &top); pus value pop (&top) pop (&top) pop (&top) value value 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 Write one testStack function, and use it as the entry point for each thread. The testStack function should intermix 3 push operations with 3 pop operations in a loop that executes 500 times. 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions

Question

1. Does your voice project confidence? Authority?

Answered: 1 week ago