Back to series
· 15 min read

What is an operating system? What problems does it solve?


1. What Is an Operating System?

We use computers every day and usually interact with a variety of applications:

  • Using a browser to visit web pages.
  • Using Word to edit documents.
  • Using VS Code to write code.
  • Using a media player to watch videos.
  • Using game software to play games.
  • Using Python or Java to run programs.

These applications look very different, but they have one thing in common: they all need to use computer hardware resources.

For example:

  • The browser needs the CPU to execute code.
  • The browser needs memory to store web page data.
  • The browser needs the hard disk to read programs and cache files.
  • The browser needs a network device to send and receive data.
  • The browser needs a graphics card to render pages.

If every application had to manage the CPU, memory, hard disk, keyboard, mouse, and monitor by itself, software development would become extremely complicated.

Operating systems exist to solve these problems.

An operating system (OS) is system software that manages computer hardware resources and the software runtime environment.

It sits between the hardware and applications and is responsible for:

  1. Managing processor resources.
  2. Managing memory resources.
  3. Managing files and storage devices.
  4. Managing input/output devices.
  5. Managing program startup, execution, and exit.
  6. Providing security isolation and permission control.
  7. Providing unified system interfaces for applications.

Common operating systems include:

Operating System Main Use Cases
Windows Personal computers, office work, gaming
macOS Mac computers, development, creative design
Linux Servers, cloud computing, development environments
Android Smartphones, tablets, smart devices
iOS iPhone, mobile devices
Unix Servers, workstations, and enterprise systems

An operating system is not just an ordinary application; it is the foundation of the entire computer software runtime environment.

1.1 Where the Operating System Sits in a Computer

The computer software stack can be understood simply as four layers:

flowchart TD
    A["User"] --> B["Application"]
    B --> C["Operating System"]
    C --> D["Computer Hardware"]

    B1["Browsers, editors, and games"] --> B
    C1["Process, memory, file, and device management"] --> C
    D1["CPU, memory, hard disk, and graphics card"] --> D

    style A fill:#FFF1D6,stroke:#E7C77D,color:#72551E
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style D fill:#EAE4F8,stroke:#B6A4D9,color:#514276
    style B1 fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style C1 fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style D1 fill:#F6FAF7,stroke:#DCE9E0,color:#334155

Note that a real computer system is not always a strict four-layer structure.

For example, an application can enter the kernel through system calls, or use hardware indirectly through system libraries, runtime environments, and drivers. The layers in the diagram are mainly intended to help understand the operating system's basic place and responsibilities.

1.2 An Operating System Is More Than a Graphical Interface

Many people first encounter an operating system through a desktop interface:

  • Windows desktop.
  • macOS Dock.
  • Linux desktop environment.
  • Android phone home screen.

This can easily lead to a misunderstanding:

The operating system is the desktop, windows, and icons we see.

In reality, a graphical interface is just one way an operating system provides interaction.

An operating system also includes many invisible core functions:

  • Process scheduling.
  • Memory allocation.
  • File system.
  • Device drivers.
  • Network protocol stack.
  • Permission management.
  • System calls.
  • Interrupt handling.

For example, a Linux server without a graphical interface can still run a complete operating system.

We can log in to the server via SSH and use the command line to manage processes, files, networks, and services.

This shows that:

A graphical interface is not the essence of an operating system; resource management and support for running programs are the core of an operating system.

2. What Problems Does an Operating System Solve?

Suppose we have a computer without an operating system.

The CPU, memory, hard disk, network card, and monitor are all installed.

Now we want to run a browser. What problems need to be solved?

First, the program needs to be loaded into memory.

Second, the CPU needs to know which instruction to execute.

Then, the program needs to request memory space, access the hard disk, and use network devices.

If multiple programs are running at the same time, their resource contention must also be handled.

What if a program accesses memory that does not belong to it?

All these problems require a unified management mechanism.

Operating systems mainly solve the following seven categories of problems.

2.1 Problem 1: How Can Hardware Resources Be Managed Uniformly?

There are many types of computer hardware, and device interfaces from different vendors may be completely different.

For example:

  • Hard drives from different brands.
  • Graphics cards of different models.
  • Network cards of different specifications.
  • Keyboards and mice of different models.
  • CPUs with different architectures.

If every application directly operated the hardware, it would have to understand the low-level implementation of each device.

This would make software and hardware highly coupled.

The operating system abstracts different hardware into relatively uniform resources through device drivers and a unified system interface.

For example, when an application needs to read a file, it usually does not need to care whether the underlying storage is a SATA SSD, NVMe SSD, or hard disk drive.

It only needs to read data through the file operation interface.

The operating system handles low-level device access.

flowchart TD
    A["Application"] --> B["Unified system interface"]
    B --> C["Operating system kernel"]
    C --> D["Device drivers"]
    D --> E["Different hardware devices"]

    E --> F["NVMe SSD"]
    E --> G["SATA SSD"]
    E --> H["Hard disk drive"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style C fill:#EAE4F8,stroke:#B6A4D9,color:#514276
    style D fill:#FFF1D6,stroke:#E7C77D,color:#72551E
    style E fill:#F1F5F4,stroke:#C7D4CF,color:#475569
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976

This mechanism reduces the difficulty of application development and also improves hardware replaceability.

2.2 Problem 2: How do multiple programs run at the same time?

We often run multiple programs on a computer at the same time:

  • The browser opens multiple web pages.
  • A music player plays music.
  • An editor runs code.
  • A download tool downloads files.
  • Background services keep running.

However, the CPU's computing resources are limited.

With only one CPU core, how can multiple programs appear to run at the same time?

The operating system solves this problem through process management and CPU scheduling.

It can let multiple processes take turns gaining access to the CPU.

For example:

flowchart TD
    A["Multiple running processes"] --> B["OS scheduler"]
    B --> C["CPU executes process A"]
    C --> D["Time slice ends or CPU is voluntarily yielded"]
    D --> E["Save process A's running state"]
    E --> F["Switch to process B"]
    F --> G["CPU executes process B"]
    G --> H["Continue scheduling other processes"]
    H --> B

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style C fill:#EAE4F8,stroke:#B6A4D9,color:#514276
    style D fill:#FFF1D6,stroke:#E7C77D,color:#72551E
    style E fill:#F1F5F4,stroke:#C7D4CF,color:#475569
    style F fill:#EAE4F8,stroke:#B6A4D9,color:#514276
    style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style H fill:#DCEFE5,stroke:#8EBFA5,color:#24563F

This process involves an important concept: Process Scheduling.

The operating system decides based on scheduling policies:

  • Which process gets the CPU.
  • How long a process can run.
  • When a process switch occurs.
  • How priorities are handled.
  • How to balance response speed and overall throughput.

On multi-core CPUs, multiple processes or threads can also truly execute in parallel.

Therefore, it is necessary to distinguish between:

  • Concurrency: Multiple tasks make progress alternately over a period of time.
  • Parallelism: Multiple tasks execute at the same moment using different computing resources.

Through scheduling mechanisms, the operating system enables limited CPU resources to serve multiple tasks.

2.3 Problem 3: How should memory be allocated and protected?

Suppose a browser, editor, and player are running at the same time.

They all need to use memory.

Without unified management, programs could arbitrarily read or overwrite other programs' data.

For example:

Browser uses memory region A
Editor uses memory region B
Player uses memory region C

If the browser accidentally modifies the editor's memory, it may cause the program to crash or data corruption.

The operating system solves these problems through memory management mechanisms.

These mainly include:

  1. Allocate memory for the process.
  2. Reclaim memory that has been freed.
  3. Create a virtual address space.
  4. Map virtual addresses to physical memory.
  5. Control memory access permissions.
  6. Manage page swap-in and swap-out.

Modern operating systems usually provide each process with its own independent virtual address space.

For example:

Browser process:
Virtual address space A

Editor process:
Virtual address space B

Player process:
Virtual address space C

Processes use their own virtual addresses to access memory, and the operating system and the hardware memory management unit (MMU) work together to handle address translation and access protection.

flowchart TD
    A["Browser process"] --> B["Virtual address space A"]
    C["Editor process"] --> D["Virtual address space B"]
    E["Player process"] --> F["Virtual address space C"]

    B --> G["MMU address translation"]
    D --> G
    F --> G

    G --> H["Physical memory"]
    H --> I["Pages and memory regions"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style C fill:#EAE4F8,stroke:#B6A4D9,color:#514276
    style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style E fill:#FFF1D6,stroke:#E7C77D,color:#72551E
    style F fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style G fill:#EAE4F8,stroke:#B6A4D9,color:#514276
    style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style I fill:#F1F5F4,stroke:#C7D4CF,color:#475569

In this way, an ordinary process cannot access the memory of other processes at will.

If a program accesses an address it has no permission to access, the operating system handles the corresponding exception, which usually causes the program to receive an error such as an access violation or a segmentation fault.

This is the important role of memory protection.

2.4 Problem Four: How Should Files Be Organized and Managed?

Computers need to store data over the long term.

For example:

  • Documents.
  • Images.
  • Videos.
  • Programs.
  • Databases.
  • Configuration files.

Without a file system, we might be able to locate data only by its physical location on the hard drive.

That would be very inconvenient for both ordinary users and software developers.

Operating systems provide a unified way of organizing data through file systems.

Common file systems include:

File System Common Use Cases
NTFS Windows system disks
FAT32 USB flash drives, memory cards, and compatibility scenarios
exFAT Removable storage devices
ext4 Linux systems
XFS Linux servers and large-capacity storage
APFS Apple systems such as macOS and iOS

File systems typically provide:

  • File creation.
  • File deletion.
  • File reading.
  • File writing.
  • File renaming.
  • Directory management.
  • File permissions.
  • Disk space management.

For example, opening a file in a program:

with open("hello.txt", "r", encoding="utf-8") as file:
    content = file.read()

print(content)

An application does not need to calculate by itself which physical location on the SSD the data resides in.

It only needs to read the file through the file operation interface.

The operating system and the file system take care of the underlying data location, caching, and device access.

2.5 Problem Five: How Should External Devices Be Used in a Unified Way?

A computer has not only a CPU, memory, and a hard drive, but is also connected to various external devices:

  • Keyboards.
  • Mice.
  • Monitors.
  • Printers.
  • Cameras.
  • Microphones.
  • USB storage devices.
  • Network adapters.

Different devices work in different ways.

For example:

  • A keyboard sends input events through a device interface.
  • A mouse continuously generates movement and button events.
  • A network card receives network packets.
  • A graphics card handles graphical rendering and display output.

The operating system coordinates devices through device drivers, interrupt handling, and the I/O subsystem.

For example, when the user presses a key on the keyboard:

flowchart TD
    A["User presses a key"] --> B["Keyboard controller receives input"]
    B --> C["Generates a hardware interrupt"]
    C --> D["Operating system handles the interrupt"]
    D --> E["Input subsystem processes the event"]
    E --> F["Application receives the key event"]
    F --> G["Updates the UI or performs an action"]

    style A fill:#FFF1D6,stroke:#E7C77D,color:#72551E
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#EAE4F8,stroke:#B6A4D9,color:#514276
    style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style E fill:#F1F5F4,stroke:#C7D4CF,color:#475569
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#DCEFE5,stroke:#8EBFA5,color:#24563F

Actual input event handling also involves components such as device drivers, event queues, the window system, and application frameworks.

Applications usually do not need to interact directly with the keyboard controller; instead, they obtain events through input interfaces provided by the operating system.

2.6 Question 6: How can programs be securely isolated from one another?

A computer may run many pieces of software from different sources at the same time.

Some programs are trustworthy, while others may have vulnerabilities or even be malicious.

If all programs had exactly the same hardware access permissions, it would be difficult to ensure system security.

The operating system solves this problem through permission control and isolation mechanisms.

These mainly include:

  • User identity authentication.
  • File access permissions.
  • Process isolation.
  • Kernel mode and user mode isolation.
  • System call permission checks.
  • Network access control.
  • Security policies and auditing.

For example, in Linux:

ls -l

you can view file permissions:

-rw-r--r-- 1 user user 1024 Dec 1 hello.txt

Here:

  • The file owner can read and write.
  • The owning user group can read.
  • Other users can read.

If system-level configuration needs to be modified, higher privileges are usually required.

For example:

sudo apt update

sudo allows authorized users to execute commands with higher privileges.

Permission control can reduce the risk of ordinary programs accidentally operating on critical system resources.

2.7 Question 7: How can software run on different hardware?

Suppose we have developed a text editor.

Without an operating system, the program might need different low-level code for different hardware:

  • One version of the code adapted to a certain brand of hard drive.
  • One version adapted to another graphics card.
  • One version adapted to different input devices.

The cost of software development would be very high.

Through abstraction layers and standard interfaces, the operating system allows applications to avoid directly depending on all hardware details.

For example, a program accesses files through standard file interfaces:

#include <stdio.h>

int main(void) {
    FILE *fp = fopen("hello.txt", "w");

    if (fp == NULL) {
        perror("fopen");
        return 1;
    }

    fprintf(fp, "Hello, Operating System!\n");

    fclose(fp);

    return 0;
}

The program calls standard library interfaces such as fopen(), fprintf(), and fclose().

At a lower level, these operations ultimately request the operating system to complete file operations through mechanisms such as system calls.

The program does not need to know which physical sector the file is ultimately stored in, nor does it need to directly control the SSD's internal flash memory.

This abstraction allows applications to reuse the same programming logic across different hardware devices.

III. Core Components of an Operating System

An operating system is not a single functional module; it is composed of multiple cooperating components.

The most important part is the kernel.

3.1 What is the kernel?

The kernel is the most core part of the operating system, responsible for managing hardware resources and providing basic services to upper-layer software.

The kernel is usually responsible for:

  • Process and thread management.
  • CPU scheduling.
  • Memory management.
  • Interrupt handling.
  • Device management.
  • File system support.
  • Network protocol stack.
  • System call handling.

The kernel can be understood as the central control center for computer resource management.

Other components of the operating system, such as the command interpreter, desktop environment, system services, and utility programs, can run outside the kernel.

Therefore, the kernel and the operating system are not exactly the same concept.

3.2 Kernel Mode and User Mode

Modern processors usually support different privilege levels.

The operating system uses these privilege levels to isolate ordinary applications from core management code.

The most common concepts are:

User Mode

Ordinary applications usually run in user mode.

For example:

  • Web browsers.
  • Text editors.
  • Python programs.
  • Games.

User-space programs cannot execute all privileged instructions at will, nor can they arbitrarily access memory managed by the kernel.

Kernel Mode

The operating system kernel runs in an execution environment with higher privileges.

It can perform privileged operations and manage critical resources such as memory mappings, device control, and interrupts.

When an application needs operating system services, it usually enters the kernel through a system call to perform the corresponding operation.

flowchart TD
    A["User-space application"] --> B["Initiate a system call"]
    B --> C["System call entry"]
    C --> D["Switch to kernel mode"]
    D --> E["Kernel performs resource management operations"]
    E --> F["Return the system call result"]
    F --> G["Switch back to user mode"]
    G --> H["Application continues execution"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#FFF1D6,stroke:#E7C77D,color:#72551E
    style C fill:#EAE4F8,stroke:#B6A4D9,color:#514276
    style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style E fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style F fill:#EAE4F8,stroke:#B6A4D9,color:#514276
    style G fill:#FFF1D6,stroke:#E7C77D,color:#72551E
    style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976

Note the following:

User mode and kernel mode describe CPU execution privilege levels; they are not two different CPUs, nor are they two completely independent operating systems.

3.3 What Is a System Call?

A system call is an important interface through which an application requests services from the operating system kernel.

For example:

  • Create a process.
  • Read a file.
  • Write to a file.
  • Allocate a memory mapping.
  • Create a network connection.
  • Wait for a process to terminate.

In Linux, you can operate on files through the following system calls:

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main(void) {
    int fd = open("hello.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);

    if (fd == -1) {
        perror("open");
        return 1;
    }

    const char *message = "Hello from system call!\n";

    ssize_t count = write(fd, message, 24);

    if (count == -1) {
        perror("write");
        close(fd);
        return 1;
    }

    close(fd);

    return 0;
}

This program uses:

  • open(): Open or create a file.
  • write(): Write data to a file.
  • close(): Close a file descriptor.

These interfaces correspond to system calls in Linux.

In actual development, applications often use wrapper interfaces provided by the C standard library, language runtimes, or other libraries rather than directly using system call instructions.

For example, Python's open() and Java's file APIs interact with the operating system through their respective runtimes and system libraries.

3.4 What Is a Device Driver?

A device driver is an important link between the operating system and hardware devices.

It is responsible for converting the operating system's generic requests into operations that a device can understand.

For example:

When an application requests to read a file, the file system may need to issue an I/O request to the storage driver.

The storage driver then communicates with the specific hardware through the device controller.

Common drivers include:

  • Graphics card drivers.
  • Network card drivers.
  • Sound card drivers.
  • USB drivers.
  • Storage controller drivers.
  • Printer drivers.

The purpose of drivers is to hide hardware differences so that the operating system can manage different devices in a unified way.

However, not all drivers run in kernel mode. Different operating systems may also use architectures such as user-mode drivers.

3.5 Basic Structure of an Operating System

Overall, a typical operating system can be divided into multiple functional layers:

flowchart TD
    A["User and Applications"] --> B["Application Programming Interfaces and System Libraries"]
    B --> C["System Call Interface"]
    C --> D["Operating System Kernel"]

    D --> E["Process and Thread Management"]
    D --> F["Memory Management"]
    D --> G["File System"]
    D --> H["Network Protocol Stack"]
    D --> I["Device Drivers and I/O"]

    I --> J["Computer Hardware"]
    F --> J
    G --> J
    H --> J
    E --> J

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style C fill:#EAE4F8,stroke:#B6A4D9,color:#514276
    style D fill:#FFF1D6,stroke:#E7C77D,color:#72551E
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style G fill:#EAE4F8,stroke:#B6A4D9,color:#514276
    style H fill:#F1F5F4,stroke:#C7D4CF,color:#475569
    style I fill:#FFF1D6,stroke:#E7C77D,color:#72551E
    style J fill:#F6FAF7,stroke:#DCE9E0,color:#334155

Different operating systems do not have exactly the same internal architecture, but these core responsibilities are universal.

IV. How Does an Operating System Manage Program Execution?

One of the best ways to understand an operating system is to observe a program from startup to exit.

4.1 What Is a Program?

A program is a collection of instructions and data stored on a storage device such as a disk.

For example:

hello.exe
python.exe
chrome.exe

The program itself is usually just a static file.

Only when the program is loaded by the operating system and begins executing does it become a running execution entity.

4.2 What Is a Process?

A process is one running instance of a program and an important unit for resource management and allocation by the operating system.

A process typically includes:

  • Program code.
  • Data area.
  • Heap memory.
  • Stack memory.
  • Virtual address space.
  • Open files.
  • Security permissions.
  • Running state.
  • One or more threads.

For example, when we open two browser windows in succession, multiple processes may be involved behind the scenes, and multiple browser processes may work together to handle different tasks.

Therefore, we cannot simply assume that one program file corresponds to only one process.

4.3 What States Does a Process Have?

A process goes through different states during its lifecycle.

Common states include:

State Meaning
New The process is being created
Ready Has the conditions to run and is waiting for the CPU
Running Is executing on the CPU
Blocked Waiting for an event or resource
Terminated Process execution has ended

A simplified process state transition diagram:

flowchart TD
    A["New"] --> B["Ready"]
    B --> C["Running"]
    C --> D["Blocked"]
    D --> B
    C --> B
    C --> E["Terminated"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style C fill:#EAE4F8,stroke:#B6A4D9,color:#514276
    style D fill:#FFF1D6,stroke:#E7C77D,color:#72551E
    style E fill:#F1F5F4,stroke:#C7D4CF,color:#475569

For example:

  1. The user starts the browser, and the system creates a process.
  2. The browser enters the ready state.
  3. The scheduler allocates CPU time, and the browser starts executing.
  4. The browser waits for network data and enters the blocked state.
  5. The network data arrives, and the browser re-enters the ready state.
  6. The browser obtains the CPU and continues executing.
  7. The user closes the browser, and the related processes gradually exit.

This is the basic process by which an operating system manages program execution.

4.4 What Is a Thread?

A thread is an execution unit within a process and an important object of CPU scheduling.

A process can contain multiple threads.

For example, a browser may have:

  • User interface thread.
  • Network processing thread.
  • Rendering thread.
  • JavaScript execution thread.
  • Background task thread.

Multiple threads within the same process usually share the process's address space and some resources, but each has its own independent execution state, register context, and stack.

In simple terms:

  • A process mainly provides an environment for resource isolation and resource management.
  • Threads are responsible for the actual execution activities.

Multithreading enables a program to handle multiple tasks at the same time.

5. How Does the Operating System Let Multiple Programs Share the CPU?

The CPU is a limited computing resource.

The operating system uses a scheduler to decide which thread gets the CPU.

5.1 Time-Slice Round Robin

In a simplified round-robin time-slicing schedule, each task can use the CPU continuously for only a certain period of time.

For example:

Process A: runs 10ms
Process B: runs 10ms
Process C: runs 10ms
Process A: continues running

The operating system switches tasks quickly, giving multiple programs a chance to execute.

5.2 Context Switching

When the CPU switches from one thread to another, it needs to save the execution state of the current thread and restore the execution state of the next thread.

This process is called a context switch.

It usually involves:

  • CPU registers.
  • Program counter.
  • Stack pointer.
  • Scheduling state.
  • Address-space-related state.

Context switching incurs some overhead, so more threads are not always better.

5.3 Scheduling Policies

Different operating systems adopt different scheduling policies depending on the use case.

For example:

Scheduling Policy Main Characteristics
First Come, First Served Processes tasks in the order they arrive
Shortest Job First Prioritizes tasks with shorter estimated runtimes
Round-Robin Time Slicing Each task takes turns using the CPU
Priority Scheduling Allocates the CPU according to task priority
Multilevel Feedback Queue Dynamically adjusts scheduling queues based on task behavior