質問編集履歴
1
ソースコードの変更後の掲載
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,6 +14,46 @@
|
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
+
######変更後
|
18
|
+
|
19
|
+
moreコマンドが終了するようになりましたが,
|
20
|
+
|
21
|
+
```terminal
|
22
|
+
|
23
|
+
#ps -A | more
|
24
|
+
|
25
|
+
11223 ? 00:00:00 kworker/4:3
|
26
|
+
|
27
|
+
11224 ? 00:00:00 kworker/2:0
|
28
|
+
|
29
|
+
11265 pts/9 00:00:00 ps
|
30
|
+
|
31
|
+
11266 pts/9 00:00:00 more
|
32
|
+
|
33
|
+
```
|
34
|
+
|
35
|
+
```terminal
|
36
|
+
|
37
|
+
#./a.out
|
38
|
+
|
39
|
+
11392 ? 00:00:00 kworker/2:2
|
40
|
+
|
41
|
+
11422 ? 00:00:00 kworker/3:0
|
42
|
+
|
43
|
+
11454 pts/9 00:00:00 a.out
|
44
|
+
|
45
|
+
11455 pts/9 00:00:00 ps
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
となり,ps -A | more と全く同じ動きにはなりませんでした.
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
17
57
|
### 該当のソースコード
|
18
58
|
|
19
59
|
|
@@ -146,6 +186,134 @@
|
|
146
186
|
|
147
187
|
```
|
148
188
|
|
189
|
+
変更後
|
190
|
+
|
191
|
+
```c
|
192
|
+
|
193
|
+
#include <sys/types.h>
|
194
|
+
|
195
|
+
#include <stdio.h>
|
196
|
+
|
197
|
+
#include <stdlib.h>
|
198
|
+
|
199
|
+
#include <sys/stat.h>
|
200
|
+
|
201
|
+
#include <sys/wait.h>
|
202
|
+
|
203
|
+
#include <fcntl.h>
|
204
|
+
|
205
|
+
#include <unistd.h>
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
#define READ 0
|
210
|
+
|
211
|
+
#define WRITE 1
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
#define STD_in 0
|
216
|
+
|
217
|
+
#define STD_out 1
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
int main(int argc, char *argv[])
|
222
|
+
|
223
|
+
{
|
224
|
+
|
225
|
+
int fd[2];
|
226
|
+
|
227
|
+
pid_t pid1, pid2;
|
228
|
+
|
229
|
+
int status;
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
if(pipe(fd) < 0){
|
234
|
+
|
235
|
+
perror("pipe");
|
236
|
+
|
237
|
+
exit(EXIT_FAILURE);
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
pid1 = fork();
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
if (pid1 == 0)
|
248
|
+
|
249
|
+
{
|
250
|
+
|
251
|
+
dup2(fd[WRITE],STD_out);
|
252
|
+
|
253
|
+
execlp("ps", "ps", "-A", NULL);
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
else if (pid1 > 0)
|
258
|
+
|
259
|
+
{ //親
|
260
|
+
|
261
|
+
wait(&status);
|
262
|
+
|
263
|
+
close(fd[WRITE]);
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
else
|
268
|
+
|
269
|
+
{
|
270
|
+
|
271
|
+
perror("fork");
|
272
|
+
|
273
|
+
exit(EXIT_FAILURE);
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
pid2 = fork();
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
if (pid2 == 0)
|
284
|
+
|
285
|
+
{
|
286
|
+
|
287
|
+
dup2(fd[READ],STD_in);
|
288
|
+
|
289
|
+
execlp("more", "more", NULL, NULL);
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
else if (pid1 > 0)
|
294
|
+
|
295
|
+
{ //親
|
296
|
+
|
297
|
+
wait(&status);
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
else
|
302
|
+
|
303
|
+
{
|
304
|
+
|
305
|
+
perror("fork");
|
306
|
+
|
307
|
+
exit(EXIT_FAILURE);
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
```
|
316
|
+
|
149
317
|
|
150
318
|
|
151
319
|
### 試したこと
|