News for hackers. Stuff that matters.
Re:Operating System in 1,000 Lines – Intro by nuta on Wednesday January 08, 2025 @09:17AM (#42634474)
Author here.

I wrote this book so you can spend a boring weekend writing an operating system from scratch. You don’t have to write it in C - you can use your favorite programming language, like Rust or Zig.

I intentionally made it not UNIX-like and kept only the essential parts. Thinking about how the OS differs from Linux or Windows can also be fun. Designing an OS is like creating your own world—you can make it however you like!

BTW, you might notice some paragraphs feel machine-translated because, to some extent, they are. If you have some time to spare, please send me a PR. The content is written in plain Markdown [1].

Hope you enjoy :)

[1] https://github.com/nuta/operating-system-in-1000-lines/tree/...

Re:Operating System in 1,000 Lines – Intro by viraj_shah on Wednesday January 08, 2025 @11:58AM (#42636117)
Hi OP, this looks super cool. I remember hearing about this (https://www.linuxfromscratch.org/) many years ago but have never done it.

Curious, what are the prerequisites for this? Do I have to know about how kernels work? How memory management, protection rings or processes are queued? Some I'd like to definitely learn about.

Re:Operating System in 1,000 Lines – Intro by johnmaguire on Wednesday January 08, 2025 @12:45PM (#42636593)
LFS is about building a Linux distribution from scratch (i.e. using the Linux kernel.)

The book in question is about how to build your own operating system (i.e. a non-Linux) kernel from scratch.

> We'll implement basic context switching, paging, user mode, a command-line shell, a disk device driver, and file read/write operations in C. Sounds like a lot, however, it's only 1,000 lines of code!

Re:Operating System in 1,000 Lines – Intro by nuta on Thursday January 09, 2025 @10:04AM (#42646316)
Thank you! If you've written some programs (ideally in C), you're good to go. You might stuck on some concepts, but you can learn one by one. IMO, implementing it makes you to understand the concepts more deeply, and learn more that you won't notice when you just read textbooks.

Also, because the implementation in this book is very naive, it would stimulate your curiosity.

Re:Operating System in 1,000 Lines – Intro by sesm on Wednesday January 08, 2025 @11:17AM (#42635690)
> Designing an OS is like creating your own world

Or like building your temple so you can talk to God directly

Re:Operating System in 1,000 Lines – Intro by sohang on Wednesday January 08, 2025 @12:04PM (#42636199)
Re:Operating System in 1,000 Lines – Intro by 6510 on Thursday January 09, 2025 @01:41AM (#42642277)
We won't know until we try.
Re:Operating System in 1,000 Lines – Intro by johndoe0815 on Wednesday January 08, 2025 @10:02AM (#42634941)
Are you planning to also provide an English translation of your microkernel book? That sounds very interesting...
Re:Operating System in 1,000 Lines – Intro by Rochus on Thursday January 09, 2025 @05:51AM (#42643972)
Right, that would be great.
Re:Operating System in 1,000 Lines – Intro by nuta on Thursday January 09, 2025 @08:53AM (#42645489)
Unfortunately not. Maybe I'll write another online book like this in English, after building a more practical microkernel-based OS. It's a very interesting topic indeed :D
Re:Operating System in 1,000 Lines – Intro by cplusplus6382 on Wednesday January 08, 2025 @11:15AM (#42635674)
Excellent content!
Re:Operating System in 1,000 Lines – Intro by ilikeorangutans on Wednesday January 08, 2025 @09:35AM (#42634668)
This is fantastic. Thank you so much for writing this.
Re:Operating System in 1,000 Lines – Intro by pshirshov on Wednesday January 08, 2025 @09:42AM (#42634730)
Could you port this to riscv64 please?
Re:Operating System in 1,000 Lines – Intro by ndnsjxj on Wednesday January 08, 2025 @02:52PM (#42637807)
Porting to any platform is trivial after understanding the basics as described in these articles

The author explicitly describes why they chose riscv32

Why would you want the author to use riscv64 instead?

Re:Operating System in 1,000 Lines – Intro by foobiekr on Friday January 10, 2025 @02:23PM (#42659053)
At least on Intel, it was quite a significant difference in some ways, in kernel terms.
Re:Operating System in 1,000 Lines – Intro by nuta on Thursday January 09, 2025 @09:23AM (#42645828)
No it's "left as an exercise to the reader" ;)

But seriously, it's not that hard. Change build options to generate 64-bit ELF, replace all 32-bit-wide parts (e.g. uint32_t, lw/sw instructions), and implement a slightly more complicated page table (e.g. Sv48).

Re:Operating System in 1,000 Lines – Intro by pjmlp on Wednesday January 08, 2025 @10:07AM (#42634994)
Kudos for not being yet another UNIX clone tutorial.
Re:Operating System in 1,000 Lines – Intro by sydbarrett74 on Wednesday January 08, 2025 @04:05PM (#42638426)
Did you post to OSDev?
Re:Operating System in 1,000 Lines – Intro by niutech on Wednesday January 08, 2025 @05:45PM (#42639322)
How does your book compare with the classic "Operating systems design and implementation" by Andrew S. Tanenbaum and Albert S. Woodhull implementing MINIX?
Re:Operating System in 1,000 Lines – Intro by nuta on Thursday January 09, 2025 @10:37AM (#42646668)
I thinks this 1,000 OS book is a good start for beginners before diving into the MINIX book.

MINIX book describes more practical designs, with a more feature-rich implementation. However, UNIX features such fork, brk, and tty are not intuitive for beginners. By writing a toy OS first, readers can compare the toy OS with MINIX, and understand that UNIX-like is just one of many possible designs. That's an important perspective IMO.

Also, readers can actually implement better algorithms described in the MINIX book. It makes the MINIX book more interesting to read.

Re:Operating System in 1,000 Lines – Intro by LeFantome on Friday January 10, 2025 @01:38AM (#42653209)
This is so much easier. You are implementing printf on page 5 and it can handle formatted output to the screen for integers, hexadecimals, and strings. Minix eventually gets around to the write system call but even then it is actually just a way of sending bytes from a buffer to a file descriptor. To a what? That is a lot more complexity for, in some ways, less functionality. For write, you still have to write printf on top.

The Tanenbaum book is great but it is a particle physics textbook compered to this OS cookbook.

Re:Operating System in 1,000 Lines – Intro by laurieg on Wednesday January 08, 2025 @07:53PM (#42640372)
Great resource, thanks for sharing.

I thought the written text was very high quality and didn't show many of the usual tells of a non-native writer. Could you share some details of how you used AI tools to help write it?

Re:Operating System in 1,000 Lines – Intro by nuta on Thursday January 09, 2025 @09:14AM (#42645721)
Copy-and-pasting to ChatGPT. Machine translation (JP to EN) before this LLM era was already high quality, but LLM does a really great job. It does not translate as is, but understands the context, drops unnecessary expressions (important!), and outputs a more natural translation.

That said, LLM is ofc not perfect, especially when the original text (or Japanese itself) is ambiguous. So I've heavily modified the text too. That's why there are some typos :cry:

Re:Operating System in 1,000 Lines – Intro by mayli on Wednesday January 08, 2025 @10:26PM (#42641302)
Nice project, short and concise enough for an undergrad to learn basic os concept and implementation.
Re:Operating System in 1,000 Lines – Intro by jb_briant on Wednesday January 08, 2025 @11:28PM (#42641636)
"Designing an OS is like creating your own world"

Making video game is also like creating your own world!

And it's order of magnitude less hard than making an OS...

Bonus point, you have a chance to make a living from it!

Re:Operating System in 1,000 Lines – Intro by leonewton253 on Thursday January 09, 2025 @09:03PM (#42651762)
actually making a video game would be 100x more complicated. Plus kernel developer jobs are more lucrative than most game dev jobs.
Re:Operating System in 1,000 Lines – Intro by jb_briant on Thursday January 09, 2025 @11:09PM (#42652352)
Is it really more complicated to make a video game that a kernel? I don't consider myself knowledgeable enough to make a full kernel by myself but I did manage to make a full 3d video game by myself.

Also I'm not speaking about making a game engine along with the game.

Having a kernel dev job is different than making your own kernel, speaking on a "world builder" perspective.

Re:Operating System in 1,000 Lines – Intro by 66yatman on Sunday January 12, 2025 @06:17AM (#42672753)
Even a 3D game engine can be simpler than a fairly complex operating system.
Re:Operating System in 1,000 Lines – Intro by pronoiac on Thursday January 09, 2025 @01:59AM (#42642383)
If you want PRs, I suggest you set up Semantic Linefeeds* sooner, rather than later. Each sentence gets its own line, and it really helps avoid merge conflicts.

* https://rhodesmill.org/brandon/2012/one-sentence-per-line/

Re:Operating System in 1,000 Lines – Intro by nuta on Thursday January 09, 2025 @08:49AM (#42645425)
TIL. It's too late for this book, but I'll try it in the next one.
Re:Operating System in 1,000 Lines – Intro by pronoiac on Thursday January 09, 2025 @09:29AM (#42645911)
You have a line per paragraph now, so a quick and dirty version is to add newlines after "[.!?] ", though code blocks might take some fiddling.
Re:Operating System in 1,000 Lines – Intro by Tainnor on Thursday January 09, 2025 @03:21AM (#42643038)
I almost feel tempted to use your book to both learn about OS and improve my shitty Japanese. :)
Re:Operating System in 1,000 Lines – Intro by nuta on Thursday January 09, 2025 @09:45AM (#42646103)
Thank you. My efforts on preparing both EN/JP translations paid off :D
Re:Operating System in 1,000 Lines – Intro by 66yatman on Sunday January 12, 2025 @06:11AM (#42672721)
Can I write it in js?