Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Inter-process communication

Inter-process communication (IPC) is the set of OS-provided techniques processes use to exchange data and synchronise; engineers use IPC to coordinate components, share results and implement client-server and concurrent designs.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Inter-process communication (IPC) is used whenever two or more processes must exchange data or coordinate actions: for example, a web server process handing off data to worker processes, device drivers signalling user-space helpers, or multiple processes implementing a pipeline. Engineers choose an IPC method to meet performance, security, and complexity constraints.

Key ideas

Start with first principles: a process is an instance of a running program with its own address space and resources. When two processes need to cooperate they need a channel to exchange information and a way to order or synchronise that exchange.

Definitions (bold the first time each is used):

  • Inter-process communication (IPC): the set of OS mechanisms that allow processes to exchange data and synchronise. Assumes processes exist on the same OS instance; sockets can be local (AF_UNIX) or networked (AF_INET/AF_INET6).

  • Message passing: IPC where one process sends discrete messages to another via the kernel (e.g. message queues, sockets). Messages are copied by the kernel between address spaces.

  • Shared memory: IPC where two processes map the same physical memory region into their address spaces. Data is shared without kernel copying, but synchronisation primitives (semaphores, mutexes) are required.

  • Synchronous vs asynchronous communication: In synchronous (blocking) calls, the sender or receiver waits until the communication completes (e.g. blocking read). In asynchronous (non-blocking) calls, the call returns immediately; completion is signalled later (e.g. via poll, select, signals).

  • Rendezvous: a strong form of synchronous message passing where sender and receiver must meet (both block) before transfer completes.

  • Kernel-mediated IPC: the kernel mediates data transfer and enforces protection (e.g., pipes, System V message queues). Assumes kernel copies data and performs permission checks.

  • Zero-copy / data-sharing: shared memory is often described as "zero-copy" because the kernel does not copy message payloads; however, cache coherence and flush costs still exist.

Key properties and trade-offs (state plainly):

  1. Overhead vs latency: kernel-mediated message passing copies data between user spaces so it has higher CPU overhead and latency, but simpler correctness. Shared memory avoids kernel copies and gives higher bandwidth but requires explicit synchronisation and careful memory ordering.

  2. Synchronisation: message passing can provide implicit ordering (messages delivered in FIFO per channel) whereas shared memory requires external synchronisation (semaphores, mutexes, memory barriers).

  3. Persistence and capacity: some IPC objects are persistent (System V IPC objects can survive process exit until removed), others (unnamed pipes) are transient.

What to draw in an exam diagram and label exactly:

  • Pipe between parent and child: draw two process boxes (Parent, Child). Draw kernel in between as a box containing a circular buffer labelled "pipe buffer" with two arrows: one arrow from Child to buffer labelled "write fd" and one arrow from buffer to Parent labelled "read fd". Mark file descriptors (fd[0], fd[1]).

  • Shared memory layout: draw two process boxes with separate virtual-address spaces. Draw a physical page frame between them labelled "shared physical page" and arrows from each process's virtual address to the same physical frame. Label the mapping addresses (e.g. 0x7ff...). Also draw a semaphore object in kernel (or in shared memory) and label it for synchronisation.

Exact statements and assumptions universities expect:

  • Definition (formal): "An IPC mechanism is correct if it ensures data integrity (no lost or corrupted messages) and consistency (ordering required by the abstraction) under the system's concurrency model." State assumptions: processes run concurrently on one machine, OS enforces process isolation, and kernel provides primitives.

  • Producer-consumer with bounded buffer: with a shared-memory buffer you need two conditions—mutual exclusion for head/tail updates and full/empty signalling. With kernel pipes/queues, blocking semantics provide full/empty signalling and the kernel provides per-write/message atomicity (up to PIPE_BUF for pipes), so extra mutual exclusion is usually unnecessary; with shared memory you must provide both using semaphores (and memory barriers as needed).

Formulas

Provide syscall prototypes and small syntax items to remember (each on its own line):

int pipe(int fd[2]) — creates an unnamed pipe; fd[0] for read, fd[1] for write. Returns 0 on success, -1 on error.

int mkfifo(const char *pathname, mode_t mode) — create a named FIFO (special file) in the filesystem.

int msgget(key_t key, int msgflg) — System V message queue create/get.

int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg) — send message to System V queue.

ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg) — receive from System V queue.

int msgctl(int msqid, int cmd, struct msqid_ds *buf) — control/remove System V message queue.

mqd_t mq_open(const char *name, int oflag, mode_t mode, struct mq_attr *attr) — open/create POSIX message queue.

int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio) — send on POSIX message queue.

ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio) — receive from POSIX message queue.

int mq_close(mqd_t mqdes) — close POSIX message queue descriptor.

int mq_unlink(const char *name) — remove POSIX message queue.

int shmget(key_t key, size_t size, int shmflg) — System V shared memory create/get.

void *shmat(int shmid, const void *shmaddr, int shmflg) — attach shared memory segment.

int shmdt(const void *shmaddr) — detach shared memory.

int shmctl(int shmid, int cmd, struct shmid_ds *buf) — control/remove System V shared memory.

int shm_open(const char *name, int oflag, mode_t mode) — open/create POSIX shared memory object.

int shm_unlink(const char *name) — remove POSIX shared memory object.

void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) — map a file/shared memory into address space.

int munmap(void *addr, size_t length) — unmap a mapped region.

int semget(key_t key, int nsems, int semflg) — System V semaphores get/create.

int semop(int semid, struct sembuf *sops, size_t nsops) — perform semaphore operations.

int semctl(int semid, int semnum, int cmd, ...) — control System V semaphore set.

int socket(int domain, int type, int protocol) — create a socket (AF_UNIX or AF_INET).

int socketpair(int domain, int type, int protocol, int sv[2]) — create a connected local socket pair (commonly AF_UNIX).

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) — bind socket.

int listen(int sockfd, int backlog) — mark socket as passive.

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) — accept connection.

int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) — connect to a socket.

ssize_t send(int sockfd, const void *buf, size_t len, int flags) — send on socket.

ssize_t recv(int sockfd, void *buf, size_t len, int flags) — receive on socket.

int kill(pid_t pid, int sig) — send a signal to a process.

typedef void (*sighandler_t)(int) and sighandler_t signal(int signum, sighandler_t handler) — install simple signal handler.

(Each syntax line above: symbol names are from the C POSIX API; check headers like unistd.h, sys/types.h, sys/msg.h, sys/shm.h, sys/sem.h, sys/socket.h, sys/un.h, netinet/in.h, arpa/inet.h, signal.h, sys/stat.h, sys/wait.h, fcntl.h, errno.h, mqueue.h, sys/mman.h.)

Worked examples

  1. Unnamed pipe between parent and child (simple):

Code (compile with: gcc pipe_example.c -o pipe_example):

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h>

int main(void) { int fd[2]; if (pipe(fd) == -1) { perror("pipe"); exit(1); } pid_t pid = fork(); if (pid < 0) { perror("fork"); exit(1); } if (pid == 0) { /* child: write */ close(fd[0]); const char msg = "Hello from child\n"; write(fd[1], msg, strlen(msg)); close(fd[1]); return 0; } else { / parent: read */ close(fd[1]); char buf[100]; ssize_t n = read(fd[0], buf, sizeof(buf)-1); if (n > 0) { buf[n] = '\0'; printf("Parent received: %s", buf); } close(fd[0]); return 0; } }

Sample run (no input required):

./pipe_example

Exact output:

Parent received: Hello from child

  1. System V message queue between forked processes (single program):

Code (compile with: gcc msgq_example.c -o msgq_example):

#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <unistd.h> #include <string.h> #include <sys/wait.h>

struct msgbuf { long mtype; char mtext[100]; };

int main(void) { int msqid = msgget(IPC_PRIVATE, 0600 | IPC_CREAT); if (msqid == -1) { perror("msgget"); exit(1); } pid_t pid = fork(); if (pid < 0) { perror("fork"); exit(1); } if (pid == 0) { /* child sends */ struct msgbuf m; m.mtype = 1; strncpy(m.mtext, "Message from child", sizeof(m.mtext)); if (msgsnd(msqid, &m, strlen(m.mtext)+1, 0) == -1) { perror("msgsnd"); } return 0; } else { wait(NULL); struct msgbuf r; if (msgrcv(msqid, &r, sizeof(r.mtext), 0, 0) == -1) { perror("msgrcv"); } printf("Parent received: %s\n", r.mtext); msgctl(msqid, IPC_RMID, NULL); return 0; } }

Sample run (no input required):

./msgq_example

Exact output:

Parent received: Message from child

  1. Model theoretical question (short model answer): "Compare shared memory and message passing."

Model answer: Define both (as above). Explain performance: shared memory has lower copying cost and higher throughput; message passing has kernel copies and higher CPU overhead. Explain ease-of-use and safety: message passing provides encapsulation and simpler ordering guarantees; shared memory requires explicit synchronisation (semaphores, mutexes) and careful memory ordering (use memory barriers on multi-core). Explain persistence and suitability: message queues or sockets are better for decoupled processes, shared memory is good for large data transfer (multimedia, databases). Conclude with recommendation: choose message passing when correctness and simplicity matter; choose shared memory when throughput and low latency are primary and you can handle synchronisation complexity.

Common mistakes

  • Assuming shared memory provides synchronisation — it does not; students forget to add semaphores and get races.

  • Mixing System V and POSIX APIs without checking headers/semantics (e.g. using msgget with wrong includes).

  • Not handling return values and errno on syscalls; ignoring partial writes/reads on sockets/pipes.

  • Assuming message atomicity beyond system guarantees: on pipes, writes larger than PIPE_BUF may be interleaved or read in parts; on System V/POSIX message queues, oversized messages are rejected (not split).

For your exam

Question types you will see: definitions (define IPC, shared memory, message queue, pipe), short notes (compare two IPC methods), derivations/diagrams (draw pipe or shared-memory mapping and explain), programming/short programs (write a simple pipe or message queue program), and scenario questions (choose the right IPC and justify).

What a full-marks answer includes:

  • Clear definitions of terms used.
  • Correct description of mechanism and its kernel vs user-space behaviour.
  • Diagrams where asked, labelled exactly as described in Key ideas.
  • Correct syscall names and correct order of operations (create, attach/open, use, detach/remove).
  • Discussion of trade-offs (performance, synchronisation, persistence, security).

Quick check

  1. What is the main performance advantage of shared memory over message passing?
  2. Name two kernel-mediated IPC mechanisms in Unix/Linux.
  3. Why must you close the unused end of a pipe after fork?

Answers: 1) Avoids kernel copying of large payloads (higher throughput); 2) Pipes and System V message queues (also sockets, FIFOs, semaphores, shared memory); 3) To prevent blocking/deadlock: an open write-end keeps read from returning EOF and vice-versa, and to free resources.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?