Why not develop an operating system in Go?
Why not develop an operating system in Go?
Why not develop an operating system in Go?
This article is compiled from an article on GoLand’s official blog: “OS in Go? Why Not?”
It explores why programming languages like C have an advantage in OS development, and whether you can use Go to write OS.
In addition, the feasibility and limitations of using Go to develop the operating system, as well as some current achievements are also mentioned.
An operating system (OS) is the heart and soul of a computer system, managing the computer’s hardware and software resources and providing a way for users to interact with the computer.
Languages such as C and Assembly have traditionally been used to develop operating systems because of their low overhead and “close to machine code” nature.
But the rise of high-level languages such as Go has introduced features that may make it easier to develop complex software such as operating systems.
For example, type safety, error handling, and concurrency should be beneficial when developing an operating system.
Therefore, it should be a natural choice to adopt a high-level language like Go to develop an operating system, but why is there no successful case?
An operating system is made up of different components that are responsible for different functions and can be written in different programming languages.
The heart of an operating system is the kernel (Kernel), which is responsible for interacting with the hardware—almost always written in C or assembly language.
As for user-facing components such as GUI applications, they can be written in any language.
For example, Android uses Java to write user-level components such as the GUI framework and system applications (camera, phone, etc.).
Correspondingly, its kernel is written in C and assembly language; the underlying system components, the libraries, are written in C++.
The main reasons why the C language “rules” the kernel:
- Direct memory management
- Lack of abstraction
- No runtime dependencies
- Portability
Go offers many desirable features as a high-level language.
From this perspective, it seems like it could be an excellent choice for developing an operating system:
- Certain types of bugs are much less likely to occur in high-level languages
- Con-currency: Dealing with concurrency is easier in high-level languages because almost every high-level language has built in the mechanisms needed to handle concurrency
- Type safety: prevents C-like lax type enforcement
But while Go offers desirable features that can make life easier for operating system developers, it has some limitations.
As a language with garbage collection, Go is not really suitable for operating system development. Writing the kernel in Go means carefully bypassing Go’s garbage collection.
As mentioned by a developer in a Reddit forum, mouse lag can be caused by interrupt handlers allocating memory that triggers garbage collection.
In addition, Go also requires a large runtime to execute, which means that it cannot run directly on hardware.
Although TinyGo can compile Go to run on bare metal, it only supports a small number of architectures compared to C, which can run on almost any architecture.
In general, it is almost impossible to develop an operating system that is not a “toy” using Go.
In particular, it is extremely challenging to support running on multiple architectures, support different devices (such as graphics cards or network cards), and comply with POSIX standards.
Finally, take a look at the case for exploring the development of an operating system in Go.
Biscuit is an operating system developed in Go and runs on the 64-bit X86 architecture.
It is implemented using a modified Go 1.10 runtime with more assembly code added to handle booting and entry/exit of system calls and interrupt handlers.
Biscuit provides POSIX interface for user process, supports fork, exec, etc.
It implements a filesystem that supports core POSIX filesystem calls.
Biscuit implements a TCP/IP stack and driver for Intel PCI-Express Ethernet NICs written in Go.
Using the POSIX interface, Biscuit can run many Linux C programs without modifying the source code.
However, Biscuit lacks many features, such as scheduling priorities, swapping out pages or disks, and security features, such as users, access control lists, and address space randomization.
Why not develop an operating system in Go?
