/
main.c
326 lines (280 loc) · 7.57 KB
/
main.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdbool.h>
#include <time.h>
#include "timer.h"
#include <math.h>
/** define number of member, insert and delete operations **/
static float const memberFunction_count=9900;
static float const insertFunction_count=50;
static float const deleteFunction_count=50;
static int const THREAD_COUNT = 2;
static int const SAMPLE_SIZE = 100;
/** node structure **/
struct node
{
int value;
struct node *next;
};
struct node * head=NULL;
int memberFunctionCount;
int insertFunctionCount;
int deleteFunctionCount;
int totalOps;
pthread_mutex_t mutexList,mutexTotalOps;
pthread_rwlockattr_t rwlock;
int member_ratio=990;
int insert_ratio=5;
int delete_ratio=5;
void *threadOperations();
struct node * init();
bool insert_node(int val,int thread_id);
bool delete_node(int val,int thread_id);
struct node * member(int val,int thread_id);
void * executeThreads(void * rank);
int main()
{
int iteration = 0;
float runtime[SAMPLE_SIZE] ;
for(iteration = 0; iteration < SAMPLE_SIZE; iteration ++)
{
double start, finish;
pthread_t * thread;
/** initialize mutex and read write lock **/
//pthread_mutex_init(&mutexList,NULL);
pthread_mutex_init(&mutexTotalOps,NULL);
pthread_rwlock_init(&rwlock,NULL);
memberFunctionCount=memberFunction_count;
insertFunctionCount=insertFunction_count;
deleteFunctionCount=deleteFunction_count;
totalOps=memberFunction_count+insertFunction_count+deleteFunction_count;
int i=0; // thread ID
int numberOfThreads=THREAD_COUNT; // number of threads
init();
GET_TIME(start); // start timer
thread=malloc(numberOfThreads * sizeof(pthread_t));
/** create threads **/
for(i=0; i<numberOfThreads; i++)
{
pthread_create(&thread[i],NULL,executeThreads,(void *) i);
}
//printf("threads created\n");
/** wait main thread until other threads are finished **/
for(i=0; i<numberOfThreads; i++)
{
pthread_join(thread[i],NULL);
}
GET_TIME(finish); // stop timer
double totalTime = finish - start;
printf("%d Time Taken to Complete Task: %f\n",iteration+1,totalTime);
runtime[iteration] = totalTime;
free(thread);
}
float average = 0;
float sd = 0;
int j = 0;
for(j=0; j<SAMPLE_SIZE; j++)
{
average = average + runtime[j];
}
average = average/SAMPLE_SIZE;
for(j=0; j<SAMPLE_SIZE; j++)
{
sd = sd + pow((runtime[j]-average),2);
}
sd = sd/SAMPLE_SIZE;
sd = sqrt(sd);
printf("Mean: %f SD: %f \n",average,sd);
return 0;
}
/** create a linklist with 1000 elements and return head **/
struct node * init()
{
int i=0;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
head->value=NULL;
struct node * node;
for(i=0; i<1000; i++)
{
node=(struct node *)malloc(sizeof(struct node));
node->value=i;
node->next=head->next;
head->next=node;
}
return head;
}
/** insert a node to the linklist **/
bool insert_node(int val,int thread_id)
{
if(insertFunctionCount <= 0)
{
return false;
}
insertFunctionCount--;
//printf("insert|Thread_id: %d\n",thread_id);
struct node * node=(struct node *)malloc(sizeof(struct node));
node->value=val;
node->next=head->next;
head->next=node;
//printf("Thread_id: %d inserted Value : %d\n",thread_id,val);
return true;
}
/** delete a node by value, return true if success otherwise false **/
bool delete_node(int val,int thread_id)
{
if(deleteFunctionCount <= 0)
{
return false;
}
deleteFunctionCount--;
//printf("delete|Thread_id: %d\n",thread_id);
struct node * temp;
struct node * nodePrev;
if(head->next == NULL)
{
return false;
}
else
{
temp=head->next;
nodePrev=head;
}
while(temp != NULL && temp->value != val)
{
nodePrev=temp;
temp=temp->next;
}
if(temp == NULL)
{
return false;
}
else
{
nodePrev->next=temp->next;
free(temp);
//printf("Thread_id: %d deleted Value : %d\n",thread_id,val);
return true;
}
}
/** search a node by value, return node if exist otherwise NULL **/
struct node *member(int val,int thread_id)
{
if(memberFunctionCount <= 0)
{
return NULL;
}
memberFunctionCount--;
//printf("member|Thread_id: %d\n",thread_id);
struct node * temp;
if(head->next == NULL)
return NULL;
temp=head->next;
while(temp->next != NULL)
{
if(temp->value == val)
{
//printf("Thread_id: %d Searched Value : %d\n",thread_id,val);
return temp;
}
else
temp=temp->next;
}
return NULL;
}
/** thread function **/
void * executeThreads(void *rank)
{
srand(time(NULL)); // initialize random seed
while(totalOps > 0)
{
/** mutex for totalOps global variable**/
pthread_mutex_lock(&mutexTotalOps);
totalOps--;
pthread_mutex_unlock(&mutexTotalOps);
if (insertFunctionCount == 0 && memberFunctionCount == 0 && deleteFunctionCount == 0)
{
break;
}
int rndVal=rand()%3+1; // generate random value from 1 to 1000
/** adjust pre defined ranges by considering function count **/
if(memberFunctionCount == 0)
{
if (insertFunctionCount == 0)
{
rndVal=3;
}
else if(deleteFunctionCount == 0)
{
rndVal=2;
}
else
{
rndVal=rand()%2+2;
}
}
else if (insertFunctionCount == 0)
{
if (memberFunctionCount == 0)
{
rndVal=3;
}
else if(deleteFunctionCount == 0)
{
rndVal=1;
}
else
{
int array[]= {1,3};
rndVal=array[rand()%2];
}
}
else if(deleteFunctionCount == 0)
{
if (memberFunctionCount == 0)
{
rndVal=2;
}
else if(insertFunctionCount == 0)
{
rndVal=1;
}
else
{
rndVal=rand()%2+1;
}
}
/** execute relevant operation according to the pre defined limit ranges **/
if(rndVal == 1)
{
pthread_rwlock_rdlock(&rwlock);
//pthread_mutex_lock(&mutexList);
member(rand()%1500+1,(int)rank);
//pthread_mutex_unlock(&mutexList);
pthread_rwlock_unlock(&rwlock);
}
else if(rndVal == 2)
{
//pthread_mutex_lock(&mutexList);
pthread_rwlock_wrlock(&rwlock);
insert_node(rand()%1000+1001,(int)rank);
//pthread_mutex_unlock(&mutexList);
pthread_rwlock_unlock(&rwlock);
}
else if(rndVal == 3)
{
pthread_rwlock_wrlock(&rwlock);
//pthread_mutex_lock(&mutexList);
delete_node(rand()%1500+1,(int)rank);
//pthread_mutex_unlock(&mutexList);
pthread_rwlock_unlock(&rwlock);
}
}
return NULL;
}
/* questions
seed is not defined
use of 1-2^16
ranges of insert delete and member function
*/