Technical skill

Linux interprocess communications

Programs need ways to communicate

One program may write a log while another searches it for errors. The two programs need a channel to pass information. A running program is called a process; ways for processes to exchange data or coordinate work are called interprocess communication, or IPC. Some channels pass information in order, some deliver separate messages, and others let programs read and write the same shared area.

A shell can connect two commands with a pipe. Here, journalctl prints selected log lines and grep keeps lines containing a search word:

journalctl -u myapp --since today | grep -i 'failed'

The pipe carries the first command's output into the second command's input. A regular pipe sends a one-way stream of bytes and does not mark where one message ends and another begins, so the programs must agree how to divide the stream.

Choose a channel for the kind of data

A FIFO, also called a named pipe, gives a similar stream a name in the filesystem. Programs can open the same FIFO even when one did not start the other, if its permissions allow it. A Unix domain socket connects programs on the same computer and can support request-and-response messages. Linux can also pass open-file handles, called file descriptors, or credentials such as a program’s identity through a socket. A pathname socket follows filesystem ownership and permission rules, including the permissions on its parent directories.

Shared memory lets several processes see the same memory area. This can avoid copying large amounts of data, but the programs must agree on its layout and coordinate updates. A semaphore is a counter that can help processes coordinate access to shared data. Signals are short notices that an event occurred; they are a poor way to transfer a large amount of information.

System V IPC and POSIX IPC are two interface families with different object types. Their inspection tools do not show every kind of IPC. Use these commands to check three facilities:

ss -xlp
ipcs -q -m -s
ls -l /dev/shm

ss -xlp lists listening Unix domain sockets and requests process details when permissions allow. ipcs reports System V message queues, shared-memory areas, and semaphore arrays. Linux commonly makes POSIX shared-memory names visible under /dev/shm, though the exact view depends on the implementation and mount setup.

Find what is keeping an exchange stuck

If a pipe reader waits, some process may still have the write end open, perhaps because it inherited that open file reference. After all write ends close, a read with no buffered bytes returns end-of-file. A writer waits when the pipe's buffer is full. A socket error can come from the socket path or permissions on a parent directory. Shared-memory processes may disagree about the data layout or be waiting for a lock.

Some IPC objects remain after the program that created them exits. A POSIX shared-memory object stays until it is unlinked or the system shuts down. System V objects have separate removal operations. IPC namespaces isolate some resources, including System V objects, between groups of processes. Check who owns an object and whether any process still uses it before removing it.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful