-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart3.c
155 lines (127 loc) · 2.64 KB
/
part3.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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int tot_sum;
void *runner(void *param);
int sum[100];
void *grand_runner(void *param);
pthread_t tid[60];
pthread_attr_t attr[60];
int main(int argc,char* argv[])
{
char *a = argv[1];
char *b = argv[2];
char *c = argv[3];
int arg3 = atoi(c);
int arg2=atoi(b);
int arg1=atoi(a);
int i;
int j;
int temp = -arg3;
char argument[100];
printf("Parent Thread is start executing\n");
for(int i = 0;i<arg2*arg3+arg2 ; i=i+arg3+1)
{
temp +=arg3;
snprintf(argument,100,"%d,%d,%d,%d,%d\n",i,(temp/arg3)*(arg1/arg2)+1,(temp/arg3+1)*(arg1/arg2),arg3,arg1/arg2);
pthread_attr_init(&attr[i]);
pthread_create(&tid[i],&attr[i],runner,(void *)&argument);
pthread_join(tid[i],NULL);
}
for (int j =0 ; j<arg2*arg3+arg2 ; j=j+arg3+1)
{
tot_sum+=sum[j];
}
printf("Toatl sum = %d\n",tot_sum);
printf("Parent end of execution\n");
pthread_exit(NULL);
return 0;
}
void *runner(void *param)
{
char *str = (char *)param;
char arg[50];
int temp=-1;
int count =0;
int idx;
int start;
int end;
int arg3;
int size;
char *pt;
pt = strtok (str,",");
while (pt != NULL) {
count++;
if (count == 1)
{
idx = atoi(pt);
}
else if (count == 2)
{
start = atoi(pt);
}
else if(count==3)
{
end = atoi(pt);
}
else if(count == 4)
{
arg3=atoi(pt);
}
else
{
size=atoi(pt);
}
pt = strtok (NULL, ",");
}
for (int i=idx+1;i<=idx+arg3; i++)
{
temp+=1;
snprintf(arg,50,"%d,%d,%d,%d\n",start+(size/arg3)*temp,(start+(size/arg3)*temp)+(size/arg3)-1,idx,i);
pthread_attr_init(&attr[i]);
pthread_create(&tid[i],&attr[i],grand_runner,(void *)&arg);
pthread_join(tid[i],NULL);
}
printf("Child Thread Number %d\t child start = %d\t end = %d\t Child sum = %d\n",idx,start,end,sum[idx]);
pthread_exit(NULL);
}
void *grand_runner(void *param)
{
char *str = (char *)param;
int grand_sum=0;
int count =0;
int idx;
int start;
int end;
int i;
int j;
char *pt;
pt = strtok (str,",");
while (pt != NULL) {
count++;
if (count == 1)
{
start = atoi(pt);
}
else if (count == 2)
{
end= atoi(pt);
}
else if(count == 3)
{
idx = atoi(pt);
}
else
{
i = atoi(pt);
}
pt = strtok (NULL, ",");
}
for (j=start;j<=end; j++){
grand_sum += j;
}
sum[idx]+=grand_sum;
printf("Child Thread Number %d\t GrandChild Thread Number = %d\t GrandChild Start = %d\t GrandChild End = %d\t Grand child sum = %d\n",idx,i,start,end,grand_sum);
pthread_exit(NULL);
}