-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
131 lines (100 loc) · 2.46 KB
/
stack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
CP386 A4 Q1 (stack.c)
Author: Rhea Sharma & Mobina Tooranisama
ID: 200576620 & 200296720
Email: [email protected] & [email protected]
Date: March , 2023
**/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 10
//StackNode struct
typedef struct Node {
int data;
struct Node *next;
} StackNode;
StackNode *top;
//function prototypes
pthread_mutex_t mutex_lock;
void push(int v, StackNode **top);
int pop(StackNode **top);
int is_empty(StackNode *top);
//push
void push(int v, StackNode **top){
StackNode *new_node;
printf("Thread is running Push() operation fro value: %d\n", v);
new_node = (StackNode *)malloc(sizeof(StackNode));
new_node->data = v;
new_node->next = *top;
*top = new_node;
}
//pop function
int pop(StackNode **top){
StackNode *temp;
if(is_empty(*top)){
printf("Stack empty \n");
return 0;
}
else{
int data = (*top)->data;
printf("Thread is runnning Pop() operation and value is: %d\n", data);
temp = *top;
*top = (*top)->next;
free(temp);
return data;
}
}
//is_empty function to Check if top is NULL
int is_empty(StackNode *top){
if (top == NULL){
return 1;
}
else{
return 0;
}
}
//Thread's push function
void* thread_push(void *args){
pthread_mutex_trylock(&mutex_lock);
//int i;
int *threadId = (int *)args;
push(*threadId + 1, &top);
pthread_mutex_unlock(&mutex_lock);
return NULL;
}
//Thread's pop function
void* thread_pop(){
pthread_mutex_trylock(&mutex_lock);
pop(&top);
pthread_mutex_unlock(&mutex_lock);
return NULL;
}
//main function
int main(void){
int thread_arg[NUM_THREADS];
int i, j;
//StackNode *top = NULL;
pthread_t threads_push[NUM_THREADS];
pthread_t threads_pop[NUM_THREADS];
//creating pop threads
for (i = 0; i < NUM_THREADS; i++){
thread_arg[i] = i;
pthread_create(&threads_push[i], NULL, thread_push, (void *)&thread_arg[i]);
}
//create pop threads
for (i = 0; i < NUM_THREADS; i++){
thread_arg[i] = i;
pthread_create(&threads_pop[i], NULL, thread_pop, NULL);
}
//join push threads
for (j = 0; j < NUM_THREADS; j++){
pthread_join(threads_push[j], NULL);
}
//join pop threads
for (j = 0; j < NUM_THREADS; j++){
pthread_join(threads_pop[j], NULL);
}
pthread_exit(NULL);
return 0;
}