To switch the CPU among processes, the operating system must record everything about each one and decide which runs next.
Process Control Block
Each process is represented in the OS by a process control block (PCB), also called a task control block. It contains the process state, the process ID (PID), the program counter, the contents of the CPU registers, CPU-scheduling information such as priority and queue pointers, memory-management information such as base and limit registers or page tables, accounting information such as CPU time used, and I/O status information such as open files and devices. The PCB is the store of everything needed to stop a process and later resume it exactly where it left off.
Definition: Process control block. A data structure kept by the operating system for each process, holding its state, identifier, program counter, registers, scheduling, memory, accounting and I/O information.
Context Switch
When the CPU is switched from one process to another, the OS performs a context switch: it saves the state of the old process in its PCB and loads the saved state of the new process from its PCB. Context-switch time is pure overhead, since no useful work is done during it, and it typically takes a few microseconds. Frequent switching therefore wastes CPU time.
Scheduling Queues
The OS keeps processes in queues. The job queue holds all processes in the system. The ready queue holds processes in memory that are ready to run, usually as a linked list of PCBs. Device queues hold processes waiting for a particular device. A process moves between these queues during its life.
Schedulers
The long-term scheduler (job scheduler) chooses which jobs are admitted from disk into memory, controlling the degree of multiprogramming. It runs rarely and should choose a good mix of I/O-bound and CPU-bound processes. The short-term scheduler (CPU scheduler) chooses which ready process gets the CPU next. It runs very often, every few milliseconds, so it must be fast. The medium-term scheduler swaps processes out of memory to disk and back again, to reduce the load or improve the mix. The dispatcher is the module that gives control of the CPU to the chosen process, and the time it takes is the dispatch latency.
Table 2.2: Types of schedulers
| Scheduler | Selects | Frequency |
|---|---|---|
| Long-term | Jobs to load into memory | Seldom (seconds to minutes) |
| Short-term | Ready process to run | Very often (milliseconds) |
| Medium-term | Processes to swap out and in | Occasionally |
Example 2.2: Overhead of context switching
Problem: A time slice is 4 ms and a context switch takes 0.1 ms. What fraction of CPU time is spent on switching? What if the time slice is 1 ms?
Solution:
- With 4 ms slices: overhead = 0.1 / (4 + 0.1) = 0.0244, about 2.4 percent.
- With 1 ms slices: overhead = 0.1 / (1 + 0.1) = 0.0909, about 9.1 percent.
Result: Smaller time slices give quicker response but waste more CPU time on switching.
#include <stdio.h>
int main(void) {
double cs = 0.1, q[] = {1, 2, 4, 10, 50};
for (int i = 0; i < 5; i++)
printf("slice %5.1f ms: overhead %.1f%%\n",
q[i], cs / (q[i] + cs) * 100);
return 0;
}
Output
slice 1.0 ms: overhead 9.1%
slice 2.0 ms: overhead 4.8%
slice 4.0 ms: overhead 2.4%
slice 10.0 ms: overhead 1.0%
slice 50.0 ms: overhead 0.2%
Common mistakes
- Confusing the short-term scheduler with the dispatcher. The scheduler chooses; the dispatcher switches.
- Thinking that context switching does useful work. It is overhead.
- Forgetting that the long-term scheduler controls the degree of multiprogramming.
Key takeaways
- The PCB holds everything needed to resume a process.
- A context switch saves one PCB and loads another.
- Processes move among the job, ready and device queues.
- Long-term, short-term and medium-term schedulers do different jobs.
Practice questions
- What is a PCB?
- What is a context switch?
- Which scheduler controls the degree of multiprogramming?
Answers to the practice questions
- A data structure holding all the information about a process, such as its state, PID, program counter and registers.
- Saving the state of the running process and loading the state of the next one.
- The long-term scheduler.
Sources
- Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit II
