Question

Need help. write a C program stack-ptr.c that implements a stack using a link list. Below...

Need help. write a C program stack-ptr.c that implements a stack using a link list. Below is a skeleton code to start with.Jjust edit to make thread friendly.

examplpe:

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. use Pthread mutex locks to fix the race conditions. test you now thread safe stack by creating 200 concurrent threads in main() that push and pop values.

-use loop in main to create the threads

-write a teststack function, and use it as the entry point for each thread

-testStack function should intermis 3 push operations and 3 pop operations in a loop that executes 500 times

-all threads use the same stack

/* * Stack containing race conditions */

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

// 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;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

// 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* stackOperation(void* arg) {

  

push(5, &top);

  

push(10, &top);

  

push(15, &top);

  

int value = pop(&top);

printf("%d\n", value);

  

return NULL;

}

int main(void) {

  

  

int i = 0;

int err;

  

while(i < 200)

{

err = pthread_create(&(tid[i]), NULL, &stackOperation, NULL);

if (err != 0) {

  

}

  

i++;

}

  

push(5, &top);

  

push(10, &top);

  

push(15, &top);

  

int value = pop(&top);

printf("%d\n", value);

  

value = pop(&top);

printf("%d\n", value);

  

value = pop(&top);

  

printf("%d\n", value);

  

return 0;

  

}

// 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;

  

}

Add a comment
Know the answer?
Add Answer to:
Need help. write a C program stack-ptr.c that implements a stack using a link list. Below...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please answer in C++. Derive a class called Stack from the linked list described in Assignment...

    Please answer in C++. Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions...

  • C LANGUAGE I just need the void push() function, which inserts new node to the front...

    C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...

  • c program Here we see a Stack ADT implemented using array. We would like the stack...

    c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include <stdio.h> #include <stdlib.h> typedef struct { int *data;   // stack data, we assume integer for simplicity int top;     // top of the stack int maxSize; // max size of the stack } Stack; void StackInit(Stack* stack, int size) {     // this...

  • I need help with a c++ code, i am new learner on stack, please help me...

    I need help with a c++ code, i am new learner on stack, please help me write Stack.cpp according to Stack.h provided, thanks(Notes: That data structure is a singly linked list in which pushed items are placed at the tail of the linked list. Similarly, popped items will be removed from the tail of the list.) Actually,I can write a regular one, but i'm not sure how to use the tail in the question. class Stack { private: // Desc:...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h>...

    Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list...

  • Can I get a C++ code and output for this program using classes instead of using...

    Can I get a C++ code and output for this program using classes instead of using struct. The following program implements a Last In First Out (LIFO) stack. ( I want to use class for function definitions too and if main need.) #include <iostream> using namespace std; const int MAX = 100; struct stack {    int s[MAX]; // an array of integers    int top; // the index of the last number }; void push(stack &, int); void pop(stack&,...

  • Suppose you were debugging the push() function of your program. Which of the following variables would...

    Suppose you were debugging the push() function of your program. Which of the following variables would be accessible to the debugger before the function is called? static struct node *stack; static struct node *new_node() { int size = sizeof(struct node); return malloc(size); void push(void *value) { struct node *n = new_node(); n->value = value; n->next = stack; stack = n; return malloc(size); void push (void *value) { struct node *n = new_node(); n->value = value; n->next = stack; stack =...

  • Question 5 (1 point) Suppose you were debugging the following C code. Which of the following...

    Question 5 (1 point) Suppose you were debugging the following C code. Which of the following variables would be accessible to the debugger when the call to push() complete? static struct node *stack; static struct node *new_node() { int size = sizeof(struct node); return malloc(size); بب void push (void *value) { void push (void *value) { struct node *n = new_node(); n->value = value; n->next = stack; stack = n; } stack, value. n, size stack stack, n, value stack,...

  • Stacks are used by compilers to help in the process of evaluating expressions and generating machine...

    Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code.In this exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4and 7 / 9in which the operator (+ or / here) is written between its operands—this is called infix notation. Computers “prefer” postfix notation in which the operator is written to the right of its two operands. The preceding...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT