Question: Below is my C code. the program uses a stack that uses threads and mutex locks for race conditions. i just need to modify testStack
Below is my C code. the program uses a stack that uses threads and mutex locks for race conditions. i just need to modify testStack function should intermis 3 push operations and 3 pop operations in a loop that executes 500 times. Also test the code by creating 200 threads in main function by using a loop to create all those threads.
push(5, &top); push(10,&top); pop ( &top); push(15,&top); pop ( &top); pop ( &top); push(20,&top);
#include
#include
#include
// Linked list node
typedef int value_t;
typedef struct Node {
value_t data;
struct Node *next;
pthread_mutex_t lock;
} StackNode;
// Stack function declarations
void push (value_t v, StackNode **top);
value_t pop ( StackNode **top);
int is_empty( StackNode *top);
pthread_t tid[2];
StackNode *top = NULL;
void* testStackn() {
}
int main(void) {
}
// Stack function definitions
void push(value_t v, StackNode **top)
{
StackNode * new_node = malloc(sizeof(StackNode));
pthread_mutex_lock(&new_node->lock);// lock the current node to get it modify
new_node->data = v;
new_node->next = *top;
*top = new_node;
pthread_mutex_unlock(&new_node->lock);// unlock the current node to get it modify
}
value_t pop(StackNode **top)
{
if (is_empty(*top)) return (value_t)0;
pthread_mutex_lock(&(*top)->lock);// lock the current node to get it modify
value_t data = (*top)->data;
StackNode * temp = *top;
*top = (*top)->next;
free(temp);
pthread_mutex_unlock(&(*top)->lock);// unlock the current node to get it modify
return data;
}
int is_empty(StackNode *top) {
if (top == NULL) return 1;
else return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
