Skip to content

Commit 56738e3

Browse files
authored
Create pipe_execl.c
Modeling the command-line command:  ps -ef | wc  using pipes.
1 parent c4bad51 commit 56738e3

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Diff for: pipe_execl.c

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
5+
enum {READ, WRITE};
6+
7+
int main()
8+
{
9+
int fd[2];
10+
11+
if (pipe(fd) == -1)
12+
{
13+
perror("Pipe");
14+
exit(1);
15+
}
16+
17+
switch (fork())
18+
{
19+
case -1:
20+
perror("Fork");
21+
exit(2);
22+
23+
case 0:
24+
// Child
25+
26+
// The function fileno() examines the argument
27+
// stream and returns its integer descriptor.
28+
dup2(fd[WRITE], fileno(stdout));
29+
close(fd[READ]);
30+
close(fd[WRITE]);
31+
32+
// int execl(const char *path, const char *arg0, ..., const char *argn, (char *)0);
33+
execl("/bin/ps", "ps", "-ef", (char *) 0);
34+
exit(3);
35+
36+
default:
37+
// Parent
38+
39+
dup2(fd[READ], fileno(stdin));
40+
close(fd[READ]);
41+
close(fd[WRITE]);
42+
43+
execl("/usr/bin/wc", "wc", (char *) 0);
44+
exit(4);
45+
}
46+
47+
return 0;
48+
}
49+

0 commit comments

Comments
 (0)