One of my goals in 2022 was to dive deeper into core computer science subjects.
The first subject in the list is Operating Systems.
This is a very wide subject but for simplicity sake, we will cover the basics.
Today you will learn:
- What is an operating system?
- Different kinds of operating systems
- How are operating systems built?
- Knowledge required to build an operating system
What is an Operating System
An operating system is the first program that starts when the computer boots up. It has four main responsibilities:
- Load and Manage Processes (Programs)
- Provide an abstraction to the hardware.
- File System.
- An interface, either a graphical interface (GUI) or text-based.
Loading and Managing Processes
When you run a program, the program gets loaded into memory (RAM) and gets processed in the CPU.
This is a running program or in short a process.
It's the OS's job to load the program into memory and manage the different programs that are run simultaneously in your system. For example, you might be using a browser and listening to music using some other program. The OS makes sure that the two processes don't interfere with each other.
Another important job of the OS is to allocate resources fairly to the different processes. Programs have different requirements and it's up to the OS to allocate the appropriate amount of resources needed for the process to properly run.
A single CPU core can only run one process at a time, so how do we use multiple different programs simultaneously?
If you only have one core, then your OS would alternate the processes depending on your use. For example, if your playing a game, then the game process would run. If you switch to some other program, the game process will wait and the other process will take its place.
This is the use-case if you only have a single CPU core, nowadays that's not the case. Modern CPUs have between two and 64 cores, allowing us to run multiple programs simultaneously or run a single program more efficiently by utilizing multiple cores.
The second job of an OS is providing an abstraction to the hardware. As programmers, we tend to not think much about how stuff works under the hood.
How do we open files?
How do we send network requests?
This is all done by the hardware, but you as a programmer don't even know it exists. You simply run the functions that the OS provides for you to talk to the hardware. These functions allow us to read/write files and create/destroy processes.
Each OS has a different set of system calls. You can read more about them here:
Another core function of the OS is to provide a good user interface for its users. A big reason why Windows is the leading OS is because of its easy to use interface. On the other hand, Linux is not as popular due to it being catered to a niche audience.
Now that we know what an OS is, let's talk about the different ways of building them. The core of the OS is the kernel. It is the primary interface between the hardware and the processes of a computer. A kernel is made up of multiple different components. For example, these are the main components for the UNIX kernel:
- The Process Scheduler
- The Memory Management Unit (MMU)
- The Virtual File System (VFS)
- The Networking Unit
- Inter-Process Communication Unit
So when deciding on building an OS, we should first decide on how will we build and organize the components of our kernel.
A monolithic OS is very basic in which all the components are inside the kernel. OS that uses monolithic architecture were first time used in the 1970s.
CP/M and DOS are simple examples of a monolithic OS.
The advantage of using a monolithic OS is that it's fast and simple. The downsides are that it gets hard to maintain and is not fault-tolerant meaning that if one component fails, the whole OS fails.
In a microkernel OS, the kernel contains the minimum amount of functionality needed. These functions include low-level address space management, thread management, and inter-process communication.
The other services are in the user address space. This allows for extensibility meaning that you can plug n play with different components.
The benefits of this approach are:
- The architecture of this kernel is small and isolated hence it can function better.
- Expansion of the system is easier, it is simply added to the system application without disturbing the kernel.
The introduction is pretty much over but if you ever want to make your own OS and don't know what you need to know. Here's a list of subjects that you should be familiar with.
Knowing systems programming will make you a better developer.
So I suggest everyone to create a simple OS or compiler.
Thanks for Reading