-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipe_execl.c
49 lines (37 loc) · 954 Bytes
/
pipe_execl.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
enum {READ, WRITE};
int main()
{
int fd[2];
if (pipe(fd) == -1)
{
perror("Pipe");
exit(1);
}
switch (fork())
{
case -1:
perror("Fork");
exit(2);
case 0:
// Child
// The function fileno() examines the argument
// stream and returns its integer descriptor.
dup2(fd[WRITE], fileno(stdout));
close(fd[READ]);
close(fd[WRITE]);
// int execl(const char *path, const char *arg0, ..., const char *argn, (char *)0);
execl("/bin/ps", "ps", "-ef", (char *) 0);
exit(3);
default:
// Parent
dup2(fd[READ], fileno(stdin));
close(fd[READ]);
close(fd[WRITE]);
execl("/usr/bin/wc", "wc", (char *) 0);
exit(4);
}
return 0;
}