11638Srgrimes.\" Copyright (c) 1986, 1993
21638Srgrimes.\"	The Regents of the University of California.  All rights reserved.
31638Srgrimes.\"
41638Srgrimes.\" Redistribution and use in source and binary forms, with or without
51638Srgrimes.\" modification, are permitted provided that the following conditions
61638Srgrimes.\" are met:
71638Srgrimes.\" 1. Redistributions of source code must retain the above copyright
81638Srgrimes.\"    notice, this list of conditions and the following disclaimer.
91638Srgrimes.\" 2. Redistributions in binary form must reproduce the above copyright
101638Srgrimes.\"    notice, this list of conditions and the following disclaimer in the
111638Srgrimes.\"    documentation and/or other materials provided with the distribution.
121638Srgrimes.\" 3. All advertising materials mentioning features or use of this software
131638Srgrimes.\"    must display the following acknowledgement:
141638Srgrimes.\"	This product includes software developed by the University of
151638Srgrimes.\"	California, Berkeley and its contributors.
161638Srgrimes.\" 4. Neither the name of the University nor the names of its contributors
171638Srgrimes.\"    may be used to endorse or promote products derived from this software
181638Srgrimes.\"    without specific prior written permission.
191638Srgrimes.\"
201638Srgrimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211638Srgrimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221638Srgrimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231638Srgrimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241638Srgrimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251638Srgrimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261638Srgrimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271638Srgrimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281638Srgrimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291638Srgrimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301638Srgrimes.\" SUCH DAMAGE.
311638Srgrimes.\"
321638Srgrimes.\"	@(#)pipe.c	8.1 (Berkeley) 6/8/93
331638Srgrimes.\"
341638Srgrimes#include <stdio.h>
351638Srgrimes
361638Srgrimes#define DATA "Bright star, would I were steadfast as thou art . . ."
371638Srgrimes
381638Srgrimes/*
391638Srgrimes * This program creates a pipe, then forks.  The child communicates to the
401638Srgrimes * parent over the pipe. Notice that a pipe is a one-way communications
411638Srgrimes * device.  I can write to the output socket (sockets[1], the second socket
421638Srgrimes * of the array returned by pipe()) and read from the input socket
438875Srgrimes * (sockets[0]), but not vice versa.
441638Srgrimes */
451638Srgrimes
461638Srgrimesmain()
471638Srgrimes{
481638Srgrimes	int sockets[2], child;
491638Srgrimes
501638Srgrimes	/* Create a pipe */
511638Srgrimes	if (pipe(sockets) < 0) {
521638Srgrimes		perror("opening stream socket pair");
531638Srgrimes		exit(10);
541638Srgrimes	}
551638Srgrimes
561638Srgrimes	if ((child = fork()) == -1)
571638Srgrimes		perror("fork");
581638Srgrimes	else if (child) {
591638Srgrimes		char buf[1024];
601638Srgrimes
611638Srgrimes		/* This is still the parent.  It reads the child's message. */
621638Srgrimes		close(sockets[1]);
631638Srgrimes		if (read(sockets[0], buf, 1024) < 0)
641638Srgrimes			perror("reading message");
651638Srgrimes		printf("-->%s\en", buf);
661638Srgrimes		close(sockets[0]);
671638Srgrimes	} else {
681638Srgrimes		/* This is the child.  It writes a message to its parent. */
691638Srgrimes		close(sockets[0]);
701638Srgrimes		if (write(sockets[1], DATA, sizeof(DATA)) < 0)
711638Srgrimes			perror("writing message");
721638Srgrimes		close(sockets[1]);
731638Srgrimes	}
741638Srgrimes}
75