Sunday, October 5, 2008

What is a pipe?

A pipe is a communication buffer defined as 2 file descriptor. One descriptor allows write to the pipe and the other allows one to read from the pipe. A pipe has no external name and so the only way to access it is via the file descriptor. Pipe is also temporary such that they will be deallocated when no process has them open.

A pipe is typically used by a parent process to communicate with its child. It is achieve by creating the pipe before the fork call. The file dscriptors of the pipe will be then be inherited by the child process. Read/write to the pipe is not atomic. In other words, the read could return partial data if the read and write call is in a race condition.

The pipe "|" used in shell (e.g. ls -al | sort -n 4) can be implemented using a combination of pipe and I/O redirection. The pipe-read file descriptor replaces the stdin for one process, and the pipe-write file descriptor replaces the stdout for the second process.

No comments: