-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.c
72 lines (60 loc) · 1.41 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <strings.h>
#define BUF_SIZE 16384
int main(int argc, char* argv[])
{
pid_t chid;
char buf[100];
int pipefd[2];
if(argc != 2)
{
printf("\nWrong input, two arguments are needed\n");
exit(EXIT_FAILURE);
}
if (pipe(pipefd) == -1)
{
perror("pipe");
exit(EXIT_FAILURE);
}
chid = fork();
if(chid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
if(chid == 0)
{
printf("\n\tHi, I'm a child\n");
close(pipefd[0]);
int fd = open(argv[1], O_RDONLY);
if(fd == -1)
{
perror(argv[1]);
exit(EXIT_FAILURE);
}
char buf[BUF_SIZE] = {};
ssize_t read_bytes = 0;
while((read_bytes = read(fd, buf, BUF_SIZE)) == BUF_SIZE)
{
write(pipefd[1], buf, BUF_SIZE);
}
write(pipefd[1], buf, read_bytes);
}
else
{
printf("\n\tHi, I'm a parent\n");
close(pipefd[1]);
char out_buf[BUF_SIZE] = {};
ssize_t read_bytes = 0;
while((read_bytes = read(pipefd[0], out_buf,
BUF_SIZE) == BUF_SIZE))
{
write(STDOUT_FILENO, out_buf, BUF_SIZE);
}
write(STDOUT_FILENO, out_buf, read_bytes);
}
return 0;
}