This repository contains classic mutual exclusion algorithms implemented in C to manage concurrent access to shared resources.
It demonstrates various synchronization techniques essential for building reliable multi-threaded applications.
Algorithm | Description |
---|---|
Race Condition Example | Demonstrates unsafe access to shared resources. |
Dekker's Algorithm (Deadlock-Prone) | Early solution to mutual exclusion (with issues). |
Peterson's Algorithm | Correct two-process mutual exclusion algorithm. |
Improved Peterson's Algorithm | Optimized version of Peterson's algorithm. |
Flag-Based Mutex | Simple mutex using flags. |
Strict Alternation Mutex | Alternating access to critical section. |
When multiple processes or threads attempt to access shared resources at the same time without coordination, it can cause race conditions, deadlocks, and data inconsistency.
These classical algorithms provide fundamental solutions to ensure that only one process accesses the critical section at a time.
- Clone the repository:
git clone https://github.com/chouaibcher/mutual-exclusion-algorithms.git cd mutual-exclusion-algorithms
- Compile and run an algorithm (for example, Peterson's Algorithm):
gcc peterson_algorithm.c -o peterson ./peterson
Each algorithm has different characteristics in terms of fairness, performance, and complexity:
- Race Condition: Fast but incorrect (included for demonstration only)
- Dekker's Algorithm: First correct solution but complex
- Peterson's Algorithm: Elegant two-process solution with good fairness
- Flag-Based Mutex: Simple implementation but potential for starvation
- Strict Alternation: Guarantees fairness but unnecessary blocking
- C Programming Language
- POSIX Threads
- Standard Synchronization Techniques
- GCC compiler
- POSIX threads library (pthread)
- Linux/Unix environment
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request