repo.or.cz
/
lcapit-junk-code.git
/
blob
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
log
|
graphiclog1
|
graphiclog2
|
commit
|
commitdiff
|
tree
|
refs
|
edit
|
fork
blame
|
history
|
raw
|
HEAD
Introduce old redir program
[lcapit-junk-code.git]
/
books
/
apue
/
pipe.c
blob
e44f5dd96ab3f6d5905bdce4a290e1de4710043c
1
#include <stdio.h>
2
#include <string.h>
3
#include <unistd.h>
4
#include <stdlib.h>
5
6
int
main
(
int
argc
,
char
*
argv
[])
7
{
8
pid_t pid
;
9
int
err
,
n
,
fd
[
2
];
10
char
line
[
1024
];
11
12
err
=
pipe
(
fd
);
13
if
(
err
) {
14
perror
(
"pipe()"
);
15
exit
(
1
);
16
}
17
18
pid
=
fork
();
19
if
(
pid
<
0
) {
20
perror
(
"fork()"
);
21
exit
(
1
);
22
}
else if
(!
pid
) {
23
/* Child */
24
close
(
fd
[
1
]);
25
memset
(
line
,
'\0'
,
sizeof
(
line
));
26
n
=
read
(
fd
[
0
],
line
,
sizeof
(
line
));
27
write
(
STDOUT_FILENO
,
line
,
n
);
28
}
else
{
29
/* Parent */
30
close
(
fd
[
0
]);
31
write
(
fd
[
1
],
"hello world!
\n
"
,
13
);
32
}
33
34
exit
(
0
);
35
}