This C program implements a voting system that demonstrates the classic Readers-Writers synchronization problem using POSIX threads, semaphores, and shared memory on Linux.
Developed as part of the Operating Systems course in the Software Engineering program at FAST NUCES, Karachi.
- ✅ Multiple candidates (up to 10)
- ✅ Supports up to 1000 voters
- ✅ Advanced synchronization using named semaphores
- ✅ Concurrent reading and writing with proper coordination
- ✅ Reader-preference synchronization pattern
- ✅ Comprehensive logging system with timestamps
- ✅ Performance tracking and comparison between modes
- ✅ Prevention of duplicate voting
- ✅ Three operation modes: Manual, Thread, and Process
- POSIX Threads (
pthread) - Named Semaphores (
sem_open,sem_wait,sem_post) - Shared Memory (
shm_open,mmap) - Process Management (
fork,waitpid) - Readers-Writers Synchronization
- Signal Handling (
SIGINT) - File I/O and Performance Tracking
- 🧑🏫 Reader-preference implementation:
- Multiple observers (readers) can view results simultaneously
- Voters (writers) get exclusive access
- 🔒 Uses four semaphores:
mutex: Protects access to reader countwrt: Controls write access to voting dataread_count_sem: Manages reader count accessconsole_sem: Prevents interleaved console output
initialize_resources(): Sets up semaphores and shared memorycleanup_resources(): Releases all system resources
reader_enter(),reader_exit(): Controls observation accesswriter_enter(),writer_exit(): Controls vote casting access
cast_vote(): Records votes with proper synchronizationview_results(): Displays current vote tallies safely
manual_mode(): Interactive CLI for votingthread_mode(): Simulates voting using threadsprocess_mode(): Simulates voting using separate processes
print_performance_comparison(): Analyzes thread vs process efficiency
-
Manual Mode
- Interactive CLI interface
- User manually enters voter and candidate IDs
- View live results at any time
-
Thread-Based Simulation
- Creates multiple threads for voters and observers
- Automatically simulates concurrent voting
- Measures and logs performance metrics
-
Process-Based Simulation
- Uses
fork()to create child processes for voters and observers - Demonstrates IPC via shared memory
- Allows performance comparison with thread mode
- Uses
-
Performance Comparison
- Analyzes efficiency differences between thread and process modes
- Generates detailed performance reports
- Creates timestamped log files for each session
- Records all voting activities with timestamps
- Maintains separate performance tracking for comparison
- Generates detailed performance analysis reports
make make clean # Removes executable and main log
make clear_logs # Removes all vote log files./voting_system