The Life Cycle of a Pipe

A pipe appears when one program decides to create it. The program that creates a pipe is called the pipe’s server. Other processes, called clients, may connect to the pipe’s other end. The server assumes responsibility for the life of the pipe. Any process may be a server or a client, or both at once on different pipes.

After the pipe is created and another process connects to it, either the client or the server, or sometimes both the client and the server, write into its end. Anything written at one end is read from the other. Reading and writing operations rely on the same commands you use to work with any file: ReadFile and WriteFile. Typically, a process that expects to receive a series of messages creates a new thread to wait for them. The thread repeatedly calls ReadFile and blocks, remaining suspended until each new message arrives.

Eventually, the server decides that the conversation has ended and breaks the connection. To destroy the pipe, the server calls CloseHandle. (The pipe will not actually be destroyed until all handles to it, both the server’s and the client’s, have been closed.) Alternatively, the server may decide to connect the old pipe with a new client.

© 1998 SYBEX Inc. All rights reserved.