Skip to content

Commit 19ea82b

Browse files
authored
Added more pipe examples.
1 parent 039621a commit 19ea82b

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

pipe_grep

8.33 KB
Binary file not shown.

pipe_grep.c

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* usage: ./a.out text_pattern input_file output_file
3+
* Executes the command "grep text_pattern < input_file > output_file"
4+
*/
5+
6+
#include <stdio.h>
7+
#include <unistd.h>
8+
#include <fcntl.h>
9+
#include <sys/types.h>
10+
#include <sys/stat.h>
11+
12+
int main(int argc, char **argv)
13+
{
14+
int ifp, ofp;
15+
16+
if (argc == 4)
17+
{
18+
// grep on text_pattern
19+
char *grep_args[] = {"grep", argv[1], NULL};
20+
21+
// open input and output files
22+
ifp = open(argv[2], O_RDONLY);
23+
ofp = open(argv[3], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
24+
25+
// duplicate input file to stdin
26+
dup2(ifp, 0);
27+
28+
// duplicate output file to stdout
29+
dup2(ofp, 1);
30+
31+
// close unused input file descriptor
32+
close(ifp);
33+
34+
// close unused output file descriptor
35+
close(ofp);
36+
37+
// execute grep
38+
execvp("grep", grep_args);
39+
}
40+
else
41+
{
42+
printf("usage: %s text_pattern input_file output_file\n", argv[0]);
43+
}
44+
}

pipe_multi_branch

8.42 KB
Binary file not shown.

pipe_multi_branch.c

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* usage: ./a.out input_file text_pattern
3+
* Executes the command "cat input_file | grep text_pattern | cut -b 1-10".
4+
* Note only minimal error checking is done for simplicity/shortness of code.
5+
*/
6+
7+
#include <stdio.h>
8+
#include <unistd.h>
9+
#include <fcntl.h>
10+
#include <sys/types.h>
11+
#include <sys/stat.h>
12+
#include <wait.h>
13+
14+
int main(int argc, char **argv)
15+
{
16+
int status;
17+
int i;
18+
19+
if (argc == 3)
20+
{
21+
// arguments for commands
22+
char *cat_args[] = {"cat", argv[1], NULL};
23+
char *grep_args[] = {"grep", argv[2], NULL};
24+
char *cut_args[] = {"cut", "-b", "1-10", NULL};
25+
26+
// file descriptors for 2 pipes: fd1 for cat-to-grep, fd2 for grep-to-cut
27+
int fd1[2], fd2[2];
28+
29+
// make pipe for cat to grep
30+
pipe(fd1);
31+
32+
// fd1[0] = read end of cat->grep pipe (read by grep)
33+
// fd1[1] = write end of cat->grep pipe (written by cat)
34+
35+
// make pipe for grep to cut
36+
pipe(fd2);
37+
38+
// fd2[0] = read end of grep->cut pipe (read by cut)
39+
// fd2[1] = write end of grep->cut pipe (written by grep)
40+
41+
// fork the first child (to execute cat)
42+
if (fork() == 0)
43+
{
44+
// duplicate write end of cat->grep pipe to stdout
45+
dup2(fd1[1], 1);
46+
47+
// close both ends of all created fd# pipes (very important!)
48+
close(fd1[0]);
49+
close(fd1[1]);
50+
close(fd2[0]);
51+
close(fd2[1]);
52+
53+
execvp(*cat_args, cat_args);
54+
}
55+
else // parent (assume no error)
56+
{
57+
// fork second child (to execute grep)
58+
if (fork() == 0)
59+
{
60+
// duplicate read end of cat->grep pipe to stdin (of grep)
61+
dup2(fd1[0], 0);
62+
63+
// duplicate write end of grep->cut pipe to stdout (of grep)
64+
dup2(fd2[1], 1);
65+
66+
// close both ends of all created fd# pipes (very important!)
67+
close(fd1[0]);
68+
close(fd1[1]);
69+
close(fd2[0]);
70+
close(fd2[1]);
71+
72+
execvp(*grep_args, grep_args);
73+
}
74+
else // parent (assume no error)
75+
{
76+
// fork third child (to execute cut)
77+
if (fork() == 0)
78+
{
79+
// duplicate read end of grep->cut pipe to stadin (of cut)
80+
dup2(fd2[0], 0);
81+
82+
// close both ends of all created fd# pipes (very important!)
83+
close(fd1[0]);
84+
close(fd1[1]);
85+
close(fd2[0]);
86+
close(fd2[1]);
87+
88+
execvp(*cut_args, cut_args);
89+
}
90+
}
91+
}
92+
93+
// only the parent gets here, close all pipes and wait for 3 children to finish
94+
close(fd1[0]);
95+
close(fd1[1]);
96+
close(fd2[0]);
97+
close(fd2[1]);
98+
99+
for (i = 0; i < 3; i++)
100+
{
101+
wait(&status);
102+
}
103+
}
104+
else
105+
{
106+
printf("usage: %s input_file text_pattern\n", argv[0]);
107+
}
108+
109+
return 0;
110+
}

0 commit comments

Comments
 (0)