Skip to content

Repository files navigation

IPC Token Ring Implementations in C

A collection of C implementations of the Token Ring algorithm using different Inter-Process Communication (IPC) mechanisms available in Unix/Linux operating systems.

The project demonstrates how multiple processes can communicate and synchronize by passing a token around a logical ring. Each implementation uses a different IPC technique, allowing you to compare their design, communication model, and synchronization behavior.


Features

  • Implementation of the Token Ring process communication model.
  • Demonstrates multiple Unix/Linux IPC mechanisms.
  • Well-structured and easy-to-understand C source code.
  • Useful for learning Operating Systems, Process Synchronization, and IPC concepts.
  • Each implementation follows the same token-passing logic using a different communication method.

IPC Mechanisms Implemented

1. Anonymous Pipes

File: pipes_token_ring.c

Uses anonymous pipes to establish communication between related processes created using fork(). Since pipes are unidirectional, multiple pipes are created to form a circular communication channel.

Concepts Demonstrated

  • pipe()
  • fork()
  • read()
  • write()
  • Parent-child process communication

2. Named Pipes (FIFOs)

File: fifo_token_ring.c

Implements the token ring using named pipes (FIFOs). Unlike anonymous pipes, FIFOs exist as special files in the filesystem and allow unrelated processes to communicate.

Concepts Demonstrated

  • mkfifo()
  • open()
  • read()
  • write()
  • File-based IPC

3. Message Queues

File: message_queue_token_ring.c

Uses message queues for asynchronous message passing between processes. Instead of reading and writing raw bytes, processes exchange structured messages.

Concepts Demonstrated

  • Message Queue creation
  • Sending messages
  • Receiving messages
  • Queue management
  • Asynchronous IPC

4. Shared Memory

File: shared_memory_token_ring.c

Implements token passing using shared memory, allowing all processes to access the same memory region. Synchronization mechanisms are used to ensure safe concurrent access.

Concepts Demonstrated

  • Shared memory creation
  • Shared memory mapping
  • Process synchronization
  • High-speed IPC

Prerequisites

  • Linux or Unix operating system
  • GCC or Clang compiler
  • POSIX-compliant environment

Compilation

Compile the desired implementation using GCC.

Anonymous Pipes

gcc -o pipes_ring pipes_token_ring.c

Named Pipes (FIFO)

gcc -o fifo_ring fifo_token_ring.c

Message Queues

gcc -o mq_ring message_queue_token_ring.c

Shared Memory

gcc -o shm_ring shared_memory_token_ring.c -lrt -lpthread

Note: Depending on your Linux distribution and compiler version, linking with -lrt may not be necessary.


Running

Execute the compiled binary:

./pipes_ring

Similarly,

./fifo_ring
./mq_ring
./shm_ring

Each program creates a logical ring of processes and demonstrates token passing between them.


Repository Structure

.
├── fifo_token_ring.c            # Named Pipe (FIFO) implementation
├── message_queue_token_ring.c   # Message Queue implementation
├── pipes_token_ring.c           # Anonymous Pipe implementation
├── shared_memory_token_ring.c   # Shared Memory implementation
└── README.md

Learning Objectives

This project helps in understanding:

  • Inter-Process Communication (IPC)
  • Process creation using fork()
  • Process synchronization
  • Token Ring communication protocol
  • Unix/Linux system programming
  • Differences between IPC mechanisms
  • Performance and design trade-offs of various IPC methods

IPC Comparison

IPC Mechanism Communication Type Speed Suitable For
Anonymous Pipes Byte Stream Fast Parent-child processes
Named Pipes (FIFO) Byte Stream Moderate Related and unrelated processes
Message Queues Message-Based Moderate Structured asynchronous communication
Shared Memory Shared Address Space Very Fast High-performance process communication

License

This project is licensed under the MIT License.

Feel free to use, modify, and distribute the code for educational and personal projects.

About

Implementation of token ring communication using POSIX IPC mechanisms in C

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages