Process Scheduling
A process is an active-dynamic entity, it changes its state overtime, which a program is a static entity
Context¶
- Contents of all the process' registers
- PC
- instruction (text section)
- Dedicated address space
- stack
- data
- heap
A program becomes a process when instructions from an executable are loaded into memory. Concurrency and protection
Process Scheduling States¶
- New: The process is being created
- Running: The process is running
- Waiting: The process is waiting for some event to occur (I/O)
- Ready: The process is ready to be assigned to a processor to start execution
- Terminated: The process has finished execution
Process Table and Process Control Block¶
The system wide process table is a data struct that is maintained by the kernel to facilitate context switching and scheduling. Each process' metadata is stored by the kernel in a Process Control Block (PCB, aka Task Control Block). A process table is made of an array of PCBs, containing info about the current processes in the system
The PCB contains data such as:
- Process state
- PC
- Regs
- Scheduling Info: priority, pointers to scheduling queues and any other scheduling programs
- Memory Management Info: Page tables, MMU-related info, memory limits
- Accouting Info: CPU and real time used, time limits, account numbers, process id (pid)
- I/O status Info: The list of I/O devices allocated to the process, a list of open files
In the linux kernel, the proctable is a doubly linked list whoes nodes are made up of task_struct
s implemented in C. The kernel maintains a current_pointer
to the process currently running in the CPU
Context Switching¶
When a CPU switches execution between processes, the kernel has to store all the process' metadata (the updated task_struct
) in the corresponding PCB and then load the new process' information from its PCB
Context switch: The mechanism of saving the states of the current process and restoring (loading) the state of a different process when switching the CPU to execute another process.
The advantages of rapid context switching is that it gives the illusion of concurrency in a single processor system
- Improved responsiveness, multiple apps at ocne
- Support for multiprogramming: optimise CPU usage, we cannot allow one single process to hog the CPU specially if the process blocks execution when waiting for I/O calls.
The drawbacks of context switches is pure overhead related to saving and restoring a context. Context switch times are highly dependent on hardware support, some hardware support rapid context switching out of the box, and circumvent the CPU entirely by having a dedicated unit for it.
Mode Switch¶
- The privilege of a process changes. Simply escalates privilege from user mode to kernel mode to access kernel services.
- Done by either: hardware interrupts, system calls (traps, software interrupt), exception, or reset
- Mode switch may not always lead to context switch. Depending on implementation, Kernel code decides whether or not it is necessary.
Process Scheduling Queues¶
There are several scheduling queues that are maintained by the kernel scheduler:
- Job Queue: Set of all processes in the system (may be in swap space or RAM)
- Ready Queue: Set of all processes residing in the main memory which are ready to be executed by the CPU (queuing for CPU time)
- Device Queues: Set of processes that are waiting for some form of I/O from a device. One queue is maintained per device