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 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...

  • 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...

  • 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,...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • 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...

  • I have a queue and stack class program that deals with a palindrome, I need someone...

    I have a queue and stack class program that deals with a palindrome, I need someone to help to put in templates then rerun the code. I'd greatly appreciate it. It's in C++. Here is my program: #include<iostream> #include<list> #include<iterator> #include<string> using namespace std; class Queue { public: list <char> queue; Queue() { list <char> queue; } void Push(char item) { queue.push_back(item); } char pop() { char first = queue.front(); queue.pop_front(); return first; } bool is_empty() { if(queue.empty()) { return...

  • 1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer...

    1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

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
ADVERTISEMENT