Process Scheduling Notes
Basic Concepts
Process State
As a process executes, it transitions through different states:
New: The process is being created.
Running: Instructions are being executed.
Waiting: The process is waiting for an event (I/O completion, signal).
Ready: The process is waiting to be assigned to a processor.
Terminated: The process has finished execution.
CPU-I/O Burst Cycle
Process execution is characterized by alternating cycles of CPU execution (CPU burst) and I/O wait (I/O burst).
A process starts with a CPU burst, then performs I/O, followed by another CPU burst, and so on.
The final CPU burst ends with a system request to terminate execution.
CPU Scheduler
When the CPU becomes idle, the operating system selects a process from the ready queue to execute.
The CPU scheduler carries out this selection process.
The ready queue can be implemented using various data structures like FIFO queues, priority queues, trees, or linked lists.
The ready queue typically stores process control blocks (PCBs) of the processes.
Preemptive and Nonpreemptive Scheduling
Preemptive Scheduling
The operating system can interrupt (preempt) a currently running process.
The CPU can be allocated to another process, often based on priority or scheduling criteria.
Preemption occurs when:
A new process arrives with a higher priority.
A time quantum expires (e.g., in Round Robin scheduling).
Requires context switching: saving the state of the preempted process and restoring the state of the new process.
Non-preemptive Scheduling
Once a process is allocated the CPU, it runs to completion or voluntarily yields the CPU.
The OS cannot interrupt the running process unless it completes or enters a waiting state.
Fewer context switches compared to preemptive scheduling.
Dispatcher
The dispatcher gives control of the CPU core to the process selected by the CPU scheduler.
Dispatcher functions include:
Switching context from one process to another.
Switching to user mode.
Jumping to the proper location in the user program to resume execution.
Context switches can be monitored using the
vmstatcommand on Linux systems (e.g.,vmstat 1 3shows system-wide statistics including context switches per second).
Dispatch Latency
Dispatch latency is the time taken by the dispatcher to stop one process and start another.
Scheduling Criteria
CPU utilization: Keep the CPU as busy as possible (Maximize).
Throughput: Number of processes that complete execution per time unit (Maximize).
Turnaround time: The sum of time spent waiting in the ready queue, executing on the CPU, and doing I/O (Minimize).
Waiting time: Amount of time a process has been waiting in the ready queue (Minimize).
Response time: Amount of time it takes from request submission until the first response is produced (Minimize).
Scheduling Algorithms
First-Come, First-Served (FCFS) Scheduling
Processes are executed in the order they arrive in the ready queue.
Non-preemptive algorithm.
Advantages
Simple and easy to implement.
Fair in that processes are handled in order of arrival.
Disadvantages
Convoy Effect: A long CPU burst process arriving first can cause subsequent processes to wait for a long time, increasing the average waiting time.
Not optimal for systems requiring fast response times.
How it works
Processes are placed in the ready queue in order of arrival.
The first process in the queue is assigned the CPU until its CPU burst is complete.
Once the first process finishes, the next process in the queue is executed, and so on.
Example
Processes arrive at time 0 with the following CPU burst times (in milliseconds):
Process | Burst Time |
|---|---|
P1 | 24 |
P2 | 3 |
P3 | 3 |
If processes arrive in the order P1, P2, P3:
Gantt Chart: P1(0-24), P2(24-27), P3(27-30)
Average waiting time: milliseconds.
If processes arrive in the order P2, P3, P1:
Gantt Chart: P2(0-3), P3(3-6), P1(6-30)
Average waiting time: milliseconds.
Shortest-Job-First (SJF) Scheduling
Prioritizes the process with the shortest CPU burst at the time the CPU becomes available.
Non-preemptive algorithm.
If two processes have the same CPU burst time, FCFS is used to break the tie.
Advantages
Optimizes average waiting time.
Efficient in systems where CPU burst times can be predicted.
Disadvantages
Requires prior knowledge of CPU burst times.
Can lead to starvation.
Not suitable for systems requiring fast response times.
How It Works
Processes are placed in the ready queue.
When the CPU becomes available, the process with the shortest CPU burst is selected.
The process runs to completion.
Example
Consider the following set of processes with CPU burst times:
Process | Burst Time |
|---|---|
P1 | 6 |
P2 | 8 |
P3 | 7 |
P4 | 3 |
Gantt chart: P4(0-3), P1(3-9), P3(9-16), P2(16-24)
The average waiting time is milliseconds.
Predicting CPU Burst Time
A prediction method is used to estimate the CPU burst time based on the history of previous CPU bursts.
The next CPU burst is often similar to previous bursts.
An exponential average of previous CPU bursts is calculated.
Let be the actual CPU burst time of the -th execution of a process.
Let be the predicted CPU burst time for the -th execution.
The exponential average formula is: where:
is a weighting factor, with .
: The predicted CPU burst time for the current execution.
The initial can be defined as a constant or as an overall system average.
Expanded Formula
Substituting in the formula yields:
Continuing to expand, we get:
This shows that is a linear combination of all actual CPU burst times from to , plus the initial value , with weights that decrease over time.
Example
Calculating with and :
If , , :
Shortest-Remaining-Time-First (SRTF) Scheduling
If a new process arrives with a remaining time shorter than the remaining time of the running process, the current process is preempted.
Preemptive algorithm.
Advantages
Reduces average waiting time.
Theoretically optimal for minimizing average waiting time if CPU burst times are known.
Disadvantages
Requires prior knowledge of CPU burst times.
Increases system overhead due to context switches.
Can cause starvation.
How it works
At each time step, the OS checks all processes in the ready queue.
The process with the shortest remaining time is selected.
If a new process arrives with a shorter burst time, the current process is preempted.
Example
Consider four processes with burst times and arrival times:
Process | Arrival Time | Burst Time |
|---|---|---|
P1 | 0 | 8 |
P2 | 1 | 4 |
P3 | 2 | 9 |
P4 | 3 | 5 |
The average waiting time is milliseconds.
Round Robin (RR) Scheduling
Ensures fairness among processes.
Each process is assigned the CPU for a fixed time interval (time quantum).
Preemptive algorithm.
Processes are placed in a circular queue.
How it works
Processes are placed in a circular queue.
The first process in the queue is assigned the CPU and runs for one time quantum.
If the process completes within the time quantum, it releases the CPU.
If the process does not complete, it is preempted and placed at the end of the queue.
Advantages
Fairness.
Suitable for time-sharing systems.
Easy to implement and manage.
Disadvantages
Context switch overhead.
High average waiting time if the time quantum is not chosen appropriately.
Performance depends on the time quantum.
Example
Consider the set of processes arriving at time 0 with given burst times:
Process | Burst Time |
|---|---|
P1 | 24 |
P2 | 3 |
P3 | 3 |
With a time quantum of 4 milliseconds, the average waiting time is milliseconds.
Priority Scheduling
Each process is assigned a priority.
The CPU is allocated to the process with the highest priority.
If two processes have the same priority, they are scheduled in FCFS order.
Advantages
Simple to implement.
Prioritizes important processes.
Disadvantages
Starvation.
Not optimal for systems requiring fast response times.
Example
Consider the set of processes arriving at time 0 with burst times and priorities:
Process | Burst Time | Priority |
|---|---|---|
P1 | 10 | 3 |
P2 | 1 | 1 |
P3 | 2 | 4 |
P4 | 1 | 5 |
P5 | 5 | 2 |
The average waitting time is milliseconds.
With a time quantum of 2 milliseconds, the average waiting time is milliseconds.