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