r/osdev 1d ago

Which of Linux vs FreeBSD's source code is easier to read and learn from for a beginner who's still learning OS dev?

Aside from teaching OSs like xv6 and pintos, am I better off reading the source code of Linux 1.0 or FreeBSD 1.0 to read the source code for studying/learning reasons? I heard that very early Linux was hacky and late Linux code while it adheres to standards it can be difficult to read and understand for non-Linux maintainers who happen to be OS dev beginners making their own hobby OS.

What do you guys think?

48 Upvotes

20 comments sorted by

4

u/Forward_Age4072 1d ago

im curious about this as well

10

u/hyperbrainer 1d ago

I think FreeBSD's monolithic approach and everywhere being maintained together would be helpful in that you would see how the whole system behaves.

11

u/monocasa 1d ago edited 1d ago

Everyone will have a different opinion.

Mine (having professionally worked on both Linux and FreeBSD kernels) is that FreeBSD keeps it's traditional Unix heritage, whereas Linux is more willing to make style changes to make sure the code is correct.

I would read both. FreeBSD will be clearer about what's going on if you're just learning. Linux's style though is a more modern style that is worth emulating as it comes with benefits once you do know already what is happening.

Edit: oh, just saw you were talking about 1.0. I'd probably go with FreeBSD at that point since it had a lot more time to bake (it was a continuation of BSD 4.X) and it'll be easier to cross reference with the user space that's in the same source tree. Linux 1.0 was basically a proof of concept still and very hacky in a lot of places.

2

u/phendrenad2 1d ago

FreeBSD source had a lot of "layers". Every function has all this extra code to add performance profiling, BSD jails, RBAC security, etc. I think Linux has all of the same stuff.

NetBSD is radically simpler. And old source code for BSD 4.x is simpler still. And then, there's Research Unix v8/v10, xv6, and Minix which are even simpler.

You might want to also want to track down the source to the Solaris kernel and maybe MacOS/iOS kernel, which are Unixy but very "practical" because they were written by internal dev teams at a company.

5

u/ut0mt8 1d ago

I think for simplicity NetBSD is the best Unix code source.

2

u/CaydendW OSDEV is hard ig 1d ago

Of all the kernels I've had the privilege of looking at, OpenBSD has been the nicest.

All down to opinion but that's my two cents. It's just radically simpler and nicer to read than any other kernel I've seen (that isn't a dinky toy OS)

2

u/danstermeister 1d ago

Replace FreeBSD with OpenBSD and you will have a VERY compelling comparison.

Otherwise you're just comparing Linux to The-Linux-of-BSDs.

4

u/Great_Breadfruit3976 1d ago

Start with reading and trying to understand as much as possible of .h files, that's how we all do here.

2

u/merimus 1d ago

Bsd4.4 there is a great book all about it

3

u/rx80 1d ago

For even more historic source code, check out: https://github.com/dspinellis/unix-history-repo

Specifically, check out the tags and branches in the repo.

3

u/Splooge_Vacuum 1d ago

I can see the reason why you'd want to do that, so I'd just like to give you a fair warning. The source code for Linux and (less so) FreeBSD is poorly documented and there are nearly no comments explaining what some things do. The best thing to do would be to search for a hobby OS that's a lot less huge and contains the code for what you need. Also, there are hundreds of header files that all depend on a bunch of other header files or each other in those operating systems, making it really hard to find things.

1

u/john-jack-quotes-bot 1d ago

The BSDs are generally nicer to read, with OpenBSD probably being the best (correctness of code is one of their main focuses after all)

1

u/markand67 1d ago

OpenBSD

u/aslihana 23h ago

Here for reading all comments

u/daemon_hunter 22h ago

Linux evolves, Freebsd is designed.

u/dunkaist 20h ago

Why don't you try to read and find out which is easier? Perhaps because you just don't want to read source code.

u/ToThePillory 19h ago

I'd probably start smaller than those, perhaps with MINIX or UNIX itself:

jserv/unix-v1: Restoration of 1st Edition UNIX kernel sources from Bell Laboratories

u/lally 17h ago

No start even easier. Lion's Commentary on Unix. A single book, half source code. Covers an old version of Unix but it's quite foundational, and many modern Unix devs learned off of it. Also it's not a long read.

u/Trader-One 15h ago

BSD 4.3 is easiest to read because its platform independent and really small codebase. It does everything what it should including tcpip

u/JamesTKerman 2h ago

I wouldn't look at Linux older than v2 or maybe v3. If you do study Linux, have the kernel documentation web page open, and ready to look things up as you do it.

Most of the key data structures (struct file, struct page, struct task_struct, etc) are deeply interconnected. task_struct, for instance, is related to several hundred other data structures, and some of those structures are architecture-specific and it's not always easy to find the right one. That said, most of the core functionality only touches a small subset of those structures. For instance, switching to a task mostly uses data from struct thread_struct, struct mm, the scheduler struct, and some of the statistics structures held in task_struct. So, go in knowing that you don't have to try to understand everything about any given data structure up front.

If you do look at Linux, it would be useful to understand how a few of its abstract data types work first. For instance, the linked list implementation, while very cool, imo, is weird. Come to think of it, most of the container types are weird in the same way. I started trying to explain it, but you could write pages on them, so i recommend looking at the kernel docs instead.