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 execution

  • Limited:
    Hardware restrictions ensure programs cannot perform dangerous operations

This balance is achieved using:

  1. User mode and kernel mode

  2. System calls

  3. Timer interrupts

  4. 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):

  1. The program makes a system call

  2. A special trap instruction executes

  3. The CPU:

    • Switches to kernel mode

    • Saves the process state

    • Jumps to a predefined OS handler

  4. The OS performs the requested operation

  5. 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:

  1. Saving the state of the currently running process

  2. Restoring the state of another process

  3. 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

  1. Process A is running

  2. Timer interrupt occurs

  3. Hardware saves A’s registers

  4. OS scheduler selects Process B

  5. OS:

    • Saves A’s context into its PCB

    • Loads B’s context from its PCB

  6. OS executes return-from-trap

  7. 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

ConceptPurpose
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

Popular posts from this blog

Operating Systems OS PCCST403 Semester 4 BTech KTU CS 2024 Scheme

Introduction to Operating System -Virtualization, Concurrency, and Persistence

Operating Systems PCCST403 Scheme and Syllabus