Sharing the Processor Among Processes
Sharing the Processor Among Processes
User Mode, Kernel Mode, and Context Switching
Modern operating systems give us the illusion that many programs are running at the same time, even though a system may have only one CPU (or a few CPUs). This illusion is achieved through CPU sharing, a core idea behind CPU virtualization.
This post explains how the OS accomplishes this using a mechanism called Limited Direct Execution.
1. Why Do We Need to Share the CPU?
In a multitasking system:
Many processes want to run simultaneously
The CPU can execute only one instruction at a time per core
The OS must:
Run each process efficiently
Prevent any one process from taking over the system
Switch between processes quickly
This leads to a fundamental challenge:
How can the OS run programs fast while still keeping control of the CPU?
2. Limited Direct Execution: The Big Idea
The solution is Limited Direct Execution (LDE):
Direct execution:
Programs run directly on the CPU, not through an interpreter → fast executionLimited:
Hardware restrictions ensure programs cannot perform dangerous operations
This balance is achieved using:
User mode and kernel mode
System calls
Timer interrupts
Context switching
3. User Mode and Kernel Mode
🔹 User Mode
Normal applications run in user mode
Restrictions:
Cannot perform I/O directly
Cannot access hardware
Cannot modify memory outside its address space
If a program tries a forbidden operation → trap to OS
👉 Example:
A text editor trying to read a file cannot directly access the disk
🔹 Kernel Mode
The operating system runs in kernel mode
Full privileges:
Access hardware
Manage memory
Perform I/O
Control processes
👉 Only trusted OS code is allowed here
🔑 Why Two Modes?
This separation:
Protects the system from buggy or malicious programs
Ensures security and stability
Keeps the OS in control of resources
4. System Calls: Safe Entry into the Kernel
When a user program needs a privileged service (e.g., file I/O):
The program makes a system call
A special trap instruction executes
The CPU:
Switches to kernel mode
Saves the process state
Jumps to a predefined OS handler
The OS performs the requested operation
Control returns to the program using return-from-trap
📌 Important:
User programs cannot jump to arbitrary kernel code
Only allowed services (via system call numbers) are accessible
5. The Need for Context Switching
Running a process forever would block others.
The OS must periodically stop one process and run another.
This raises an important question:
If a process is running, how does the OS regain control?
6. Timer Interrupts: Taking Back Control
The OS uses a timer interrupt:
A hardware timer is set during boot
Every few milliseconds:
The timer generates an interrupt
The CPU stops the current process
Control transfers to the OS
This ensures:
No process can run forever
The OS can enforce time sharing
This approach is non-cooperative—even misbehaving programs can be stopped
7. Context Switching: Switching Between Processes
Once the OS regains control, it may decide to run a different process.
This requires a context switch.
🔄 What Is a Context Switch?
A context switch is the act of:
Saving the state of the currently running process
Restoring the state of another process
Resuming execution of the new process
What Is Saved?
The process context includes:
Program Counter (PC)
CPU registers
Stack pointer
Kernel stack information
These are stored in the Process Control Block (PCB).
🛠 How Context Switching Happens
Process A is running
Timer interrupt occurs
Hardware saves A’s registers
OS scheduler selects Process B
OS:
Saves A’s context into its PCB
Loads B’s context from its PCB
OS executes return-from-trap
Process B resumes execution
📌 The switch happens so fast that users don’t notice it.
8. Cooperative vs Non-Cooperative Switching
Cooperative (Old Systems)
Process voluntarily gives up CPU
Relies on system calls or yield
Problem: infinite loops can freeze system
Non-Cooperative (Modern Systems)
Uses timer interrupts
OS forcibly regains control
Robust and secure
Modern OSes (Linux, Unix, Windows) use non-cooperative preemption
9. Why Context Switching Is Important
Context switching enables:
Multitasking
Fair CPU sharing
Responsive systems
Protection from runaway processes
However:
It has overhead (saving/restoring registers)
Must be efficient
10. Summary
| Concept | Purpose |
|---|---|
| User mode | Restricts application behavior |
| Kernel mode | Allows OS full control |
| System calls | Safe access to OS services |
| Timer interrupts | Prevent CPU monopolization |
| Context switch | Enables multitasking |
Key Takeaway
The OS shares the CPU by letting programs run directly on hardware—but only under strict rules enforced by hardware and the kernel.
This elegant cooperation between hardware mechanisms and OS design is what makes modern multitasking possible.
Comments
Post a Comment