100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is Message Passing in Operating Systems?

Learn what message passing is — send/receive, mailboxes, sync vs async — with a C example and OS interview questions answered.

mediumQ97 of 224 in Operating Systems Est. time: 6 minsLast updated:
Open Code Lab

Expected Interview Answer

Message passing is an IPC model where processes exchange discrete, self-contained messages through kernel-mediated send() and receive() primitives, rather than sharing a region of memory directly.

In message passing, the sender constructs a message and hands it to the kernel via send(), and the kernel copies that message into a queue or channel associated with the destination, which the receiver later drains with receive(); the two processes never need a shared address space. This model can be direct, naming the destination process explicitly, or indirect, naming a shared mailbox or queue that decouples sender and receiver identities, and it can be implemented as blocking (synchronous, rendezvous-style) or non-blocking (asynchronous, buffered) communication. Because the kernel copies data on every send and receive, message passing has higher overhead than shared memory, but it avoids the need for explicit synchronization primitives like mutexes since the kernel enforces ordering and atomicity of each message. Message passing is the natural fit for distributed systems and microkernel architectures, where processes may not even share physical memory.

  • No shared address space required, so it works across machines too
  • Kernel enforces message atomicity and ordering, reducing synchronization bugs
  • Naturally supports both direct and indirect (mailbox-based) addressing
  • Foundation for distributed systems and microkernel IPC design

AI Mentor Explanation

Message passing is like a runner physically carrying a written instruction slip from the coach in the dugout to a fielder on the pitch, rather than the fielder walking over to read the coach’s shared notebook directly. Each slip is a complete, self-contained instruction handed off atomically — the fielder cannot get a half-written slip — and the runner (the kernel) is the only one who touches both parties. If the slip goes to a labeled drop box instead of a specific fielder by name, that is indirect addressing through a shared mailbox.

Step-by-Step Explanation

  1. Step 1

    Sender constructs a message

    The sending process builds a self-contained message and calls send(destination, message).

  2. Step 2

    Kernel copies into the channel

    The kernel copies the message into a queue, mailbox, or channel associated with the destination, atomically.

  3. Step 3

    Receiver drains the channel

    The receiving process calls receive(), either blocking until a message arrives or returning immediately if non-blocking.

  4. Step 4

    Delivery semantics apply

    Depending on design, communication is synchronous (rendezvous, both block until handoff) or asynchronous (buffered, sender continues immediately).

What Interviewer Expects

  • Clear contrast between message passing (kernel-copied messages) and shared memory (direct memory access)
  • Understanding of direct vs indirect (mailbox) addressing
  • Understanding of synchronous vs asynchronous message passing
  • Awareness of the overhead tradeoff versus shared memory

Common Mistakes

  • Confusing message passing with shared memory IPC
  • Assuming message passing is always synchronous/blocking
  • Not knowing indirect addressing via a mailbox decouples sender and receiver identity
  • Ignoring the copy overhead that makes message passing slower than shared memory for large data

Best Answer (HR Friendly)

Message passing is when processes talk to each other by sending complete, self-contained messages through the operating system, instead of both reaching into the same block of memory. It is a bit slower than sharing memory directly because the kernel has to copy the data each time, but it is safer to reason about and works even across machines, which is why it is common in distributed systems.

Code Example

Message passing with POSIX message queues
#include <mqueue.h>

/* sender */
mqd_t mq = mq_open("/mychannel", O_CREAT | O_WRONLY, 0644, NULL);
mq_send(mq, "hello via message queue", 25, 0);  /* priority 0 */
mq_close(mq);

/* receiver (separate process) */
mqd_t rq = mq_open("/mychannel", O_RDONLY);
char buf[64];
ssize_t n = mq_receive(rq, buf, sizeof(buf), NULL);  /* blocks until a message arrives */
buf[n] = '\0';
printf("received: %s\n", buf);
mq_close(rq);

Follow-up Questions

  • What is the difference between direct and indirect (mailbox) message addressing?
  • How does synchronous message passing differ from asynchronous message passing?
  • Why is message passing preferred over shared memory in distributed or microkernel systems?
  • How do message queues ensure ordering and what happens if the queue is full?

MCQ Practice

1. In message passing, how do processes exchange data?

Message passing uses kernel primitives to copy self-contained messages between processes, without a shared address space.

2. What is indirect addressing in message passing?

Indirect addressing routes messages through a shared mailbox, decoupling the sender from needing to know the specific receiving process.

3. Why does message passing generally have more overhead than shared memory?

Each send/receive involves the kernel copying data, unlike shared memory where processes read/write the same physical pages directly.

Flash Cards

What is message passing?An IPC model where processes exchange discrete messages via kernel send()/receive() calls, no shared memory needed.

Direct vs indirect addressing?Direct names the destination process; indirect sends through a shared mailbox/queue.

Synchronous vs asynchronous message passing?Synchronous blocks both sides until handoff; asynchronous buffers so the sender continues immediately.

Why is message passing slower than shared memory?The kernel must copy each message between address spaces on every send and receive.

1 / 4

Continue Learning