-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_thread.c
More file actions
123 lines (79 loc) · 1.94 KB
/
multi_thread.c
File metadata and controls
123 lines (79 loc) · 1.94 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
#define _GNU_SOURCE
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#define MAX_NUM 10
void *add_array (void *arg);
int thread_count;
long no_elts;
long range;
long *array;
long *thread_index;
void generate_numbers()
{
int i;
srandom(time(NULL));
for(i=0; i<no_elts; i++)
array[i] = random()%MAX_NUM;
}
void print_numbers()
{
int i;
for(i=0; i<no_elts; i++)
printf("%ld ", array[i]);
printf("\n");
}
int main (int argc, char **argv)
{
char *str_thread_count;
int r, trunc;
pthread_t *tid;
int sfd;
long total_sum = 0;
thread_count = atoi(argv[1]);
no_elts = atoi(argv[2]);
range = no_elts / thread_count;
thread_index = (long *) malloc(sizeof(long)*thread_count);
array = (long *) malloc(sizeof(long)*no_elts);
tid = (pthread_t*) malloc(sizeof(pthread_t)*thread_count);
if(tid == NULL)
perror ("Cannot allocate memory for threads\n");
generate_numbers();
print_numbers();
for (int i = 0; i < thread_count; i++) {
thread_index[i] = i;
if ((r = pthread_create (&tid [i], NULL, add_array, (void*)&thread_index[i])) != 0) {
perror("Cannot create multiple threads\n");
}
}
// Wait for threads to terminate
for (int i = 0; i < thread_count; i++) {
if ((r = pthread_join (tid [i], NULL)) != 0) {
perror("Cannot join multiple threads\n");
}
}
for (int i=0 ; i<thread_count; i++)
total_sum = total_sum + thread_index[i];
printf("Total Sum: %ld\n", total_sum);
exit (0);
}
void *add_array(void *args){
int id = *(int *)args;
long start = id*range;
long end = start + range;
long local_sum = 0;
for(long i = start; i < end; i++){
local_sum = array[i]+local_sum;
}
printf("Start:%ld End:%ld Sum:%ld\n", start, end, local_sum);
thread_index[id] = local_sum;
return NULL;
}