xref: /freebsd/share/doc/psd/20.ipctut/pipe.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
1 .\" Copyright (c) 1986, 1993
2 .\"	The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"	This product includes software developed by the University of
15 .\"	California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"	@(#)pipe.c	8.1 (Berkeley) 6/8/93
33 .\"
34 #include <stdio.h>
35 
36 #define DATA "Bright star, would I were steadfast as thou art . . ."
37 
38 /*
39  * This program creates a pipe, then forks.  The child communicates to the
40  * parent over the pipe. Notice that a pipe is a one-way communications
41  * device.  I can write to the output socket (sockets[1], the second socket
42  * of the array returned by pipe()) and read from the input socket
43  * (sockets[0]), but not vice versa.
44  */
45 
46 main()
47 {
48 	int sockets[2], child;
49 
50 	/* Create a pipe */
51 	if (pipe(sockets) < 0) {
52 		perror("opening stream socket pair");
53 		exit(10);
54 	}
55 
56 	if ((child = fork()) == -1)
57 		perror("fork");
58 	else if (child) {
59 		char buf[1024];
60 
61 		/* This is still the parent.  It reads the child's message. */
62 		close(sockets[1]);
63 		if (read(sockets[0], buf, 1024) < 0)
64 			perror("reading message");
65 		printf("-->%s\en", buf);
66 		close(sockets[0]);
67 	} else {
68 		/* This is the child.  It writes a message to its parent. */
69 		close(sockets[0]);
70 		if (write(sockets[1], DATA, sizeof(DATA)) < 0)
71 			perror("writing message");
72 		close(sockets[1]);
73 	}
74 }
75