-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeforkdemo.c
43 lines (39 loc) · 1 KB
/
pipeforkdemo.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define CHILD_MESS "I want a cookie\n"
#define PAR_MESS "testing..\n"
#define oops(m, x) {perror(m); exit(x);}
int main()
{
int pipefd[2];
int len;
char buf[BUFSIZ];
int read_len;
if(pipe(pipefd) == -1) {
oops("can`t get a pipe", 1);
}
switch(fork()) {
case -1:
oops("can`t fork", 2);
case 0:
len = strlen(CHILD_MESS);
while(1) {
if( write(pipefd[1], CHILD_MESS, len) != len )
oops("write", 3);
sleep(5);
}
default:
len = strlen(PAR_MESS);
while( 1 ) {
if ( write(pipefd[1], PAR_MESS, len) != len )
oops("write", 4);
sleep(1);
read_len = read(pipefd[0], buf, BUFSIZ);
if( read_len <= 0 )
break;
printf("%s\n", buf);
}
}
}