Function Signature
Description
Implements a non-preemptive Priority scheduling algorithm. Processes are executed based on their priority values (lower number = higher priority). When a process starts execution, it runs to completion without interruption, even if a higher-priority process arrives during execution.Parameters
Array of arrival times for each process. Each element represents when a process arrives in the system.
Array of CPU burst times for each process. Each element represents how long a process needs to execute.
Array of priority values for each process. Lower values indicate higher priority (e.g., priority 1 > priority 5).
Return Value
Returns an object containing scheduling metrics:Array of start times for each process (indexed by process ID).
Array of completion times for each process (indexed by process ID).
Array of waiting times for each process. Calculated as:
turnaround_time - cpu_time.Array of turnaround times for each process. Calculated as:
completion_time - arrival_time. Represents total time from arrival to completion.Total time required to complete all processes (maximum completion time).
Example Usage
Sample Output
Output Explanation
- Process 0: Priority 2, arrives at 0, starts immediately (only available), completes at 4
- Process 1: Priority 1, arrives at 1, starts at 4 (highest priority), completes at 7
- Process 3: Priority 1, arrives at 3, starts at 7 (same priority as P1, but P1 arrived first), completes at 9
- Process 2: Priority 3, arrives at 2, starts at 9 (lowest priority), completes at 14
Algorithm Details
How It Works
- At each scheduling point, find all processes that have arrived and not completed
- Sort available processes by priority (ascending)
- Select the highest priority process (lowest priority number)
- Execute selected process to completion (non-preemptive)
- If no processes are available, CPU remains idle and time advances
- Calculate metrics: turnaround, waiting, and response times
Complexity
- Time Complexity: O(n²) due to repeated filtering and sorting
- Space Complexity: O(n) for storing process data and results
Characteristics
- Non-preemptive: Once a process starts, it completes without interruption
- Priority-based: Higher priority processes execute first
- Starvation Risk: Low-priority processes may wait indefinitely
- Deterministic: Same inputs always produce same schedule
Tie Breaking Rules
When multiple processes have the same priority:- Arrival time: Process that arrived first gets priority
- Process ID: If arrival times are also equal, lower ID is selected
Preemptive vs Non-preemptive
This implementation is non-preemptive:- ✓ Process runs to completion once started
- ✓ Simpler to implement with less overhead
- ✗ Cannot interrupt for higher priority arrivals
- Would interrupt running process if higher priority arrives
- More responsive but increases context switching overhead