The operating system

The stack

Shell (CLI) compilers, editors, etc. ls, mv, cp, grep, etc. GUI Chrome, VSCode, etc. System libraries System call interface Kernel (Process management, Virtual memory, File system, Networking) Device drivers CPU RAM Other hardware

The top of the stack is where the user interface lives.

CPU

We’ve discussed the CPU at great length.

RAM

We’ve also discussed RAM a fair bit.

Relatively cheap, volatile, transistor-based memory.

Other hardware

Pretty much everything else that’s connected to a computer is in the other hardware category.

Drives, displays, keyboards, mice. Also GPUs.

The CPU only knows about memory and interrupts. Most of that memory is RAM but some of it is actually special memory directly connected to other hardware.

Other hardware also has access to RAM.

So the CPU can communicate with other hardware by putting data in RAM and then reading and writing the other hardware’s special addresses.

And vice versa: to read from disk the CPU writes to special addresses that are handled by the drive which then fetches data and writes it into RAM.

The CPU is then interrupted to let it know the data is in RAM.

Interrupts

Interrupts are a mechanism to let the CPU know something has happened that it may need to deal with.

Common interrupts are when data is available from some non-RAM hardware and timers going off that the OS can use to switch between tasks.

The kernel

The kernel is the core of the operating system.

It runs in a privileged mode where it has full control of the CPU in ways user code does not.

It contains the code that handles interrupts.

It can also talk to the computer’s other hardware.

Device drivers

Device drivers are special bits of code that know how to talk to the specific hardware that is attached to the computer.

System calls

System calls (a.k.a. syscalls) are functions defined in the kernel that can be called by code outside the kernel to enter the kernel and do things that only the kernel is allowed to do.

The specific system calls provided by the kernel are a big part of what makes an OS what it is and are why software compiled for one operating system won’t run on another operating system.

System libraries

There are only a few hundred syscalls in a modern OS and of those most programs probably use a couple dozen.

Another important part of the operating system is the system library which is a set of code containing functions that can be used by user programs that provide a richer interface to the basic operations provided by the kernel’s syscalls.

Most programs are written in terms of the system library, rather than using syscalls directly.

On some OSs (Mac and Windows) that is required because the system library and the kernel are more tightly integrated.

On GNU/Linux the kernel (Linux) defines a stable set of syscalls and the system library (provided by GNU) uses the syscalls but you can also write code that uses syscalls directly.

GUI

Graphical User Interfaces are typically provided as part of the interface: on a desktop they provide the now-traditional windows, mouse, pointer (WIMP) interface we’re all used to.

On a phone the interface is different since it’s based on touching and swiping and on-screen keyboards.

The GUI system is both a way to run programs (e.g. by double clicking or tapping an icon) but also provides a library of graphical elements (widgets) that programs can use.

Shell (CLI)

Before we had GUIs we had command line interfaces (CLI) where we ran programs by typing commands.

Programmers still tend to use CLIs for many things because they are very powerful if you know what you’re doing.