-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab8.c
More file actions
122 lines (95 loc) · 2.03 KB
/
Lab8.c
File metadata and controls
122 lines (95 loc) · 2.03 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
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <linux/limits.h>
#include <math.h>
#define MODE 0777
#define WRITE_CANAL 1
#define READ_CANAL 0
void StartWork();
void Process1(int);
void Process2(int, int);
void Process3(int);
char FileOutput[PATH_MAX];
int N, n;
int main (int argc, char **argv)
{
strcpy(FileOutput, "Output.txt");
if (argc < 3) {
fprintf(stderr, "Two few argument");
return 1;
}
if (N = atoi(argv[1]), N == 0) {
fprintf(stderr, "Error or too small number");
return 1;
}
if (n = atoi(argv[2]), n == 0) {
fprintf(stderr, "Error or too small number");
return 1;
}
StartWork();
return 0;
}
void StartWork()
{
pid_t pid[3];
int pipe_gr[2], pipe_out[2];
pipe(pipe_gr);
pipe(pipe_out);
if (pid[0] = fork(), pid[0] == 0) {
Process1(pipe_gr[WRITE_CANAL]);
exit(0);
}
if (pid[1] = fork(), pid[1] == 0) {
Process2(pipe_gr[READ_CANAL], pipe_out[WRITE_CANAL]);
exit(0);
}
if (pid[2] = fork(), pid[2] == 0) {
Process3(pipe_out[READ_CANAL]);
exit(0);
}
for(int i = 1; i < 3; i++) {
waitpid(pid[i], NULL, 0);
}
}
void Process1(int pipe1_write)
{
for(int i = 0; i < N; i++) {
double ch, x;
x = (2 * M_PI * i) / N;
ch = x;
write(pipe1_write, (void *)&ch, sizeof(ch));
for(int j = 1; j < n; j++) {
ch = -(ch*x*x)/((2*j)*(2*j+1)); // x * (x^2/(n(n+1)))
write(pipe1_write, (void *)&ch, sizeof(ch));
}
}
}
void Process2(int pipe1_read, int pipe2_write)
{
double sum, curmemb;
for(int i = 0; i < N; i++) {
sum = 0;
for(int j = 0; j < n; j++) {
read(pipe1_read, &curmemb, sizeof(curmemb));
sum += curmemb;
}
write(pipe2_write, (void *)&sum, sizeof(sum));
}
}
void Process3(int pipe2_read)
{
FILE *f_out;
double membsum;
char *msg = "Result of sin";
f_out = fopen(FileOutput, "w+");
for(int i = 0; i < N; i++) {
read(pipe2_read, &membsum, sizeof(membsum));
fprintf(f_out, "%s[%d] - %f\n", msg, i, membsum);
}
fclose(f_out);
}