Inter-process Communication – Shared-Memory System

Interprocess communication is necessary to share information between two or more processes. This is how a process cooperates with other processes. In this article, you will learn about one of the two popular models of IPC, which is Shared-memory system.

A process is independent if no affecting or be affected by other processes and does not share data with other processes. A process co-operating if it can be affected or affect other processes. It shares data with other processes.

The cooperating process needs an interprocess communication(IPC) mechanism. There are two models for IPC.

1. Shared Memory – A shared region of memory is established for data exchange.
2. Message passing – communication using message exchanges.

Characteristics of Shared-memory and Message Passing

We have listed a few characteristics for both shared memory and message passing:

  • Message passing is used to do smaller messages.
  • Shared memory is faster than message passing due to fewer system calls. The message passing system has to call the kernel for each message.
  • Shared memory has conflicts when both processes try to access a shared file simultaneously.
Shared Memory System
Shared Memory System

Shared-Memory System

The shared-memory system of IPC needs to establish a shared memory region by the communicating processes. Some characteristics of the shared memory region are:

  • Shared memory resides in the address space of the process that creates the shared memory region.
  • Another process must add shared address space to their address space to communicate.
  • OS does allow address space access by another process, processes must remove this restriction before establishing a shared memory region.
  • The processes are responsible for the location, creating, and access to the shared memory reason. OS does not control shared memeory region.

Example: The Producer-Consumer Problem

The producer-consumer problem will help understand the concepts of cooperating processes. A producer process produces information consumed by a consumer process.

For example, compiler code makes assembly code consumed by an assembler. A Web server produces HTML, images, etc., and client computers consume the information through the browser.

The producer-consumer problem can be solved by a shared memory region. We must have a buffer of items that is filled by a producer and the buffer emptied by the consumer. The buffer resides in the shared memory region.
The producer and the consumer must be synchronized. While producer creates an item, the consumer consumes another item and not try to consume something not yet created.

The Buffer

The buffer can be of two types:

  • Unbounded buffer
  • Bounded buffer

Unbounded buffer

  • There is no limit on the size of the unbounded buffer.
  • The consumer waits for a new item, however, there is no restriction on the producer to produce items.

Bounded buffer

  • If the buffer is empty, the consumer must wait for a new item.
  • When the buffer is full, the producer waits until it can produce new items.

Let us see how bounded buffer is implemented. A bounded buffer is implemented using a circular queue of an array.

Sample Code #1

#define BUFFER_SIZE 10

typedef struct {
......
} item;

item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;

Consider the producer and consumer code below. The buffer is empty when in == out. The buffer is full when ((in + 1) % BUFFER_SIZE) == out;

Sample Code #2: Producer


item nextProduced;

while (true) {

/* produce an item in nextProduced */
while(((in + 1)% BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;

Sample Code #3: Consumer


item nextConsumed;

while(true)
{
/* consume an item in nextConsumed */
while(in == out)
; /* do nothing */
buffer[out] = nextConsumed;
out = ( out + 1) % BUFFER_SIZE;
/* consume the item in nextConsumed */
}

The buffer contains BUFFER_SIZE - 1 items and no more.

References

Abraham Silberschatz, Peter B. Galvin, Greg Gagne (July 29, 2008) Operating System Concepts, 8 edn., : Wiley.

Tanenbaum, Andrew S. (March 3, 2001) Modern Operating Systems, 2nd edn., : Prentice Hall.


Skip to content