-
Notifications
You must be signed in to change notification settings - Fork 1
/
main2.c
85 lines (76 loc) · 1.34 KB
/
main2.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
79
80
81
82
83
84
85
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include "get_next_line.h"
void reader(int fd)
{
int ret;
char *line;
if (dup2(fd, 0) < 0)
{
write(2, "DUP2 ERROR\n", strlen("DUP2 ERROR\n"));
return;
};
ret = get_next_line(0, &line);
while (ret > 0)
{
write(1, line, strlen(line));
write(1, "\n", 1);
free(line);
line = 0;
ret = get_next_line(0, &line);
}
if (ret == 0)
{
write(1, line, strlen(line));
write(1, "\n", 1);
free(line);
line = 0;
}
close(fd);
}
void writer(int fd)
{
char buffer[10000];
int i;
int j;
j = 1;
while (j < 100)
{
i = 0;
while (i < 1000)
{
if (i % j == 0)
buffer[i] = '\n';
else
buffer[i] = 'a';
write(fd, buffer, i + 1);
i++;
}
j++;
}
close(fd);
}
int main()
{
int pid;
int stat_loc;
int pip[2];
if (pipe(pip))
{
write(2, "PIPE ERROR\n", strlen("PIPE ERROR\n"));
return (1);
}
pid = fork();
if (pid == 0)
{
close(pip[0]);
writer(pip[1]);
return (0);
}
close(pip[1]);
reader(pip[0]);
wait(&stat_loc);
}