-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecpipe.c
71 lines (52 loc) · 1.32 KB
/
execpipe.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
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include "execpipe.h"
int exec_pipe(char **first_command, char **second_command){
__pid_t pid1, pid2;
//fd[0] => reading end
//fd[1] => writing end
int fd[2];
int pip = pipe(fd);
if(pip == -1){
perror("piper error");
exit(EXIT_FAILURE);
}
pid1 = fork();
pid2 = fork();
if(pid1 == -1){
perror("forking first child error");
exit(EXIT_FAILURE);
}
if(pid1 == 0){
//in first child
//close unused ends
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
close(fd[1]);
if(execvp(first_command[0], first_command) == -1){
perror("First pipe command execution error");
exit(EXIT_FAILURE);
}
}
if(pid2 == -1){
perror("forking second child error");
exit(EXIT_FAILURE);
}
if(pid2 == 0){
//in second child
dup2(fd[0], STDIN_FILENO);
close(fd[1]);
close(fd[0]);
if(execvp(second_command[0], second_command) == -1){
perror("Second pipe command execution error");
exit(EXIT_FAILURE);
}
}
close(fd[0]);
close(fd[1]);
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
return 0;
}