Questions: Shared Memory Inter-Process Communication
5 questions to test your understanding
Score: 0 / 5
Question 1 Multiple Choice
Process A writes a 1000-element array into shared memory and then signals Process B to start reading. Without any additional synchronization, what is most likely to happen?
AProcess B will read the array correctly because memory writes are always completed before signaling
BProcess B may observe a partially-written or stale array due to CPU/compiler reordering and cache effects — explicit synchronization is required
CThe operating system automatically serializes shared-memory access when a signal is sent
DProcess B will block until Process A has finished all writes, since shared memory has built-in ordering guarantees
Shared memory provides no synchronization — the OS sets up the mapping and then steps back entirely. Without memory barriers or a synchronization primitive (semaphore, mutex), the CPU and compiler are free to reorder operations, and Process B has no guarantee about the state of memory it observes. Options C and D describe guarantees that shared memory does NOT provide. Explicit synchronization (a semaphore to signal completion, a mutex to protect the region) is required to ensure Process B only reads after Process A has fully committed the write.
Question 2 Multiple Choice
Why is shared memory faster than pipe-based or message-queue IPC for large, high-frequency data transfers?
AShared memory uses special hardware-level memory that is physically faster than normal RAM
BShared memory eliminates kernel copies — both processes access the same physical memory through their virtual address spaces, with no data copied through the kernel
CShared memory bypasses the CPU cache, so reads always see the most current value without stale-cache delays
DThe OS schedules shared-memory processes at higher priority to minimize inter-process wait time
The speed advantage of shared memory is purely about eliminating copies. With pipes or message queues, data travels through the kernel: one copy from sender to kernel buffer, one copy from kernel buffer to receiver — two copies per message. With shared memory, processes read and write the same physical RAM frames through their respective virtual address spaces. Zero copies occur. For video frames, audio buffers, or scientific simulation data, this difference is significant.
Question 3 True / False
Shared memory provides automatic mutual exclusion — the OS ensures that mainly one process can write to the shared region at a time.
TTrue
FFalse
Answer: False
This is the critical misconception about shared memory. The OS maps the same physical memory into multiple processes' address spaces and then provides no further coordination. If two processes write concurrently without synchronization, data races and corruption occur. The programmer must layer synchronization primitives (semaphores, mutexes placed in the shared region, or condition variables) on top. Unlike pipes or message queues, where the kernel serializes access internally, shared memory is completely unsynchronized by default.
Question 4 True / False
A bug in one process that corrupts a shared memory region can damage data visible to all other processes attached to that same region.
TTrue
FFalse
Answer: True
Yes — this is one of the key risks of shared memory. Because all attached processes share the same physical memory, a misbehaving process that writes out-of-bounds, writes garbage, or corrupts a shared data structure immediately affects every other process reading that region. This is fundamentally different from message-passing IPC (pipes, sockets), where each process's memory is isolated and a bug in one process cannot directly corrupt another's memory. Defensive practices (checksums, version fields, strict lock discipline) are essential in production shared-memory systems.
Question 5 Short Answer
In a producer-consumer ring buffer implemented in shared memory, why must the producer and consumer use semaphores to track the number of full and empty slots, even when only one process reads and one writes?
Think about your answer, then reveal below.
Model answer: Without semaphores, the producer has no way to know when slots are available to write into (and could overwrite unread data), and the consumer has no way to know when slots contain valid data to read (and could read uninitialized slots). The 'full' semaphore prevents the consumer from reading ahead of the producer; the 'empty' semaphore prevents the producer from lapping the consumer.
The ring buffer solves the spatial problem (where to write/read), but semaphores solve the temporal problem (when is it safe to proceed). The 'empty' semaphore is initialized to buffer capacity and decremented by the producer before writing — blocking it when the buffer is full. The 'full' semaphore starts at zero and is incremented by the producer after writing, then decremented by the consumer before reading — blocking the consumer when the buffer is empty. Together they enforce the invariant that the producer and consumer never access the same slot simultaneously.