Ch 15 Inter-Process Communication
Pipes
Unidirectional, between parent and child processes.
A pipe is created by one process, and after the process calls fork, the pipe can be used between parent and child. It is created using the pipe function, which returns two descriptors—one opened for reading and one for writing.
After fork, we specify which descriptors need to be closed for reading or writing to determine data flow direction.
Writing to a closed pipe generates the SIGPIPE signal.
A pipe is a buffer area in the kernel.
FIFO Named Pipes
Can transmit data between unrelated processes and can be used for non-linear connections.
Use cases:
- Can duplicate output streams
- Communication between client and server processes: use the same FIFO to receive requests and different FIFOs to respond to corresponding processes
XSI IPC
Includes message queues, semaphores, and shared memory.
Each IPC structure in the kernel can be referenced using a non-negative integer. The identifier is the internal name of the IPC object, while an external key value is needed as an external name for association. The key value data type is a long integer, which is finally converted to an identifier by the kernel.
Disadvantages: No reference counting, no names in the file system (cannot be displayed by ls), does not use file descriptors (cannot perform multiplexing).
Message Queues
A linked list of messages stored in the kernel, identified by a message queue identifier (queue ID). Messages can be retrieved in first-in-first-out order, or according to the message’s type field.
Speed is similar to Unix domain sockets and other full-duplex pipes. Considering the above disadvantages, full-duplex pipes should be used as much as possible.
Semaphores and Shared Memory
These are usually used together. Semaphores are used for process synchronization, allowing processes to write data before other processes read it. Shared memory is the fastest because it doesn’t require copying between client and server processes.
XSI IPC cannot be associated with files, whereas mmap can.
POSIX Semaphores
Better performance than XSI, with reference counting.
sem_post +1
sem_wait -1
The semaphore blocks when reduced to 0.