-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegrator.c
95 lines (70 loc) · 1.54 KB
/
Integrator.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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
double result = 0;
double function (double x) {
printf ("f(x) = %lf \n", x);
return x * x;
}
double integrator (double a, double b) {
return 0.5 * (b - a) * (function(b) + function(a));
}
int main (int argc, char* argv[]) {
double temporary_result;
FILE *fp;
fp = fopen ("result.txt", "w+");
fprintf(fp, "%f", result);
fclose (fp);
//fseek (fp, 0, SEEK_SET);
int rang = atoi(argv[1]); //vvodim chislo kratnoe 3
int n = rang;
rang /= 3;
double range [n];
int k = rang;
for (int i = 1; i < n; i ++) {
if (i % 2 || i == 1) {
range[i] = k;
//rang += rang;
}
else {
range[i] = k;
k += rang;
}
printf("%lf \n", range[i]);
}
for (int i = 1; i < n; i += 2) {
int pid = fork();
if (pid == -1) {
perror ("unable to fork");
exit (1);
}
else
if (!pid) { //child
fp = fopen ("result.txt", "r+");
printf("IM A CHILD NUMBER %d \n", i);
fscanf (fp, "%lf", &result);
fseek (fp, 0, SEEK_SET);
//result += 5;
printf("I got a number = %lf \n", integrator(range[i - 1], range[i]));
result += integrator (range[i - 1], range[i]);
fprintf(fp, "%lf", result);
//fseek (fp, 0, SEEK_SET);
fclose (fp);
exit(0);
}
else {
waitpid (pid, 0, 0);
}
}
fp = fopen ("result.txt", "r+");
fscanf (fp, "%lf", &result);
printf ("the result is %lf", result);
fclose (fp);
exit (0);
}