Technical note

Operating Systems: A Brief Overview (Part 1)

Computers, kernels, memory, processes, and the process control block — part one of a three-part primer on operating systems.

This is part 1 of 3 in a series on operating systems.

Understanding Computers

A computer is any electro-mechanical device that takes input, does some processing on that input, and gives an output. Ranging from an abacus to a MacBook, anything that performs the above tasks, in order, can be termed a computer.

Bus: internal communication in a machine is performed using a bus. It is the main pathway between a CPU and the main memory of the computer. A system bus carries data to and from input/output devices.

Universal Serial Bus (USB) is an industry standard specification for cables, connectors, and protocols for connection, communication, and/or power supply between computers, peripherals, and other computers.

CPU, also known as the brain of the computer, stands for Central Processing Unit. Generally, it's a small silicon chip where millions or billions of transistors are embedded to work in harmony and process data. The CPU only understands machine language. It operates in a Fetch — Decode — Execute manner, where each instruction is fetched, decoded, and executed one by one. The CPU, although very small, has its own memory with no latency, often in the form of registers. Apart from registers, the CPU also holds some amount of cache memory for faster data retrieval.

Memory

There are three major characteristics of memory devices used in computing:

  • Capacity
  • Speed
  • Cost

Registers, being the smallest in terms of capacity, are the fastest in terms of speed. Main memory and external hard drives can hold the largest capacity but are comparatively slow in data retrieval. Although speed and capacity are relative, this is the standard and generalized understanding in the industry. The cost of a memory device is always a trade-off between capacity and speed — a more expensive memory device will be faster and have a larger capacity.

RAM: one can access any point in Random Access Memory in the same amount of time (constant time). RAM is broken down into bytes for easier access. A major disadvantage of RAM is that it always needs a constant supply of power — every time it's disconnected from a power source, it loses all the data stored.

Secondary Storage Devices

Unlike RAM, secondary storage devices do not need a constant supply of power to retain their data.

HDD: a Hard Disk Drive, also called a spinning drive because of its spinning disk. It has multiple disks containing data in magnetic strips. Data is located and retrieved using 3D polar coordinates on the disks. The working of an HDD is somewhat similar to a gramophone. HDD size can be in terabytes, though they're slower compared to SSDs.

SSD: Solid State Drives are a faster yet more complex version of secondary storage devices. Data on an SSD is stored electrically on chips, which makes them more expensive but comparatively faster than HDDs.

Operating System

An operating system, or OS, can be described as a program that controls the execution of application programs and acts as an interface between applications and the computer.

In layman's terms, the main purpose of an OS is to run programs, and its efficiency is judged by the number of programs being processed, the speed of the process, and how well the OS handles errors and interrupts. Let's dive deeper into terms surrounding the OS.

Kernel

A kernel is a core component of any OS, mainly responsible for managing system resources. The kernel also assists applications with performance by acting like a memory management unit, performing process scheduling, among other things.

Batch Multi-programming

In batch multiprogramming, when one program finishes, the next scheduled program runs on the processor. This was popularized in mainframes in older days, unlike today.

Multi-programming

Multi-programming refers to the situation where all programs that need to be executed are loaded into main memory, and the OS acts as a resource allocator, allocating CPU time, memory, and so on. The most popular type of multi-programming in today's environment is time-sharing.

To understand multi-programming, let's first understand processes in brief.

Processes

A program in its running state is often termed a process. It's a way to keep track of multiple instances of a program. A process is loaded into main memory, scheduled, and has access to files and networking connections, among other things.

A process includes code, data, and context, which are stored in sequential memory space. It's created by the operating system to track the state of the running program and the resources assigned to it.

The Five-State Process Model

The five-state process model is fairly self-explanatory once you see the diagram; if you want to learn it in depth, there are many resources online.

Suspension

A process is said to be suspended when it's completely removed from main memory. It's stored on a secondary storage device for future use, while its state is saved as-is. This frees main memory and lets other high-priority processes run first. Suspension and state change are controlled by a medium-term scheduling algorithm. Importantly, the suspended process is not aware of the suspension, and is resumed later without any knowledge of it.

A process may be suspended for a few reasons:

  1. Debugging
  2. Long-term delay
  3. Freeing main memory

The introduction of suspension lets us move from the five-state process model to a seven-state process model, with two additional states: Ready — Suspended, and Blocked — Suspended.

Process Control Block (PCB)

A process control block includes all the information required to run and control a given process. It handles different aspects of programming, including:

  • Memory — uses a memory table, useful for paging and segmentation.
  • Input/Output — uses an I/O table, to make sure I/O devices are accessible when needed.
  • File — uses a file table, checking file permissions and accessibility for required files.
  • Call Stack — uses a primary table; all active functions are tracked using the process image.

A PCB also stores:

  • Process ID — a 16-bit tracking ID (for example, the process ID for init is 1)
  • Parent Process ID — used to track the parent process, how old a process is, and whom to notify when the process completes; these IDs are freed and reused later
  • User ID — tracks the resource permissions given to the process / user
  • Registers — saves state during suspension of a process
  • Stack Pointers
  • Scheduling
  • Linkage — information related to linked processes
  • Inter-Process Communication (IPC)
  • Resources
  • Memory

Kernel Mode vs. User Mode

Kernel Mode: when a process is running in kernel mode, it can execute code in any part of the system.

User Mode: any code running in user mode cannot directly access system hardware, nor run CPU instructions it doesn't have permission for. Code running in user mode cannot access memory outside its own scope.

To differentiate between processes running in kernel mode and user mode, the PCB also stores a Program Status Word (PSW) — a single bit of data that decides whether the process is running in user mode or kernel mode.


This was part 1 of a 3-part series on operating systems. Part 2 covers context switching, threads, mutual exclusion, semaphores, and deadlocks. Part 3 dives into memory management — partitioning strategies, the buddy system, paging, virtual memory, and replacement strategies.

← All writing