-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.c
78 lines (64 loc) · 1.56 KB
/
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
/*
pipe.c
Jeff Ondich, 15 April 2004
Updated 17 February 2022
Modified by Tanya Amert for Fall 2023
This program creates one child process and hooks the parent
and child up via a pipe, like so:
parent | child
Then the parent execs "ls" and the child execs "wc -l", so
we end up running this pipeline:
ls | wc -l
*/
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main()
{
int fd[2];
// Set up the file descriptors for the pipe
if (pipe(fd) < 0)
{
perror("Trouble creating pipe");
exit(1);
}
printf("Pipe file descriptors: fd[0]=%d, fd[1]=%d\n", fd[0], fd[1]);
// Fork the child process
int pid = fork();
if (pid < 0)
{
perror("Trouble forking");
exit(1);
}
if (pid != 0)
{
/* Parent */
close(fd[0]);
if (dup2(fd[1], STDOUT_FILENO) == -1) // set fd[1] to "out"
{
perror("Trouble redirecting stdout");
}
close(fd[1]);
// Execute "ls"
execlp("ls", "ls", NULL);
perror("execlp in parent failed");
}
else
{
/* Child */
close(fd[1]);
if (dup2(fd[0], STDIN_FILENO) == -1) // set fd[0] to "in"
{
perror("Trouble redirecting stdin");
}
close(fd[0]);
// Execute "wc -l" on the result from the parent
execlp("wc", "wc", "-l", NULL);
perror("execlp in child failed");
}
return 0;
}