A simulation of a multi-CPU scheduling system that dynamically generates processes and schedules them on multiple CPUs while managing deadlines, execution times, and scoring. This project demonstrates concurrent programming in Python using threads, mutex locks, and priority queues.
This project implements a multi-CPU scheduling system where processes are generated randomly and concurrently during runtime. Each process is assigned attributes such as arrival time, execution time, starting deadline, ending deadline, and a randomly generated score (value). The goal of the scheduler is to maximize the total score of the completed processes while ensuring that process deadlines are respected.
The system features:
- Random Process Generation: A dedicated thread (Process Generator) creates processes at varied intervals.
- Dynamic Scheduling: Processes are first placed in an input queue, then moved to a ready queue based on priority and deadline constraints.
- Multi-CPU Execution: Multiple CPU threads concurrently execute processes fetched from the ready queue.
- Deadline & Scoring: Processes that miss their deadlines are penalized, and scores are aggregated from successfully executed processes.
-
Randomized Process Attributes:
- Arrival Time: Time when the process enters the system.
- Execution Time: CPU time required by the process.
- Starting Deadline: Maximum delay allowed for starting execution.
- Ending Deadline: Latest allowable time to complete process execution.
- Value (Score): Random integer between 1 and 100 representing the process's reward.
-
Concurrent Process Generation:
Uses a dedicated thread to generate processes at random intervals and push them into an input queue. -
Priority-based Scheduling:
The scheduler uses a composite priority (combining process value, deadlines, and execution time) to maintain a bounded ready queue. When the queue is full, lower-priority processes can be replaced by higher-priority ones. -
Multi-Threaded CPU Simulation:
Multiple CPU threads fetch processes concurrently from the ready queue, ensuring thread-safety with locks to avoid race conditions. -
Deadline Management:
Processes that expire (due to missed starting deadlines or insufficient time to complete) are dropped and penalized by incrementing the missed process count. -
Score Tracking:
A shared score tracker aggregates the total score for completed processes and counts missed processes.
The system is organized into three main components:
-
Process Generator:
Responsible for generating random processes and feeding them into the input queue. -
Scheduler:
Manages two queues:- Input Queue: Where processes are initially enqueued.
- Ready Queue: A bounded, priority-based queue from which CPU threads fetch processes.
The scheduler also cleans up expired processes and handles queue management.
-
CPU Threads:
Multiple CPU threads act as consumers. They continuously fetch processes from the scheduler’s ready queue and simulate their execution. They enforce deadline constraints and update the score tracker based on process success or failure.
-
main.py
Entry point of the application. Initializes the process generator, scheduler, CPUs, and the score tracker. -
process.py
Contains theProcessandProcessHybridclasses with process attributes, deadline checks, and custom ordering based on composite priority. -
generator.py
Implements theProcessGeneratorclass that produces processes with random attributes and pushes them to the input queue. -
scheduler.py
Implements theSchedulerclass. It manages the input and ready queues, performs process prioritization, and cleans up expired processes. -
cpu.py
Contains theCPUclass that simulates CPU threads, executing processes fetched from the scheduler, and updating the score tracker.
-
Process Generation:
TheProcessGeneratorthread continuously generates new processes with random attributes and enqueues them in the input queue until a specified maximum number of processes is reached. -
Scheduling:
TheSchedulerthread monitors the input queue, transfers valid processes to the ready queue, and maintains the ready queue as a min-heap (using Python'sheapq) based on composite priority. Processes that are close to missing their deadlines are dropped, and the scheduler replaces lower-priority processes if the ready queue is full. -
Execution:
Multiple CPU threads run concurrently. Each CPU thread callsget_next_process()on the scheduler to obtain the next process to execute. If a process is expired or cannot finish before its ending deadline, it is marked as missed. Otherwise, the CPU thread simulates the execution by sleeping for the process's execution time and updates the score tracker. -
Score Tracking:
TheScoreTrackerclass safely aggregates scores and counts missed processes across multiple threads using mutex locks.
-
Prerequisites:
- Python 3.x
- Standard libraries (
threading,queue,time,random, etc.)
-
Clone the Repository:
git clone https://github.com/yourusername/multi-cpu-scheduler.git cd multi-cpu-scheduler