Skip to content

diegolonio/ipc-seat-reservation-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IPC Seat Reservation - System V Concurrency Engine

A robust seat reservation system built in C intended for Linux environments. This project demonstrates high-concurrency handling using System V IPC primitives (Shared Memory and Semaphores) combined with a Multi-threaded Server Architecture.

It solves the classic "Booking Race Condition" problem without using TCP/IP sockets, relying instead on direct kernel-level memory sharing for ultra-low latency communication between processes.

Architecture

The system operates on a Hybrid Process/Thread model:

  1. Main Server Process: Manages the global seat map in Shared Memory.
  2. Worker Threads: Spawns a detached pthread for each incoming client connection.
  3. Private Channels: Dynamically allocates a unique Shared Memory segment and Semaphore set for private communication between a specific Client process and its assigned Server Thread.
sequenceDiagram
    participant Client
    participant ServerMain
    participant ServerThread
    participant SharedSeatMap

    Client->>ServerMain: Request Connection (Sem Wait)
    ServerMain->>Client: Send Comm Keys (Shared Mem)
    ServerMain->>ServerThread: Spawn Worker Thread
    loop Session
        Client->>ServerThread: Select Seat X (Private SHM)
        ServerThread->>SharedSeatMap: Check Availability (Sem Lock)
        alt is Available
            SharedSeatMap-->>ServerThread: Lock Success
            ServerThread-->>Client: "Available"
            Client->>ServerThread: "Buy"
            ServerThread->>SharedSeatMap: Mark Occupied
        else is Occupied
            SharedSeatMap-->>ServerThread: Lock Fail
            ServerThread-->>Client: "Occupied"
        end
        ServerThread->>SharedSeatMap: Release Lock
    end
Loading

Technical Highlights

  • IPC Wrapper Library (ipc_utils): A custom abstraction layer over <sys/shm.h> and <sys/sem.h> to simplify memory segment allocation and semaphore operations (P and V operations implemented as down() and up()).

  • Granular Locking: Instead of locking the entire database, each seat has its own binary semaphore. This allows multiple clients to book different seats simultaneously without blocking each other.

  • Dynamic Resource Allocation: The server generates unique file keys ftok on the fly to create ephemeral communication channels for each session.

Compilation & Usage

This project includes a Makefile for automated build.

Prerequisites

  • GCC Compiler
  • Linux Environment (supports System V IPC)
  • Make

Build

make

Execution

  1. Start the Server: The server initializes the shared memory map and semaphores.

    ./servidor
  2. Start Client(s): Open a new terminal for each client. You can run multiple instances to test concurrency.

    ./cliente

Cleaning Up

Since System V IPC resources persist after process termination, use the clean command to remove semaphores and shared memory segments:

make clean

(This triggers ipcrm commands defined in the Makefile).

About

High-concurrency seat reservation system built in C. Implements a multi-threaded server architecture using System V IPC (Shared Memory & Semaphores) to prevent race conditions.

Resources

Stars

Watchers

Forks

Contributors