Understanding the operating system and why C is so important
The Operating System is a well known part of the computer however was once extremely vague in my mind. I knew of the term and knew that it was the bottom layer of software that interacts with the hardware but I had no idea how it was implemented or what it even did. On top of this, I thought of C as a soon to be extinct programming language used by old school programmers. Little did I know C is what powers all programming languages.
This article is a brief overview of what operating systems do and a little about how it does it. It attempts to shed light on the importance of the C programming language.
We all know windows. We all know Mac. Most of us know Linux. Some of us know Unix, and even fewer know Xenix, MS-DOS, Ubuntu, Android, iOS…
These are all OS. However, technically speaking Linux isn’t an operating system. That is why you can’t run Linux, you run Ubuntu or “flavours of Linux”. Linux is simply the kernel. The kernel is the largest part of any operating system, however does not do the full job which is why you don’t run Linux, you run operating systems’ that use the Linux kernel.
Understanding the kernel is key to understanding everything so lets dive in…
WHAT IS THE KERNEL:
The kernel is simply a program stored on your computer’s hardware. It is a special program with an important job, but simply a program nonetheless. The Linux kernel was written by Linus Torvalds mostly in C and can be seen here. In fact you can even make edits to it because it is open source. What is mind blowing is that it is mostly written in C, meaning all operating systems that use the Linux kernel are built from this one language. Most other kernels like Darwin (used in Mac OS) and Windows are also written in C. This is one of the reason why C is the most fundamental of all programming languages, because it has the ability to directly control a computers hardware and is the building blocks of most if not all operating systems.
The kernel is invisible if you look at a computer. You don’t control it, it just works in its own little world. The kernel is the first program loaded after your computer boots up, The kernel’s job is to do among other things, these main tasks:
1.) Process management
- A process is just a program in execution, and at any given time you will be running a large amount of processes. You can see all the processes running by running the command below in Bash
ps aux
- Each process has a process ID and takes up a certain amount of the CPU’s time. If you don’t understand how multiple programs run at the same time sharing the CPU and would like a basic understanding of the microprocessor in all its glory then see my other article.
- The kernel provides the functionality to create, kill and manage all processes.
2.) File management
- The kernel is also responsible for providing the functionality to create, files delete files, open files …
3.) Memory management
- Another main job of the kernel is to manage the memory. Without going into great detail; a microprocessor requires communication with memory to perform operations. This volatile memory is called RAM (random-access-memory). This memory is intermediate storage that your processor uses to compute. Since this memory is volatile it does not retain any information when the computer has no electricity flowing through it, so we require a hard drive to actually store information forever. See this article for more info. This hard drive is what stores your programs and any saved files. The kernel is what fetches anything you need from this memory. For example if you save a text file, it gets saved on the hard drive and to access it (read it) you have to go through a kernel function. What can really make you think, is that the kernel function ‘read’ is also stored on the hard drive. Since it is the first program that runs when a computer is booted up we don’t need to worry about how the kernel starts up.
4.) Device Drivers
- Another important task the kernel accomplishes is connecting the hardware to all outside devices. For example any input and output device such as keyboard mouse and screen must send and receive information. It is the kernel that allows such functionality to occur.
The kernel accomplishes these tasks because Linus coded the functionality of all those system functions in C. C programs we write will use wrapper functions like fork(), exec(), read(), write(). Those functions are commonly called system calls because they ‘call’ or ‘ask’ the kernel to do something.
These wrapper functions are executed in the user space and are easy to use. Underneath, they send an interrupt signal to the CPU so that the system functions that Linus wrote can run and actually accomplish the task. Those system functions run in the kernel space.
WHAT IS A SHELL:
A shell is the next layer that allows us (a user) to control computer hardware. The diagram below shows the different connection levels.
A shell is the main user program. Remember a user program is not allowed to run any system functions. That can only be done by the kernel. The shell can be thought of as the user program that allows other user programs to access the computers hardware by using the kernel.
A shell is how the computer receives user input. Meaning without your shell, you would not be able to talk to the computer.
Because of the wrapper functions provided in C which can be thought of as an API to actual system functions, we can create C executable files to perform different functionalities.
The most common shell these days is obviously a GUI. A GUI can be touch based, or require a mouse and keyboard but always displays output to a user. A GUI can be a set of click, drag actions from a mouse and button hits on a keyboard that all define system calls by this user program (shell). For example double clicking an icon to start an application makes a request to the kernel to run a system call to create a new process.
Another type of shell is a command line interfaces. Many exist and can be written in different languages. The most common shell for Unix based systems are: bash, tcsh, and korn. These shells mostly provide the exact same functionality just with different syntax. Each provides its own unique commands by executing its own unique C executable files. Those C executable files often make system calls (remember a system call is simply a request to the kernel to perform something). The confusing and incredible part is the implementation of the system function by the kernel is also written in C. C is everywhere, and is so important…
An example of a bash command such as ‘ls’ (list) which lists all files in a directory will run an executable C program in the computers /bin directory. That executable will make system calls such as fork(), exec()… and the kernel will run the built in system functions to accomplish the task.
Command line commands are similar to clicks and drags in a GUI based Shell. They provide the same functionality just in different ways!
Conclusion
C is not the only way to control your hardware or kernel however it is how the kernel implements the main functionality discussed earlier. It is also the basis behind Bash commands and other command line interfaces.
Any executable file like a python script, Java program, or any application in whatever language can be run by the computer and can make its own system calls to the kernel, but those system functions are actually written in C making it the building block of all programming.