Linux Programming – Part 1: The World of Linux Kernel

Let’s begin the journey to better understand the Linux kernel.

What is Linux? To answer this question, we need to understand what composes the OS and what’s inside. With gcc, we’ll discover the deepest level of the Linux system, the kernel, and the system-call. Let’s find out what’s really going on there.

Intro

So, I’ve begun chronicling the daily progress of my C language’s self-learning procedures (Learn C Language – Part 1: Address and Pointer). And along with that, I also want to keep track of what I’ve learned through my self-learning procedures of Linux programming with C in this channel – Linux Programming. I know, I know – two different lines of self-learning procedures now co-exist in my blog, and it may be a little confusing. But please know that those posts not only are beneficial for you guys but also beneficial for me to chronicle the daily progress of my self-learning journey. Here, we’ll learn about how we can interact with the Linux kernel and build our original Linux commands by calling Linux’s system-level APIs within the kernel. This is going to be a long series, but hope you can enjoy our journey through the open-source world.

What editor do we use?

Here, we’ll use vim. The reason? It’s simple – the editor is integral when we want to dig deeper into the Linux world and understand what’s really going on there. In Windows, we generally use IDEs, such as Eclipse, Android Studio, or Visual Studio. Still, those GUI (Graphical User Interface) tools prevent us from interacting with the deep level of the OS. Our goal is to interact with the Linux kernel and understand what Linux is from its deep core.

What is Linux?

When we say Linux, it suggests the kernel, the deep core level of the executable and bootable Linux system. What Linus Torvalds manages is also the kernel. The Linux OSes we are familiar with, such as Ubuntu or CentOS, is a user interface that is powered by the kernel inside. In Ubuntu Server 2020, for example, you can find the Linux kernel program in /boot directory.

image 01: vmlinuz file in /boot

Devices and device drivers

One of the most important Kernel’s responsibilities in the system is to control all the hardware devices connected to or inside the device. Here are some devices that are under the kernel’s control:

  • CPU
  • Memory
  • HDD
  • SSD
  • DVD-ROM driver
  • CD-ROM driver
  • Graphic adopter
  • Network adopter
  • Sound card
  • Hardware clock

Let’s focus on HDD, for instance. HDD has several different types, and if there are three different types of HDDs, the kernel will require to have three different codes to interact with them individually. However, most of the codes among them should be the same, except for the part that interacts with the different types of HDDs. In this case, wouldn’t it be better if we independently separate the HDD-interacting part from the rest of the common codes just like the image below? And the special type of software that interacts with specific devices is called ‘device drivers’ or simply ‘drivers’. I’m sure you’re familiar with the term.

image02: device drivers

System call

To interact with hardware, programs have to ask the kernel to indirectly interact with them. And to ask the kernel to do the job, programs have to use system-call (the system refers to the kernel). Here are some system -call examples:

  • open
  • read
  • write
  • fork
  • exec
  • stat
  • unlink

And here is a sample system-call code:

n = read(fd, buf, sizeof, buf);

As you can see from this example, system-call is basically just calling a function.

To understand what Linux is…

Here are the three most essential elements of Linux systems:

  • File systems
  • Processes
  • Steaming

To understand the deeper level of Linux, we have to understand what the kernel is. And what we really need to know is its integral part: the system-call.

Leave a Reply