Butting, I Mean Booting Into Linux Kernel.
Tags: Linux Kernel, Operating Systems, Linux, Programming

So I am going to read through a book called Understanding the Linux Kernel by Daniel P. Bovet & Marco Cesati which was recommended to me by Mr. Manish Kumar Bhojasia who runs an educational institution called Sanfoundry, his course first introduced me to the subject. How do Operating Systems work, that has always been on the back of my mind whenever I would run into some un-decipherable logs back when I was a Linux admin.

So I am going to translate my understanding ( limited ) here in as simple a manner as possible.( Because maybe someday I might forget what I read, and want to revisit it — not because I like you and want you to learn ).

Let’s get into it then, first of what the f@#$ is an Operating System and why do we need it? Well a while ago some folks developed some hardware that could do calculations, and within a decade they developed stuff that could handle all kinds of operations. But human beings cannot speak to machines and explain their needs very well ( there is voice recognition now, but even that’s pretty choppy), in comes the Operating System , an interface between humans and computers or computers and computers or aliens and computers ( just kidding ). We are mostly familiar with Windows OSs, because most personal computers in the last couple of decades came with these installed. We’ve seen MS-DOS, Windows 98, Windows XP (my personal favorite), Windows Vista ,Windows 7,8 and 10. The personal computing world is awash with Windows because it has a better and easier interface and many useful applications that can help people do their day to day jobs. However Windows has its limitations and major security concerns, which requires us to install third party anti-virus, firewalls and other software. Plus its really difficult to diagnose an issue, and there are plenty of issues. While Windows ruled the world of personal computing, Unix or Linux ( which is free ), did so much more better in the enterprise computing world. Linux was simple, not bloated and could handle large operations fairly well. So in a nutshell, in the server world Linux is King.

Linux’s main philosophy is don’t add shit that isn’t required, which makes it a relatively faster and simpler operating system, and it was free to tinker with , so a lot of enthusiasts quickly grew up around it and did a lot of cool things with it. The possibilities were (are) endless.

By the way, I am by no means the go to person for Linux questions as I am myself a seeker and not a master by any measure.

The Linux Operating System

The Linux operating system is a giant C program, which is executed when you switch on your computer ( if you have Linux installed , otherwise whatever other OS you installed will come online, duh! ). You can get the source code here: http://www.kernel.org. , this is the core kernel, to have any semblance to a desktop OS you might have to install many applications or download and install a commercial distribution, such as Ubuntu or Cent-OS.

The book starts with an introduction which tells you much about the history and advances in Linux. It also gives you a pretty comprehensive idea of why Linux is one of the most awesome operating systems on the planet as of today. The book discusses Linux 2.6.11, considerable advances may have happened since then ( I am ignorant as to what — so please do your own research ).

An operating system can have many applications but the most basic and essential part of it is the Kernel- The core systems which allow other applications to interact with the hardware. When a computer is switched on the kernel is loaded into the RAM. There are two main objectives for the Operating system, interact with hardware components and enable applications to be run on the platform.

Since messing around directly with the hardware can lead to disastrous and unpredictable results, users are restricted from issuing instructions to it. This is enforced by the hardware ( CPU ) which creates two modes of execution, User mode ( non-privileged mode ) and Kernel mode ( privileged mode ).

Linux is a multi-user system ( as opposed to say a Windows Desktop OS ). Different users can log in at the same time into the same Linux machine, if they have access via a network. Users get their own personal space on the computer, User Groups allow users in the same group to use common stuff and protects them from Users from other groups. Root is the name given to the super user or admin or god in a Linux system. You need to be Root to mess around with user account management, do system backups and some program upgrades. Root has no limits.

Every computer has a CPU ( at least one, else it has no brains!). The CPU makes calculations, now all the programs running on a PC wants to use the CPU, but only one can use it at a time. The smallest quantum of this execution is known as a Process. Only one process can use the CPU at any given moment of time, now if we have only one CPU, several processes have to wait in queue. But most modern computers have more than one CPU, and depending on the number, that many processes can run concurrently on the system. Even though a process is the a unit of execution, a single process can execute several programs ( a bunch of code that fulfills some purpose ) sequentially and several processes can run the same code ( program ) concurrently.

Now when the number of processes that wait on system resources increases, we obviously need something to make sure that they all get their time. This mechanism in the Kernel is known as the Scheduler. Nonpreemptable processes cannot be thrown out, and so the scheduler is invoked when it is done. Preemptable processes can be thrown out, and hence are more sensible when it comes to multi user systems where each users programs are fighting for the systems resources.

Unix/Linux is a multiprocessing OS with preemptable processes. Even when no user processes are running, several processes are listening to the peripheral devices, like the terminals, waiting for user inputs.

Unix-like operating systems adopt a process/kernel model. Each process has the illusion that it’s the only process on the machine, and it has exclusive access to the operating system services. When a system call happens the hardware changes the privilege mode from User Mode to Kernel Mode, and the process starts the execution of a kernel procedure with a strictly limited purpose. In this way, the operating system acts within the execution context of the process in order to satisfy its request. Whenever the request is fully satisfied, the kernel procedure forces the hardware to return to User Mode and the process continues its execution from the instruction following the system call.

The Kernel is basically a bunch of programs that run in the background, the scheduler, the memory management module, peripheral interfaces, etc which makes the hardware usable. Now for every process to access these things, they use a system call, and each process runs in the kernel memory space until that call is satisfied. The kernel is loaded in the memory at all times, so it is obvious that the more kernel features we have, the more peripheral devices we try to run, the more RAM is used making the system slow and unable to perform the user tasks. To counter this disadvantage we have kernel modules. A module is an object file whose code can be linked to (and unlinked from) the kernel at runtime. So a filesystem, a device driver, or other features at the kernel’s upper layer does not run as a specific process all the time, instead, it is executed in Kernel Mode on behalf of the current process, like any other statically linked kernel function.

The main advantages of using modules include:

1. Modules can be linked and unlinked at runtime. This makes it easy to develop and run new modules.

2. Even if it may rely on some specific hardware features, a module doesn’t depend on a fixed hardware platform. For example, a disk driver module that relies on the SCSI standard works as well on an IBM-compatible PC as it does on Hewlett-Packard’s Alpha.

3. A module can be linked to the running kernel when its functionality is required and unlinked when it is no longer useful; this is quite useful for small embedded systems with less RAM.

4. Once linked in, the object code of a module is equivalent to the object code of the statically linked kernel. Therefore, no explicit message passing is required when the functions of the module are invoked.

So far we have covered some basic operating system concepts, namely, multi user ability, users and groups, processes and modules. In the coming articles we will discover the Unix filesystem, how stuff runs in the Linux kernel, and why it doesn’t go batshit crazy (most of the time )