The Programmer and the Operating System. Part 1

Fortra Armenia
Fortra Armenia
Published in
4 min readJul 24, 2019
Cromemco [CC BY-SA 3.0]

Many programmers don’t know what the operating system is for and what role it has in the function of a program and how they use the resources and functionality the operating system provides them without even realizing it. Today we’ll go on a detailed excursion to the world of operating systems, and try to give the reader fulfilling answers to their questions.

As an example, let’s review a piece of the following program:

int fd = open(“/home/me/file1”, O_RDONLY);

read(fd, buf, 10);

The showcased code is written with the POSIX standard. It opens the /home/me/file1 file and the first 10 bites are read.

First of all, in this example, the functions are declared and implemented in the standard library of C language. To use it you need to include the corresponding file from that library into your code with the #include directory. Then you need to link the library to the object file of your program with the corresponding parameter in the linker. However, the need for these actions is rare as the compiler does it on its own. A good programmer has to have a good understanding of what happens to the written code in all details when it is compiled and then during the loading of the program.

Now let’s review the essence of the code presented above. Those are system calls, i.e. they are part of the interface provided to the programmer and the program by the operating system. It’s almost impossible to write any meaningful piece of code that doesn’t use these type of interface functions. Every operating system provides from a hundred to a thousand similar functions, with the help of which the virtual machine is implemented.

So the file opening function is called. It has to address the operating system using the tools provided by the processor. In the Intel IA32 architecture that tool is called the call gate. In its essence, it’s a group that consists of the software interrupt and the function that handles it. The handler function is placed in the operating system kernel and one of the first steps of loading the system is registering the handler functions like that in the corresponding registries of the processor. You can get more fundamental knowledge about this from the computer architecture books, or by participating in courses on this topic.

The call of the “open” function leads to the loading of the values of the arguments of the function in the corresponding registry of the processor, just like the architecture of the processor and the operating system demand. Afterward, a software interrupt with the respective number takes place. As a result, the software interrupt handler function is called, and it has to understand what is expected of it and call the function of the corresponding component.

The operating systems consist of a large number of complex components, which can be connected with a large variety of complex and intricate connecters. Depending on the designation and architecture of the system, the number, the functionality and the communication methods of these components can differ. In this specific case, the filesystem function has to be called, which opens the file, transferring it the parameter provided by the program.

The called function is a part of a virtual filesystem. The virtuality lies in the fact that the processes taking place at this level don’t depend on the specific implementation of the filesystem. What does this mean? Every serious operating system usually supports several filesystems. For example, the Windows system supports FAT32, NTFS, ExFAT and a few other filesystems.

On this level, we need to decide to which exact filesystem module we need to direct the request to open the file. For this, we need to analyze the path of the file at hand. We need to start at the end and proceed with breaking down the file path, analyzing on our every step if the folder is a mount point for the filesystem. Let’s assume the /home folder is a mount point in this case.

With this, we can find the drive and the partition number that has the relevant file on it. These partitions are usually created when the operating system is installed. The drive is divided into a few parts which get a specific meaning: /boot, /home, swap area, etc. To find out what filesystem is on that part we need to read its superblock. It’s usually the block #1. You can learn more about this by studying literature on file systems or operating systems.

To read the block we need to address the corresponding device driver…

The story goes on, the number of components at hand increases, the process gets more complicated… And that’s only natural, as we’re dealing with one of the hardest programs: the operating system. Having an understanding of what’s happening in the underground is essential if you want to become a good professional.

To be continued…

The text was originally featured on Vahram Martirosyan’s Medium in Armenian.

Keep in touch with HelpSystems Armenia on Facebook, Instagram or LinkedIn.

--

--