-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.c
More file actions
231 lines (197 loc) · 4.87 KB
/
manager.c
File metadata and controls
231 lines (197 loc) · 4.87 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
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
//Maciej Andrearczyk, ma333856
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "err.h"
/*
* Maksymalne ograniczenie na długość nazwy pliku.
*/
#define M 1000
/*
* Maksymalne ograniczenie na długość jednej linii.
*/
#define MAX_LINE_SIZE 1000000
/*
* Adres katalogu z plikami in out.
*/
char data_address[] = "./DATA/";
/*
* Liczba wykonawców.
*/
int N;
/*
* Nazwa pliku wejściowego.
*/
char input_file_name[M];
/*
* Nazwa pliku wyjściowego.
*/
char output_file_name[M];
/*
* Linia z pojedynczym wyrażeniem.
*/
char expr_line[MAX_LINE_SIZE];
/*
* Plik z danymi.
*/
FILE *input_file;
/*
* Plik wynikowy.
*/
FILE *output_file;
/*
* Liczba wyrażeń do obliczenia.
*/
int expr_number;
/*
* Deskryptory odpowiednio "pisze dziecko czyta ojciec"
* oraz "pisze ojciec czyta dziecko".
*/
int pipe_ch_2_p[2], pipe_p_2_ch[2];
/*
* Bufory pomocnicze.
*/
char in_buf[MAX_LINE_SIZE];
char out_buf[MAX_LINE_SIZE];
/*
* Ilość obliczonych do tej pory wyrażeń.
*/
int calculated_exprs;
int is_calculated()
{
int space_number = 0;
for (int i = 0; in_buf[i] != '\0'; i++)
if (in_buf[i] == ' ')
space_number++;
return (space_number == 1) ? 1 : 0;
}
/*
* Manager forkuje się tworzy ścieżkę n elementową korzystając z fork,
* w kazdym dziecku wywoluje exec z executorem.
*/
int main(int argc, char *argv[])
{
if (argc != 4)
fatal("Error, improper number of arguments\n");
setvbuf(stdout, NULL, _IONBF, 0);
N = atoi(argv[1]);
if (pipe(pipe_ch_2_p) == -1)
syserr("Error in pipe\n");
if (pipe(pipe_p_2_ch) == -1)
syserr("Error in pipe\n");
if (close(0) == -1)
syserr("Error, close(0)\n");
if (close(1) == -1)
syserr("Error, close(1)\n");
switch (fork())
{
case -1:
syserr("Error in main fork\n");
case 0:
if (close(pipe_ch_2_p[0]) == -1)
syserr("Error, close pipe_ch_2_p[0]\n");
if (close(pipe_p_2_ch[1]) == -1)
syserr("Error, close pipe_p_2_ch[1]\n");
if (dup(pipe_p_2_ch[0]) == -1)
syserr("Error, dup pipe_p_2_ch[0]\n");
if (close(pipe_p_2_ch[0]) == -1)
syserr("Error, close pipe pipe_p_2_ch[0]\n");
if (dup(pipe_ch_2_p[1]) == -1)
syserr("Error, dup pipe_ch_2_p[1]\n");
if (close(pipe_ch_2_p[1]) == -1)
syserr("Error, close pipe_ch_2_p[1]\n");
int pipe_dsc[2];
N--;
while(N > 0)
{
if (pipe(pipe_dsc) == -1)
syserr("Error, pipe pipe_dsc");
switch(fork())
{
case -1:
syserr("Error in chain fork\n");
case 0:
if (close(pipe_dsc[1]) == -1)
syserr("Error in close pipe_dsc[0]\n");
if (close(0) == -1)
syserr("Error in close stdin\n");
if (dup(pipe_dsc[0]) == -1)
syserr("Error in dup pipe_dsc[1]\n");
if (close(pipe_dsc[0]) == -1)
syserr("Error in close pipe_dsc[1]\n");
N--;
break;
default:
if (close(pipe_dsc[0]) == -1)
syserr("Error in close pipe_dsc[1]\n");
if (close(1) == -1)
syserr("Error in close stdout\n");
if (dup(pipe_dsc[1]) == -1)
syserr("Error in dup pipe_dsc[0]\n");
if (close(pipe_dsc[1]) == -1)
syserr("Error in close pipe_dsc[0]\n");
N = 0;
break;
}
}
execl("./executor", "executor", 0);
default:
if (close(pipe_ch_2_p[1]) == -1)
syserr("Error, close pipe_ch_2_p[1]\n");
if (close(pipe_p_2_ch[0]) == -1)
syserr("Error, close pipe_p_2_ch[0]\n");
if (dup(pipe_ch_2_p[0]) == -1)
syserr("Error, dup pipe_ch_2_p[0]\n");
if (close(pipe_ch_2_p[0]) == -1)
syserr("Error, close pipe_ch_2_p[0]\n");
if (dup(pipe_p_2_ch[1]) == -1)
syserr("Error, dup pipe_p_2_ch[1]\n");
if (close(pipe_p_2_ch[1]) == -1)
syserr("Error, close pipe_p_2_ch[1]\n");
strcpy(input_file_name, data_address);
strcpy(output_file_name, data_address);
strcat(input_file_name, argv[2]);
strcat(output_file_name, argv[3]);
input_file = fopen(input_file_name, "r");
if (input_file == NULL)
syserr("Error while opening input file\n");
output_file = fopen(output_file_name, "w");
if (fgets(in_buf, MAX_LINE_SIZE, input_file) == NULL)
syserr("Empty input file\n");
expr_number = atoi(in_buf);
int expr_counter = 0;
for (int i = 0; i < N && fgets(in_buf, MAX_LINE_SIZE, input_file) != NULL; i++)
{
printf("%d: %s", expr_counter+1, in_buf);
expr_counter++;
}
while(1)
{
fgets(in_buf, MAX_LINE_SIZE, stdin);
if (is_calculated() == 1)
{
calculated_exprs++;
fputs(in_buf, output_file);
fflush(output_file);
if (fgets(in_buf, MAX_LINE_SIZE, input_file) != NULL)
printf("%d: %s", ++expr_counter, in_buf);
}
else
printf("%s", in_buf);
if (calculated_exprs == expr_number)
{
printf("#\n");
break;
}
}
fclose(input_file);
fclose(output_file);
}
if (wait(0) == -1)
syserr("Error in wait\n");
exit(0);
}