-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_pipe.c
84 lines (67 loc) · 1.56 KB
/
simple_pipe.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "pipe.h"
void do_pipe(char *cmds[MAXCMDS][MAXARGS], int nbcmd, int bg) {
int fd[2],res,status;
if (verbose) {
printf("Entering pipe\n");
}
if (verbose) {
printf("Pipe creation\n");
}
res = pipe(fd);
if (res == -1) {
unix_error("Error in pipe creation");
}
switch(fork()) {
case -1 :
unix_error("fork cmd 1 error");
case 0 :
if (verbose) {
printf("First son process\n");
}
dup2(fd[1],STDOUT_FILENO);
close(fd[0]);
execvp(cmds[0][0],cmds[0]);
printf("execvp error\n");
exit(EXIT_FAILURE);
default :
if (verbose) {
printf("Father process\n");
}
;
}
switch(fork()) {
case -1 :
unix_error("fork cmd 2 error");
case 0 :
if (verbose) {
printf("Second son process\n");
}
close(fd[1]);
dup2(fd[0],STDIN_FILENO);
execvp(cmds[1][0],cmds[1]);
printf("execvp 2 error\n");
exit(EXIT_FAILURE);
default : ;
}
close(fd[0]);
close(fd[1]);
wait(&status);
if (WIFEXITED(status)) {
res = WEXITSTATUS(status);
}
else {
res = -1;
}
wait(&status);
if (WIFEXITED(status)) {
res |= WEXITSTATUS(status);
}
else {
res = -1;
}
return;
}