21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Solution<br />

Modern operating systems support a variety of interprocess communications primitives<br />

that vary from system to system. If you intend to make your program portable<br />

among different platforms and even different implementations of Unix, your best bet<br />

is to use sockets. All modern operating systems support the Berkeley socket interface<br />

for TCP/IP at a minimum, while most—if not all—Unix implementations also support<br />

Unix domain sockets.<br />

Discussion<br />

Many operating systems support various methods of allowing two or more processes<br />

to communicate with each other. Most systems (including both Unix and Windows)<br />

support anonymous and named pipes. Many Unix systems (including BSD) also support<br />

message queues, which have their origins in AT&T’s System V Unix. Windows<br />

systems have a similar construct known as mailslots. Unix systems also have Unix<br />

domain sockets, which share the Berkeley socket interface with TCP/IP sockets.<br />

Here’s an overview of common mechanisms:<br />

Anonymous pipes<br />

Anonymous pipes are useful for communication between a parent and a child<br />

process. The parent can create the two endpoints of the pipe before spawning<br />

the child process, and the child process will inherit the file descriptors from the<br />

parent. There are ways on both Unix and Windows for two otherwise unrelated<br />

processes to exchange file descriptors, but this is rarely done. On Unix, you can<br />

use Unix Domain sockets. On Windows, you can use the OpenProcess( ) and<br />

DuplicateHandle( ) Win32 API functions.<br />

Named pipes<br />

Instead of using anonymous pipes between unrelated processes, a better solution<br />

may be to use named pipes. With named pipes, a process can create a pipe<br />

that has a name associated with it. Another process that knows the name of the<br />

pipe can then open the pipe. On Unix, named pipes are actually special files created<br />

in the filesystem, and the name used for the pipe is the name of that special<br />

file. Windows uses a special namespace in the kernel and does not actually use<br />

the filesystem at all, although the restrictions on the names given to pipes are<br />

similar to those for files. Pipes work well when there are only two processes<br />

involved, but adding additional processes to the mix quickly complicates matters.<br />

Pipes were not designed for use by more than two processes at a time and<br />

we strongly advise against attempting to use pipes in this way.<br />

Message queues (Unix)<br />

Unix message queues are named with an arbitrary integer value called a key.<br />

Often a file is created, and that file’s inode is used as the key for the message<br />

queue. Any process that has permission to read from the message queue can do<br />

476 | Chapter 9: Networking<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!