IPC in Linux

IPC refers to the mechanisms used for Interprocess Communication. In Linux, IPC comprises the following components.

1. Pipes and FIFOs

Pipes and pipe programming are covered in section 3.10.1 of Chap. 3. A pipe has aread end and a write end. A typical usage of pipe is to connect a pair of pipe writer and read processes. A pipe writer process writes data to a pipe and a reader process reads data form the pipe. Pipe write and read operations are synchronized by the pipe’s control mechanism. Unnamed pipes are for related processes. Named pipes are FIFOs, which can be used by unrelated processes. In Linux, pipe read operation is synchronous and blocking. The reader process waits if the pipe still has writer but no data.

If desired, pipe operations can be changed to nonblocking by the fcntl system call on pipe descriptors.

2. Signals

Processes may use the kill system call to send signals to other processes, which use signal catchers to handle the signals. A major weakness of using signals as IPC is that they can only be used as notifications, which do not contain any information contents.

3. System V IPC

Linux supports System V IPC, which include shared memory, semaphores and message queues. In Linux, System V IPC functions, such as shmat/shmdt for attaching/detaching shared memory, semget/ semop for getting/ operating on semaphores and msgsnd/msgrcv for sending/receiving messages are library wrapper functions, all of which issue the single ipc() system call to the Linux kernel. The implementation of ipc() is Linux specific, which may not be portable.

4. POSIX Message Queues

The POSIX standard (IEEE 1003.1-2001) defines an IPC mechanism based on message queues. They are similar to the System V IPC’s message queues but more general and portable.

5. Threads Synchronization Mechanisms

Linux dose not distinguish between processes and threads. In Linux, processes are threads sharing some common resources. If processes are created with the clone() system call with shared address space, they can communicate by shared memory using mutex and condition variables for synchroni­zation. Alternatively, regular processes can attach to a shared memory, allowing them to be synchronized as threads.

6. Sockets

Sockets are IPC mechanisms for process communication over networks. Sockets and network pro­gramming will be covered in Chap. 13.

Source: Wang K.C. (2018), Systems Programming in Unix/Linux, Springer; 1st ed. 2018 edition.

Leave a Reply

Your email address will not be published. Required fields are marked *