Skip to content

A comprehensive web-based and interactive tutorial on System V Inter-Process Communication (IPC) in Linux. Covers semaphores, shared memory, message queues, and synchronization patterns, with a hands-on implementation of the producer-consumer problem.

License

Notifications You must be signed in to change notification settings

Michael-Matta1/sysv-ipc-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

System V IPC Tutorial

A comprehensive web-based and interactive tutorial on System V Inter-Process Communication (IPC) in Linux. Covers semaphores, shared memory, message queues, and synchronization patterns, with a hands-on implementation of the producer-consumer problem. Features clear explanations, code examples, visual diagrams, a fully structured website, and practical exercises to help learners understand and implement System V IPC concepts from scratch.

🌐 Live Tutorial: https://michael-matta1.github.io/sysv-ipc-tutorial/


πŸ“š About This Project

This repository contains a deep-dive tutorial on System V IPC, one of the most fundamental and widely-used mechanisms for inter-process communication in Unix-like systems. Unlike many brief or superficial guides, this tutorial provides:

  • Conceptual foundations β€” Understand why IPC exists and how it solves real problems
  • Complete API coverage β€” Detailed explanations of shmget, shmat, semget, semop, and more
  • Real-world implementation β€” A working producer-consumer system with commodity price streaming
  • Best practices β€” Error handling, signal management, deadlock prevention, and resource cleanup
  • Visual explanations β€” Custom SVG diagrams illustrating memory layout, circular buffers, and semaphore operations

What You'll Build

A multi-process system where:

  • Producers generate simulated commodity prices (GOLD, SILVER, CRUDEOIL) using normal distribution
  • Consumer displays a real-time dashboard with prices, moving averages, and trend indicators
  • Shared memory provides ultra-fast communication between processes
  • Semaphores ensure safe synchronization without race conditions

System Overview


πŸ—‚οΈ Repository Structure

sysv-ipc-tutorial/
β”œβ”€β”€ docs/                 # Complete tutorial website (GitHub Pages root)
β”‚   β”œβ”€β”€ index.html        # Landing page
β”‚   β”œβ”€β”€ concepts.html     # Core IPC concepts
β”‚   β”œβ”€β”€ shared_memory.html
β”‚   β”œβ”€β”€ semaphores.html
β”‚   β”œβ”€β”€ c_essentials.html
β”‚   β”œβ”€β”€ implementation.html
β”‚   β”œβ”€β”€ best_practices.html
β”‚   β”œβ”€β”€ reference.html
β”‚   β”œβ”€β”€ styles.css
β”‚   β”œβ”€β”€ script.js
β”‚   └── images/           # SVG diagrams and visualizations
β”‚       β”œβ”€β”€ hero_ipc_flow.svg
β”‚       β”œβ”€β”€ circular_buffer.svg
β”‚       β”œβ”€β”€ shared_memory_layout.svg
β”‚       └── ...
β”œβ”€β”€ code/                 # Complete C++ source code
β”‚   β”œβ”€β”€ producer.cpp      # Price generator
β”‚   β”œβ”€β”€ consumer.cpp      # Dashboard display
β”‚   └── Makefile
└── README.md             # This file

πŸš€ Quick Start

View the Tutorial

Visit the live website to read the complete tutorial with interactive navigation, syntax highlighting, and visual diagrams.

Run the Code

Prerequisites

  • Linux or Unix-like OS (macOS, WSL on Windows)
  • C++ compiler (g++ or clang++)
  • GNU Make

Build and Run

# Clone the repository
git clone https://github.com/Michael-Matta1/sysv-ipc-tutorial.git
cd sysv-ipc-tutorial/code

# Compile both programs
make

# Terminal 1: Start the consumer (creates IPC resources)
./consumer 8

# Terminal 2: Start a producer
./producer GOLD 2000.0 5.0 500 8

# Terminal 3: Start another producer (optional)
./producer SILVER 25.0 0.5 300 8

# Watch the real-time dashboard!
# Press Ctrl+C in consumer terminal to clean up and exit

Command-Line Arguments

Consumer:

./consumer <buffer_size>
  • buffer_size β€” Number of slots in the circular buffer (e.g., 8)

Producer:

./producer <commodity> <mean_price> <std_dev> <update_interval_ms> <buffer_size>
  • commodity β€” Name (e.g., GOLD, SILVER, CRUDEOIL)
  • mean_price β€” Average price around which values fluctuate
  • std_dev β€” Standard deviation for price variation
  • update_interval_ms β€” Delay between price updates in milliseconds
  • buffer_size β€” Must match consumer's buffer size

Cleanup

If processes crash or are terminated abruptly, IPC resources may persist. Clean them up with:

# View active IPC resources
ipcs -m  # Shared memory
ipcs -s  # Semaphores

# Remove specific resources
ipcrm -m <shmid>  # Remove shared memory by ID
ipcrm -s <semid>  # Remove semaphore by ID

# Quick cleanup script (removes all IPC resources for current user)
ipcs -m | awk 'NR>3 {print $2}' | xargs -n1 ipcrm -m 2>/dev/null
ipcs -s | awk 'NR>3 {print $2}' | xargs -n1 ipcrm -s 2>/dev/null

πŸ“– Tutorial Contents

The tutorial is organized into six comprehensive parts:

  • What is Inter-Process Communication (IPC)?
  • The Producer-Consumer Problem
  • Three-Semaphore Solution
  • Circular Buffers (Ring Buffers)
  • Virtual Memory and Address Spaces
  • shmget() β€” Create or access shared memory
  • shmat() β€” Attach to process address space
  • shmdt() β€” Detach from memory
  • shmctl() β€” Control and remove segments
  • Memory layout patterns with pointer arithmetic
  • P (wait) and V (signal) operations
  • semget() β€” Create semaphore sets
  • semop() β€” Perform atomic operations
  • semctl() β€” Initialize and control semaphores
  • Avoiding deadlocks
  • Pointers and memory management
  • Structs and arrays
  • String functions and argument parsing
  • Time handling and random number generation
  • ANSI escape codes for terminal output
  • Complete data structures
  • Consumer implementation walkthrough
  • Producer implementation walkthrough
  • Makefile explanation
  • Running the system
  • Error checking patterns
  • Signal handling for cleanup
  • Resource management
  • Common pitfalls to avoid
  • Debugging techniques

🎯 Learning Objectives

After completing this tutorial, you will:

  • βœ… Understand the fundamental concepts of inter-process communication
  • βœ… Master the System V IPC API (shmget, shmat, semget, semop, etc.)
  • βœ… Implement the producer-consumer pattern with semaphores
  • βœ… Work with circular buffers and shared memory layouts
  • βœ… Handle synchronization, race conditions, and deadlocks
  • βœ… Write robust IPC code with proper error handling
  • βœ… Debug and manage IPC resources on Unix systems

πŸ› οΈ Technical Details

Technologies Used

  • C/C++ β€” Core implementation language
  • System V IPC API β€” <sys/ipc.h>, <sys/shm.h>, <sys/sem.h>
  • ANSI Escape Codes β€” Terminal-based UI rendering
  • Normal Distribution β€” Realistic price simulation using <random>
  • HTML/CSS/JavaScript β€” Tutorial website
  • Highlight.js β€” Syntax highlighting
  • SVG β€” Custom diagrams and visualizations

Browser Compatibility

The tutorial website supports all modern browsers:

  • Chrome/Edge 90+
  • Firefox 88+
  • Safari 14+
  • Mobile browsers (responsive design)

πŸ“Š Key Features

Tutorial Website

  • πŸ“± Fully responsive β€” Optimized for desktop, tablet, and mobile
  • 🎨 Dark theme β€” Easy on the eyes with syntax-highlighted code
  • πŸ” Table of contents β€” Quick navigation on wide screens
  • β™Ώ Accessible β€” Skip links, ARIA labels, keyboard navigation
  • πŸ“ˆ Visual diagrams β€” Custom SVG illustrations for every concept
  • πŸ’Ύ Copy buttons β€” One-click code copying

Code Implementation

  • πŸš€ Production-quality β€” Error handling, signal management, cleanup
  • πŸ“ Well-commented β€” Every function and algorithm explained
  • 🎯 Educational focus β€” Optimized for learning, not just performance
  • πŸ”„ Real-time updates β€” Smooth dashboard rendering
  • 🎲 Realistic simulation β€” Normal distribution for price generation

πŸ“œ License

This project is released under the MIT License β€” feel free to use it for learning, teaching, or as a reference for your own projects.


πŸ”— Links

About

A comprehensive web-based and interactive tutorial on System V Inter-Process Communication (IPC) in Linux. Covers semaphores, shared memory, message queues, and synchronization patterns, with a hands-on implementation of the producer-consumer problem.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published