The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race...

90.2K

Verified Solution

Question

Programming

The program (​ stack-ptr.c​ ) implements stack using a linkedlist, however, it contains a race condition and is not appropriatefor a concurrent environment. Using Pthreads mutex locks, fix therace condition. For reference, see Section 7.3.1 of SGGbook.(Section 7.3.1 is about mutex and semaphores it doesexplain how to implement I'm just having a hard time finding therace condition within the code)

/*
* Stack containing race conditions
*/

#include
#include
#include

typedef int value_t;

// Node structure
typedef struct Node
{
value_t data;
struct Node *next;
} StackNode;

// function prototypes
void push(value_t v, StackNode **top, pthread_mutex_t*mutex);
value_t pop(StackNode **top, pthread_mutex_t *mutex);
int is_empty(StackNode *top);


void push(value_t v, StackNode **top, pthread_mutex_t *mutex)
{
StackNode *new_node;
new_node = (StackNode *)malloc(sizeof(StackNode));
  
new_node->data = v;
  
// mutex lock and unlock code

  
}

value_t pop(StackNode **top, pthread_mutex_t *mutex)
{
   StackNode *temp;
  
   pthread_mutex_lock(mutex);
  
// mutex lock and unlock code based on empty or full stack
  
}

int is_empty(StackNode *top) {
if (top == NULL)
return 1;
else
return 0;
}

int main(void)
{
StackNode *top = NULL;


// pthread_mutex variable declarion and verify push/pop operationfor 4 inputs (5, 10, 15, 20)


  
   return 0;
}

Answer & Explanation Solved by verified expert
3.7 Ratings (485 Votes)
include include include include struct emptystack stdexception const char what const throw template class threadsafestack    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students