Pipes


Pipe System Call

Syntax in a C/C++ program:
	#include <unistd.h>
	int pip[2];
	(void) pipe(pip);	
With error checking:
	#include <unistd.h>
	int pip[2];
	int result;
	result = pipe(pip);	
	if (result == -1)
	{
		perror("pipe");
		exit(1);
	}
Programming notes: The buffer size for the pipe is fixed. Once it is full all further writes are blocked. Pipes work on a first in first out basis.

Brief Example


	#include <unistd.h>

	int main()
	{

		int pid, pip[2];
		char instring[20];

		pipe(pip); 

		pid = fork();
		if (pid == 0)           /* child : sends message to parent*/
		{
			/* send 7 characters in the string, including end-of-string */
			write(pip[1], "Hi Mom!", 7);  
		}
		else			/* parent : receives message from child */
		{
			/* read from the pipe */
			read(pip[0], instring, 7);
		}
        }

Full Example:

  • Source code for pipe.cpp ( pipe-cpp.txt)
  • Output for pipe.cpp




    Table of Contents