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.
- 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.
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.
pipe()fork()read()write()- Parent-child process communication
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.
mkfifo()open()read()write()- File-based IPC
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.
- Message Queue creation
- Sending messages
- Receiving messages
- Queue management
- Asynchronous IPC
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.
- Shared memory creation
- Shared memory mapping
- Process synchronization
- High-speed IPC
- Linux or Unix operating system
- GCC or Clang compiler
- POSIX-compliant environment
Compile the desired implementation using GCC.
gcc -o pipes_ring pipes_token_ring.cgcc -o fifo_ring fifo_token_ring.cgcc -o mq_ring message_queue_token_ring.cgcc -o shm_ring shared_memory_token_ring.c -lrt -lpthreadNote: Depending on your Linux distribution and compiler version, linking with
-lrtmay not be necessary.
Execute the compiled binary:
./pipes_ringSimilarly,
./fifo_ring
./mq_ring
./shm_ringEach program creates a logical ring of processes and demonstrates token passing between them.
.
├── 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
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 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 |
This project is licensed under the MIT License.
Feel free to use, modify, and distribute the code for educational and personal projects.