An Entire RISC-V Operating System In 2000 Lines

While Microsoft and Apple don’t release the source code for their operating systems, a good estimate is that it takes around 50 million lines of code to run these software behemoths. The Linux kernel alone holds around 30 million lines, with systemd containing over one million lines on its own, which doesn’t include estimates for the desktop environment or other parts of a standard installation. But millions of lines of code, or even hundreds of thousands, aren’t necessary for building a fully functioning operating system. This one sets up a complete OS in exactly 2000 lines of code.

Called egos-2000, short for Earth and Grass Operating System, the diminutive operating system is written for RISC-V computers and while it does contain most of the tools we would recognize in an OS, it was built specifically for computer science students by PhD candidate Yunhao Zhang. The slimmed-down operating system makes it possible for students to easily read and understand every feature of an operating system without it becoming too overwhelming, and can be easily used and modified to experiment with. The name itself comes from its design principles, where parts of the operating system that interact with hardware directly are part of the “Earth” layer and parts that don’t depend on hardware being placed in the “Grass” layer, with applications taking up a third layer.

The OS is available on this GitHub page under an MIT license and works on real RISC-V hardware as well as within various emulators. Building a complete operating system in so few lines of code is an impressive feat, and making it comprehensive enough to teach students with goes well beyond that accomplishment as well. Often when concepts in computer science are reduced to their bare minimum components, we end up with completely illegible (but interesting) experiments like this programming language instead.

34 thoughts on “An Entire RISC-V Operating System In 2000 Lines

    1. Tanebaum was the go-to text when I studied.
      We were tasked with hacking MINIX.
      IIRC I made the TTY module re-entrant so you could have multiple terminals hanging off your 8088 PC… Fun times.

    1. You are right and I won’t claim myself that egos-2000 is a complete OS. I’m the author BTW. It barely contains everything we wish to teach our undergrads for education purposes only.

  1. i rather like simple operating systems. i feel that things like windows, unix, linux, etc. try to do things far outside the scope of what an os should be. and technically you can strip any of those down to their cores and dispense with their software suites they call features and end up with something rather snappy. until some nasty dependency in some piece of software you need comes along that makes you re-install crap you got rid of in order to speed things up. such as getting rid of the app store just to find out a piece of software you need can only be obtained through the app store.

    1. It’s impossible to avoid complexity when you want to have a very high performance and scalable system. Try building a large multicore system, with multiple network cards, that can handle 1M HTTP requests per second using a simple design.

      1. Scalability means the ability to expand the workload without limit, so of course it requires complexity. Very high performance on a fixed type of single-threaded task does not require complexity, it requires the OS to load program and data, export results, and otherwise keep out of the way.

        Between those extremes, most computer users never change their hardware and only add new programs occasionally. They have no need for thousands of drivers and the ability to have 32,000 threads active at once. Operating system bloat is caused by trying to meet everybody’s desires. and it degrades response time.

      2. I somewhat disagree… It also depends on hardware and goals.. Eg SQL is very complicated for high performance scalable system, but some NoSQL approaches can be very simple and outperform the SQL at large scale… Different mindset.

        If you have 1000+ Cores, preemptive multitasking may not be important, each core might have easy hardware way to send and receive messages to other cores. Memory model might have each block of memory either read only to all cores or read write to a single core, each core might have 64 kilobytes of local memory that no other core can touch other than by asking through messages.

        Lots of issues simply don’t exist under different approaches… You lose stuff like no SQL loses compared to SQL, but you might be better off without it… Better to handle 10x the load but have some results a little outdated

      3. Yes! It’s also impossible to avoid complexity when the lines between the OS and application level code become blurred with functionality that should be optional application features become embedded into the operating system.

    2. “and technically you can strip any of those down to their cores and dispense with their software suites they call features and end up with something rather snappy. ”

      That reminds me of Windows CE. It was stripped down below usability, often. It was pre-installed on those cheap ARM based nettops of the late 2000s.

      1. i was thinking more of community driven images to slim the os, but ms has their own slim versions, like the hard to get ltsb versions and of course their iot offerings. ce is a good example though. their pre-smartphone pda os was kind of crap (i want to say pocket edition but im not sure).

  2. these sorts of projects are myriad in academia. it’s definitely a lot easier to play with and do assignments against than most (all?) ‘real’ OSes.

    and fwiw i still haven’t even read the lions book.

    but personally i would advise people to read the linux 0.01 source. it’s about 10,000 lines of C and asm. it’s pretty readable, you don’t need to read every line to understand the shape of it. and it’s fairly complete and you can really see the seeds of modern linux there. it’s a pretty good introduction imo. yeah i’m really recomending a cover to cover skim of the whole thing shrug. less `find . -type f` (obviously you’ll want to jump around to answer questions as you go)

    1. I wouldn’t exactly use Unix/Linux as a prime example of an OS. Too crusted and dated, too obsolete. Monolithic kernal etc. A relic of the 1960s, in short.
      Better have a look at Amiga OS, CP/M or Atari TOS or BeOS/Haiku. These are fresh, young OSes.

  3. I would love to see stripped down linux kernel actually for ie the classical thinkpad x220 with libreboot and co.
    How much can you tear off and keep it functional and coherent ?

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.