Source | The Programmer’s Desert Island SurvivalAuthor | The Programmer’s Desert Island SurvivalHave you ever wondered why most operating systems are written in C rather than other languages? This article provides the answer.
C Language is Very Popular in the Processor World
First of all, it must be said that C language is really very simple. Just take a look at K&R’s classic “The C Programming Language”; a thin book that covers C language completely.Now look at Java, C#, etc. If the book introducing these languages isn’t thick enough to prop up your monitor, you might feel embarrassed to read it.Because C language is so simple, it has been ported to many CPU architectures, and many CPU designers’ first task is to port C language.Suppose you have created a CPU with your own unique machine instructions. Now that the CPU is ready, the next question is how to write programs to make your CPU work?We know that CPUs can only execute machine instructions, and programmers use high-level languages to write programs, so high-level languages must be translated into machine instructions. The next question is which language to choose? The extremely complex Java/C# or the simple C? The answer is obvious.Therefore, what you need next is to create a C compiler for your designed CPU. Previously, this was done by modifying gcc, but now you can also use LLVM (note that this is not as simple as it sounds).
As you can see, the first high-level language that processor designers think of to drive the CPU is C, so what language should be used to write the operating system for that CPU is self-evident.
C Language is the Most Independent
Why is C language considered the most independent?Think about it: do Java, Python, C#, etc., not require a large runtime system, including interpreters, thread models, garbage collection, etc.?In contrast, C language is much simpler; it does not require any runtime system (excluding the standard library). It does not need a memory management system, an interpreter, a thread model, etc. This means that your C programs can run directly on the hardware.
Ability to Directly Manipulate Hardware
Let’s take a look at where the operating system is located in the entire computer system:As you can see, the operating system is located between the application programs and the hardware, which means that the operating system must shield the hardware from the upper layers. This also means that when writing an operating system, one must be able to directly control the hardware, especially memory management, and C language was born for this purpose.C language does not come with a memory management system like Java; in C language, this work is entirely controlled by the programmer. This control is extremely important when developing operating systems. Coupled with the powerful tool of pointers, programmers can easily manipulate memory, such as creating page tables, DMA controllers, Memory-mapped I/O, etc.Some students may feel intimidated by pointers; indeed, for most programmers working at the application layer, we can still write useful programs without pointers, such as Java programs, Python programs, etc. These languages do not have pointers, and we do not need to directly face the hardware, which is precisely because the operating system shields us from it. However, at the operating system level, we cannot escape this, and C language can solve problems cleanly and efficiently.
The Closest High-Level Language to the Hardware
C language does not have complex data structures, such as various containers, hash tables, trees, etc. Programmers must implement these themselves, which is something many find inconvenient. However, this design intention is to allow programmers to know exactly how C code affects the hardware. C language is also the high-level language closest to machine instructions; it is very transparent, and this is almost impossible in other high-level languages because one of the design philosophies of C language is:
Trust the programmer.
Programmers using C language should clearly know what they are doing, which is very suitable for writing operating systems that are close to hardware and involve a lot of low-level details that require precise tuning. Of course, the downside is the lack of some built-in common data structures mentioned earlier.
Conclusion
This article introduced why most operating systems are written in C language and discussed many advantages of C language. However, it is worth noting that every language has its own usage scenarios, and C language is no exception. C language is very suitable for system programming and other low-level directions, but at the application layer, you have many choices.I hope this article helps everyone understand the relationship between C language and operating systems.