Skip to content

Operating System Basics

When you turn on your computer, you aren't just running a web browser or a game. You are running a massive, complex program that manages everything else. This program is the Operating System (OS).

The OS is the government of your computer. It controls the resources (CPU, RAM, Disk), enforces laws (Security, Permissions), and provides services to its citizens (Applications).

The Kernel: The Core

At the heart of every OS (Linux, Windows, macOS) is the Kernel.

The Kernel is the first program loaded when the computer starts, and it stays in memory until the computer shuts down. It has complete control over every piece of hardware.

Key Responsibilities:

  1. Process Management: Deciding which program gets to use the CPU.
  2. Memory Management: Deciding which program gets which part of RAM.
  3. Device Drivers: Talking to keyboards, screens, and Wi-Fi cards.
  4. File Systems: Organizing data on the disk.

User Mode vs. Kernel Mode

To prevent a buggy web browser from crashing the entire computer, modern CPUs run in two different modes:

  1. User Mode (Ring 3): This is where your applications run. It is "restricted." You cannot directly access hardware or memory that belongs to other programs. If you try, the CPU stops you and crashes your program (Segfault).
  2. Kernel Mode (Ring 0): This is where the Kernel runs. It has "unrestricted" access to everything. It can overwrite memory, stop the CPU, or wipe the disk.

System Calls: Asking for Permission

If a User Mode program (like Chrome) needs to save a file (which requires talking to the hard drive), it cannot do it directly. It must ask the Kernel for help.

This request is called a System Call (syscall).

  1. Application: "Hey Kernel, please write this data to file.txt." (Executes a syscall instruction).
  2. CPU: Switches from User Mode to Kernel Mode.
  3. Kernel: Checks permissions. "Does this user own file.txt?"
  4. Kernel: Writes the data to the disk.
  5. Kernel: Returns "Success" to the application.
  6. CPU: Switches back to User Mode.

This context switching is expensive, which is why high-performance software tries to minimize system calls.

Practice Problems

Practice Problem 1: The Crash

If a video game has a bug and tries to access memory that belongs to the Operating System, what happens?

A. The entire computer crashes (Blue Screen of Death). B. The video game crashes, but the OS stays running. C. The OS automatically fixes the bug.

Solution

B. The video game crashes.

Because the game runs in User Mode, the hardware prevents it from touching Kernel memory. The CPU detects the illegal attempt and tells the OS, which then kills the game process to protect the system.

Practice Problem 2: The Role of Drivers

Why do you need to install "Drivers" for a new graphics card?

Solution

The Kernel needs to know how to talk to the specific hardware. A Device Driver is a piece of code that translates the Kernel's generic commands (e.g., "Draw this pixel") into the specific electrical signals understood by that brand of graphics card.

Key Takeaways

Component Role
OS The manager of hardware and software resources.
Kernel The core program that runs in privileged mode.
User Mode Restricted mode for applications (Safe).
Kernel Mode Unrestricted mode for the OS (Powerful/Dangerous).
System Call The bridge between User Mode and Kernel Mode.

The Operating System is the unsung hero of computing. It virtualizes the messy reality of hardware into a clean, consistent interface for programmers. Without it, every program would have to be its own operating system.