|
26a4e5b224dfecb5da2dc5f27887c451
|
You’ve probably used interprocess communication, or IPC, at some point or another without even realizing it. That’s because the simplest mechanisms of IPC encompass such rudimentary functions as fork, system, the wait family, and even signal handlers such as SIGUSR1 and SIGCHLD to obtain the exit status code of a child process. In this article, I summarize Chapter 5 of ALP, and present more advanced interprocess communication techniques. As usual, I’ll skip over the material that I’m familiar with and focus more on the topics that are new to me in breadth.
Introduction to IPC
Simply, IPC is the transfer of data among processes. In this article, we discuss five modes of IPC:
- shared memory permits processes to communicate by simply reading or writing a memory location.
- mapped memory is similar to shared memory, except that it is also associated with a file on the file system.
- pipes permit sequential communication from one process to a related process.
- FIFOs are similar to pipes, but the pipe is given a name in the file system.
- sockets support communication, even on different computers.
The type of IPC mechanism that you will need will depend largely on the following criteria: whether you need to restrict communication to related processes, whether a process is write-only or read-only, the number of processes allowed, and whether the processes are synchronized.
Shared Memory
Shared memory allows two or more processes to access the same memory as if they all called malloc and were returned pointers to the same actual memory. Shared memory is fast and does not require a system call or entry into the kernel. However, you must perform your own memory management and synchronization, usually with the use of semaphores (semop). The steps necessary to utilize shared memory are as follows:
- one process allocates the segment (
shmget)
- every other process attaches to the segment, and after finishing its use, detaches. (
shmat)
- at some point, one process must deallocate the segment. (
shmctl)
Shared memory is allocated in integral multiples of the page size. You can debug shared memory with the ipcs and ipcrm commands.
Mapped Memory
Mapped memory permits different processes to communicate using a shared file; it performs an automatic association between a file and the process memory. To map an ordinary file use mmap. A common application for mapped memory is the use of /dev/zero.
Pipes
A pipe is a communication device hat permits unidirectional communication. Data written to the write-end of the pipe is read back from the read-end. Pipes automatically synchronize between processes.
To create a pipe, invoke the pipe function. Frequently, you’ll want to create a child process and set up one end of the pipe as its standard input or standard output. For such a case, use the dup2 call. The use of pipes can also be simplified with the popen and pclose functions.
FIFOs
A first-in, first-out (FIFO) is a pipe that has a name in the file system. FIFOs are also called named pipes. You can create a FIFO programmatically using the mkfifo function, and you can access a pipe just as you would an ordinary file.
Sockets
A socket is a bidirectional communication device that can additionally be used to communicate between processes on different computers. When you create a socket, you specify three parameters: the communication style, the namespace, and the protocol.
A communication style controls how the socket treats transmitted data and specifies the number of communication partners. The communication style determines how packets are handled and addressed:
- Connection styles guarantee delivery of all packets in the order they were sent (
SOCK_STREAM).
- Datagram styles do not guarantee delivery or arrival order. The system guarantees only best effort, so packets may disappear or arrive in a different order than shipping (
SOCK_DGRAM).
The socket namespace specifies how addresses are written. In the local namespace, for example, these are ordinary file names (PF_LOCAL or PF_UNIX). In the Internet namespace, these are IP addresses (PF_INET).
Finally, the protocol specifies how the data is transmitted, such as TCP/IP, AppleTalk, and so on, to name a few.
It is easy to see that the socket is more flexible than the other communication techniques. Moreover, sockets can be accessed such like file descriptors. Sockets can be controlled with the following family of system calls: socket, closes, connect, bind, listen, and accept.