Skip to main content

Command Palette

Search for a command to run...

Shared memory fastest way of Inter process communication.

Published
2 min read
Shared memory fastest way of Inter process communication.
M

Hi, How are you !! Hope you doing good....

I got introduced to Cloud initially. As I went ahead learning what is cloud and how it works, then got to know a field which is DevOps that makes Cloud model more effective.

So, as I started working & got good experience on AWS. I have been learning the DevOps tool and technologies on how to use it with the Cloud, which will give me good understanding on how Cloud and DevOps go hand in hand to deploy my applications.

Last Blog Review

In the last blog we understood, what is inter-process communication, why inter-process communication. Some real-time examples of it.

Today’s Agenda → Understanding why shared memory process is important

What is Shared memory

Shared memory is an IPC mechanism where multiple processes access a common memory area to exchange data efficiently, requiring synchronization to ensure data consistency.

Advantage of Shared memory

1. It’s very fast, as no need to copy the data

2. It’s efficient for large amount of data

3. It’s ideal for real time scenarios

How Shared memory IPC technique works

  • Create shared memory

    • One process (creator) asks the OS to create a shared memory segment.
  • Attach shared memory

    • Other processes attach to that memory segment.
  • Read / Write data

    • All attached processes can read and write to the same memory.
  • Synchronization

    • Mechanisms like semaphores or mutexes are used to avoid conflicts.
  • Detach & Destroy

    • Processes detach when done.

    • The OS frees the memory when no longer needed.

Real time example where Shared memory solves the problem Stock Trading Platform

  • Problem Without Shared Memory

In a stock trading platform, multiple processes must work together:

  1. Market Data Process

    • Receives live stock prices from the exchange (thousands per second)
  2. Trading Algorithm Process

    • Analyzes prices and decides buy/sell
  3. Display / Dashboard Process

    • Shows live prices to users

Without Shared Memory:

  • Data is sent using files or sockets

  • Prices are copied again and again

  • High latency (delay)

  • Missed trading opportunities 💸

👉 In real-time trading, even milliseconds matter


  1. Solution Using Shared Memory

🔄 Real-Time Flow

1️⃣ Market Data Process (Writer)

  • Receives stock prices from exchange

  • Writes latest price directly into shared memory

2️⃣ Trading Algorithm (Reader)

  • Reads prices instantly from shared memory

  • Executes buy/sell decisions in microseconds

3️⃣ Dashboard Process (Reader)

  • Reads same data

  • Displays real-time charts to users

🔐 Synchronization (Critical Part)

To avoid race conditions:

  • A semaphore ensures only one writer at a time

  • Readers wait while data is being updated

Example:

  • Writer locks memory

  • Updates prices

  • Unlocks memory

  • Readers safely read updated data

Conclusion →

In this blog we understood, what is shared memory technique used by process for communication, what are it’s advantage, and some real time example on how it solves the problem. There is no copying of data, it reduces kernel overhead and provides ultra-low latency.