Use Fork() to create Child process that use separate threads

Hey guys, How can I create a fork()/Child thread process for each computation in my program: Average, median, and standard deviation of the grade population, display the sorted the grades, and display a graph of the statistics all while using the prototype: void* nameOfFunction(void* param) for each function called to execute the thread and the Pthread create_thread system call to create each required thread.


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
#include <iostream>

#include <iomanip>

#include <cmath>

using namespace std;

// function declarations

double getAverage(int* grades, int size);

double getMedian(int* grades, int size);

double getSd(int* grades, int size);

void sortGrades(int* grades, int size);

int main()

{

// Declaring variables

int size, val;

// setting the precision to two decimal places

std::cout << std::setprecision(2) << std::fixed;

// Getting the input entered by the user

cout << "How many Number of Grades For Child Process?? :";

cin >> size;

// Creating array dynamically

int* grades = new int[size];

/* Getting the inputs entered by the user

* and populate those values into array

*/

for (int i = 0; i < size;)

{

while (true)

{

cout << "Enter the grade for each #" << i + 1 << ":";

cin >> val;

if (val < 0 || val>100)

{

cout << "** Invalid.Must be between 0-100. **" << endl;

continue;

}

else

{

grades[i] = val;

i++;

break;

}

}

}

// calling the functions

double avg = getAverage(grades, size);

double median = getMedian(grades, size);

double sd = getSd(grades, size);

sortGrades(grades,size);

  

//displaying the average , median ,Standard Deviation
cout<<"Child process has completed the average computation"<<endl;
cout <<"Child process can now display the Average! :"<<avg<<endl;

cout<<"Child process has completed the median computation"<<endl;
cout <<"Child process can now display the Median!:" << median << endl;

cout<<"Child process has completed the Standard-deviation computation"<<endl;
cout << "Child process can now display the Standard Deviation :" << sd << endl;

  

//Displaying the array elements after sorting

cout<<"Child process will now display the sorted Grades After Sorting the Grades :"<<endl;

for(int i=0;i<size;i++)

{

cout<<grades[i]<<" ";

}

cout<<endl;

//Displaying the graph
cout<<"Child process will now Graph"<<endl;
cout<<"\nGraph is displayed below:"<<endl;

for(int n=0;n<100;n+=20)

{

cout<<n<<"-"<<n+20<<"\t";

for(int m=0;m<size;m++)

{

if(grades[m]>=n && grades[m]<n+20)

cout<<"*";

}

cout<<endl;

}
cout<<"Child process has completed graphing"<<endl;
cout<<"Child process has completed all task"<<endl; 



return 0;

}

// This function calculates the average of grades

double getAverage(int* grades, int size)

{

double sum = 0.0;

// calculating the sum of res[] array elements

for (int i = 0; i < size; i++)

{

// calculating the sum

sum += grades[i];

}

// calculating the average

double avg = sum / size;

return avg;

}

// This function calculates the median of grades

double getMedian(int* grades, int size)

{

// This Logic will Sort the Array of elements in Ascending order

int temp;

int middle;

double median;

for (int i = 0; i < size; i++)

{

for (int j = i + 1; j < size; j++)

{

if (grades[i] > grades[j])

{

temp = grades[i];

grades[i] = grades[j];

grades[j] = temp;

}

}

}

if (size % 2 == 0)

{

middle = size / 2;

median = (float)(grades[middle - 1] + grades[middle]) / 2.0;

}

else

{

middle = (size + 1) / 2;

median = grades[middle];

}

return median;

}

// This function calculates the standard deviation of grades

double getSd(int* grades, int size)

{

int sum_of_squares = 0, standard_deviation;

double variance;

double avg = getAverage(grades, size);

/* This loop Calculating the sum of

* square of eeach element in the array

*/

for (int i = 0; i < size; i++)

{

/* Calculating the sum of square of

* each element in the array

*/

sum_of_squares += pow((grades[i] - avg), 2);

}

// calculating the variance of an array

variance = ((double)sum_of_squares / (size));

// calculating the standard deviation of an array

standard_deviation = sqrt(variance);

return standard_deviation;

}

void sortGrades(int* grades, int size)

{

//This Logic will Sort the Array of elements in Ascending order

int temp;

for (int i = 0; i < size; i++)

{

for (int j = i + 1; j < size; j++)

{

if (grades[i] > grades[j])

{

temp = grades[i];

grades[i] = grades[j];

grades[j] = temp;

}

}

}

}
Last edited on
I don't quite understand your question. fork() creates a new process. Separate processes by definition execute separate threads, therefore fork(), in creating a new process, creates a new thread as well.
Which is it? Do you want to create a new process (with a new thread to go with it) or create a new thread within the same process?
the main function, the child process uses separate threads to compute the average, median, and standard deviation of the grade population, display the sorted the grades, and display a graph of the statistics. threads belonging to the same process can share global data
Last edited on
Topic archived. No new replies allowed.