-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrtpipec.c
75 lines (54 loc) · 1.35 KB
/
rtpipec.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
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
/***
------------------------------------------
pipe.c R.Tervo Jan 2013
------------------------------------------
***/
main ()
{
char strA[100], strB[100];
pid_t pidC;
int fd[3];
int result, status;
strcpy(strA, "------------"); // empty string
result = pipe(fd);
if( result == -1 )
{ // error has occured
perror("failed pipe()");
exit(1);
} // end error
printf("FILE DESCRIPTORS: fd[0]=%i, fd[1]=%i \n\n", fd[0], fd[1] );
result = fork();
if( result == -1 )
{ // error has occured
perror("failed fork()");
exit(1);
} // end error
pidC = result; // record result of pipe
if( pidC == 0 ) // identify child process
{
close(fd[0]);
printf("child PID= %i \n", getpid() );
strcpy(strA,"Hello"); // 6 bytes
printf(" child sends [%s]\n", strA );
result = write(fd[1], strA, 6);
if( result == -1 )
{ // error has occured
perror("failed write()");
exit(1);
} // end error
} else
{ close(fd[1]);
printf("parent PID= %i \n", getpid() );
read(fd[0], strA ,6);
printf("parent reads [%s]\n", strA );
result = wait(&status);
printf("wait returns [%i] from pid [%i]\n", status, result);
}
} // end main