-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththreading.c
More file actions
128 lines (103 loc) · 3.07 KB
/
threading.c
File metadata and controls
128 lines (103 loc) · 3.07 KB
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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
//USER DEFINED GLOBAL VARIABLES
enum {numberofelements=10000,NUM_THREADS=50};
char string[]="DEVIL MAY CRY";
//global variables
int sizeofstring=0;
int sum=0;
int asd[numberofelements];//array that will be summed
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER; //semaphore
int fakeevent=0; //fake event type object
//Summation function
void *worker_sum(void *threadid)
{
long tid;
int psum=0;
int i;
tid = (long)threadid;
printf("Sum thread #%li starting\n", tid);
//Chunks of the array that will be summed is allocated to each worker based on the ratio
//of number of elements to threads.
for(i=tid*(numberofelements/NUM_THREADS);i<(tid+1)*(numberofelements/NUM_THREADS);i++){
psum+=asd[i];
}
//semaphore while accessing sum variable and a fake event counter
pthread_mutex_lock(&mymutex);
sum+=psum;
fakeevent++;
pthread_mutex_unlock(&mymutex);
printf("Sum thread #%li exiting\n", tid);
pthread_exit(0);
}
void *worker_reversestring(void *threadid)
{
long tid;
char temp;
tid = (long)threadid;
printf("Reverse thread #%li starting\n", tid);
//Reverses string
temp=string[tid];
string[tid]=string[sizeofstring-1-tid];
string[sizeofstring-1-tid]=temp;
//Increment fake event counter and printf statement
pthread_mutex_lock(&mymutex);
printf("Current status of string: %s\n",string);
fakeevent++;
pthread_mutex_unlock(&mymutex);
printf("Reverse thread #%li exiting\n", tid);
pthread_exit(0);
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
//***Sum of numbers***//
long t=0;
//Initialize array
for(t=0;t<numberofelements;t++){
asd[t]=t+1;
}
//creates threads and executes summation function
for(t=0;t<NUM_THREADS;t++){
printf("Main-creating thread %li to sum array.\n", t);
rc = pthread_create(&threads[t], NULL, worker_sum, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
//Event to print out the final sum only after all threads have finished
while(1){
if(fakeevent==NUM_THREADS){
printf("The sum is %i\n", sum);
break;
}
}
//***Reverse String***//
fakeevent=0;//reset fake event counter
//Finds size of given string
int x=0;
while(string[x]){
sizeofstring++;
x++;
}
for(t=0;t<sizeofstring/2;t++){
printf("Main-creating thread %li to reverse string.\n", t);
rc = pthread_create(&threads[t], NULL, worker_reversestring, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
//Event to print out the final sum only after all threads have finished
while(1){
if(fakeevent==sizeofstring/2){
printf("The reversed string is %s\n", string);
break;
}
}
pthread_exit(NULL);
return 0;
}