-
Notifications
You must be signed in to change notification settings - Fork 2
/
linked_list_one_mutex.c
227 lines (185 loc) · 4.98 KB
/
linked_list_one_mutex.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <math.h>
//Maximum key value
const int MAX_KEY = 65536;
//Maximum thread count
const int MAX_THREADS = 1024;
struct node {
int val;
struct node* next;
};
// Shared variables
struct node* head = NULL;
int n = 1000; //Initial population element count
int m = 10000; //Total random operations count
//These values must be changed according to each case
float mMember = 0.50; //Fraction of Member
float mInsert = 0.25; //Fraction of Insert
float mDelete = 0.25; //Fraction of Delete
int thread_count = 1; //Thread Count
double start_time, finish_time, time_elapsed;
int member_count=0; //Member function call count
int insert_count=0; //Insert function call count
int delete_count=0; //Delete function call count
pthread_mutex_t mutex;
pthread_mutex_t count_mutex;
int Member(int value);
int Insert(int value);
int Delete(int value);
void Clear_Memory(void);
int Is_Empty(void);
void* Thread_Function(void* rank);
int main(int argc, char* argv[]) {
if (argc != 2){
fprintf(stderr, "please provide a command line argument for thread count less than %d\n", MAX_THREADS);
exit(0);
}
thread_count = strtol(argv[1], NULL, 10);
if (thread_count <= 0 || thread_count > MAX_THREADS){
fprintf(stderr, "please provide a command line argument for thread count less than %d\n", MAX_THREADS);
exit(0);
}
int i=0;
//Inserting elements to linked-list
for(;i<n;i++){
int r = rand()%65536;
if(!Insert(r)){
i--;
}
}
pthread_t* thread_handles;
thread_handles = malloc(thread_count*sizeof(pthread_t));
pthread_mutex_init(&mutex, NULL);
pthread_mutex_init(&count_mutex, NULL);
start_time = clock();
for (i = 0; i < thread_count; i++)
pthread_create(&thread_handles[i], NULL, Thread_Function, (void*) i);
for (i = 0; i < thread_count; i++)
pthread_join(thread_handles[i], NULL);
finish_time = clock();
time_elapsed = (finish_time - start_time)/CLOCKS_PER_SEC;
//printf("Time Elapsed = %.10f seconds\n", time_elapsed);
printf("%.10f\n", time_elapsed);
Clear_Memory();
pthread_mutex_destroy(&mutex);
pthread_mutex_destroy(&count_mutex);
free(thread_handles);
return 0;
}
//Member Function
int Member(int value) {
struct node* temp;
temp = head;
while (temp != NULL && temp->val < value)
temp = temp->next;
if (temp == NULL || temp->val > value) {
//printf("%d is a member in linked-list\n", value);
return 0;
} else {
//printf("%d is a member in linked-list\n", value);
return 1;
}
}
// Insert function
int Insert(int value) {
struct node* current = head;
struct node* previous = NULL;
struct node* temp;
int return_value = 1;
while (current != NULL && current->val < value) {
previous = current;
current = current->next;
}
if (current == NULL || current->val > value) {
temp = malloc(sizeof(struct node));
temp->val = value;
temp->next = current;
if (previous == NULL)
head = temp;
else
previous->next = temp;
} else {
return_value = 0;
}
return return_value;
}
//Delete Function
int Delete(int value) {
struct node* current = head;
struct node* previous = NULL;
int return_value = 1;
while (current != NULL && current->val < value) {
previous = current;
current = current->next;
}
if (current != NULL && current->val == value) {
if (previous == NULL) {
head = current->next;
free(current);
} else {
previous->next = current->next;
free(current);
}
} else {
return_value = 0;
//printf("%d is not in the linked-list\n", value);
}
return return_value;
}
//Function to free memory used for linked-list
void Clear_Memory(void) {
struct node* current;
struct node* next;
if (Is_Empty()) return;
current = head;
next = current->next;
while (next != NULL) {
free(current);
current = next;
next = current->next;
}
free(current);
}
//Function to check if linked-list is empty
int Is_Empty(void) {
if (head == NULL)
return 1;
else
return 0;
}
void* Thread_Function(void* rank) {
int i, val;
int my_member=0;
int my_insert=0;
int my_delete=0;
int ops_per_thread = m/thread_count;
for (i = 0; i < ops_per_thread; i++) {
float operation_choice = (rand()%10000/10000.0);
val = rand()%MAX_KEY;
if (operation_choice < mMember) {
pthread_mutex_lock(&mutex);
Member(val);
pthread_mutex_unlock(&mutex);
my_member++;
} else if (operation_choice < mMember + mInsert) {
pthread_mutex_lock(&mutex);
Insert(val);
pthread_mutex_unlock(&mutex);
my_insert++;
} else {
pthread_mutex_lock(&mutex);
Delete(val);
pthread_mutex_unlock(&mutex);
my_delete++;
}
}
pthread_mutex_lock(&count_mutex);
member_count += my_member;
insert_count += my_insert;
delete_count += my_delete;
pthread_mutex_unlock(&count_mutex);
return NULL;
}