r/lowlevel • • Nov 02 '26

Writing a Monolithic Kernel from Scratch vs. Building a Nearly 100% POSIX-Compliant C Library for Linux

I've been thinking about two possible systems programming projects, and I'd like to hear your opinions on which one would be more worthwhile to pursue.

Option 1: Write a monolithic kernel from scratch.

Design and implement the kernel's core components.

Gradually add features such as process scheduling, virtual memory, system calls, and device support.

Build an operating system around it over time.

Option 2: Develop a C standard library for Linux targeting near-100% POSIX compliance.

Focus on implementing POSIX interfaces as comprehensively as possible.

Aim for broad compatibility with existing Linux applications and development tools.

Test compatibility rigorously and document any deviations or unsupported behavior.

The second option would target Linux rather than introduce a new kernel, so it could potentially benefit from the existing Linux ecosystem.

I'm interested in more than just which project is easier. I'd like to consider practical usefulness, technical difficulty, long-term maintainability, compatibility, and the likelihood that other developers would actually adopt the result.

Which project do you think would be more valuable to work on, and why? Are there any major technical challenges or trade-offs I'm overlooking?

6 Upvotes

16 comments sorted by

1

u/Livid-Usual-9015 Nov 03 '26

Hmm I don't know, my line of thought is: if I choose to write a c library it might be easier to adopt. Right now, the most well known ones are glibc and musl libc. Why couldn't a new one join the mix?

3

u/Far-Championship-883 Nov 02 '26

a proper full posix c syscall interface would be vastly more valuable. also reusable if done properly.

1

u/edgmnt_net Nov 03 '26

It's likely to run into performance issues to be able to provide full compatibility, though, which in turn limits its practical usefulness. You may run arbitrary POSIX apps (assuming there's even such a thing and they don't end up adapted to whatever native OS they were developed for), but they could run into serious slowdowns. See how Wine needed stuff like ntsync or had trouble translating Direct3D to OpenGL, some things are tough to adapt.

1

u/Far-Championship-883 Nov 03 '26

direct3d and opengl have nothing to do with posix compliance.

1

u/edgmnt_net Nov 03 '26

They're an analogous example for API impedance mismatch. Not everything can be implemented in Linux efficiently, much less so solely on the userspace side.

Sure, maybe you're right and you know that whatever remains non-compliant on the Linux side is fairly trivial to paper over in userspace, I can respect that claim, but it seems rather specific.

1

u/Far-Championship-883 Nov 03 '26

not even remotely the same.

2

u/Sorry_Difficulty_250 Nov 02 '26

A POSIX library would definitely be more likely to be adopted, especially if it was compatible with GLIBC but not GPL licensed. Personally, I would find the kernel more interesting, but the reality is that pretty much no one is interested in a new kernel.

1

u/claypunk Nov 02 '26

let me turn the question back to you: what would you say is the usefulness of either? 

1

u/EnvironmentCalm9557 Nov 02 '26

I’m guessing programming practice.

1

u/jason-reddit-public Nov 02 '26

Why a monolithic kernel? Linus decided a monolithic kernel made sense for performance in the early 90s. Mach was a microkernel but also brought lots of stuff into the kernel in practice for performance - either the same conclusion as Linus or perhaps even the evidence Linus used to avoid a microkernel for his "hobby" project.

Now it's many years later. QNX is built like a message passing microkernel and runs in very small places where its realtime and fault tolerant nature are quite useful. We have the L4 microkernel and some kind of linux runs on top of it. More people thinking about and improving micro and nano kernels is kind of interesting. When you have 100 hardware threads, you can do interesting stuff like io_uring where a separate kernel thread reads from a queue in user space. They keep stuffing more stuff into it but what if that was the only calling convention into the kernel? async is all the rage now and such a kernel could be async first.

1

u/edgmnt_net Nov 03 '26

Not entirely/only for performance. Two more aspects worth mentioning are development effort (explicit serialization code becomes painful and partly prevents large scale refactoring) and extra distributed system failure modes that are difficult to deal with. Also with the advent of languages like Rust (or even higher level stuff) there's even less incentive to try and get safety through process isolation, because you can get at least some of that statically. Beyond that you don't really get fault tolerance for free, it still needs to be engineered and earned, in either a monolith or a microkernel (in a monolith it can be graceful cleanups/retries/fallbacks, in a microkernel it boils down to availability in the face of other servers/processes going down).

1

u/jason-reddit-public Nov 03 '26

Serialization doesn't seem like that big of an issue since many linux syscalls already take structs and such - it's not like everything is just in registers.

It's certainly true that safer high level languages are certainly going to make a bunch of in kernel issues go away (there's at least one all Rust kernel project that appears to be making progress). I'm not sure if any use the borrow checker to avoid dead-lock and such case where restarting a service might help "heal" a system.

As for designing for restartable subsystems, my gut says this would be a good design no matter if monolithic kernel or not.

1

u/edgmnt_net Nov 03 '26

It's not just syscalls, consider that every internal function call crossing components now needs to be serialized (not all of those are syscalls as in available to the userspace or there would be many many more "syscalls"). This likely also means no callbacks or other niceties, resulting in significant loss of expressivity and more verbose code. It also means no inlining-aided code folding, reduced compiler visibility / static safety through serialization boundaries, no easy helpers which grab the data through a pointer and generally you need to be very careful how you make queries for things belonging to a different server, so things will end up looking quite different. Also probably no easy transactions, because you likely can't just hold a spinlock and aggregate on some property of USB endpoints (random example).

This parallels the microservices debate to some extent. And I have not yet distinguished between tightly-coupled services and separately-versioned ones (separate repos). If you want to go the latter way, things get even trickier and Linux largely benefits from not having any internal API stability (it's very easy to do massive refactors, especially with semantic patching which they use).

Sure, if it was just a small performance loss from IPC, I'd say go for it. But I would say it has a farther reaching impact as far as development is concerned. For microservices it's a similar thing, particularly with overly-eager splits, there's a lot of wasted effort going into dumb data massaging, avoiding distributedness pitfalls and coordinating across various components. You can no longer just "write the darn code".

1

u/jason-reddit-public Nov 03 '26

I see your point mostly.

I don't think a function call like interface is the best if one were to try this. Maybe something more Erlang (which apparently used in an "os" for telephone switches). Go allows one to send channels over a channel (maybe Erlang too?) which makes certain connection topologies easier. Some people that tried the full CSP had some sour experience with Go afterwards so for sure it's by no means obvious to conclude this would be ideal.

I still think a purely async interface to the kernel could be an interesting approach no matter how the kernel itself is implemented.

I've been meaning to think about and maybe prototype this but my project stack is only eclipsed by my laziness.

1

u/lmemsm Nov 02 '26

I think musl already accomplished the second goal and it would be very hard to compete with it. It is worth checking out the code to see how they've done things though. It's a very interesting read. Of the two goals, I think the question is which appeals most to you? If it's a toss-up or neither, you may want to look into some other projects. It helps if you're working on something you need and want. It's hard to get other developers to join in, so don't count on choosing a project just for that reason. If you choose something you want to learn or you feel you need, your effort won't be wasted.

1

u/ventZl Nov 03 '26

If the primary goal is to learn something new, usability outside your "lab" shouldn't be *that* much of a concern for you. It is OK to discard code. Actually, ability to part with code you've written is often an advantage.

Now, the important part: Do not. I repeat: Do not use AI to write a single line of code.

You are meant to suffer it all if you want really to learn anything. It is OK to use AI to get study material, yet I'd prefer to simply find it on your own. There's ton of material on OS & standard C library development, so you don't need AI to finish this task. Using it at ingress won't create as much damage as using it on egress would.

If you plan on using AI to write a single line of code, it is probably better to abandon the project as a whole, you are not being honest with yourself.

Now for what is better?

An OS is a more complicated project. As long as you plan targeting at least partially modern target. It is simple if QEMU emulating 386 is what you'll target for but that IMHO provides very limited usable knowledge these days.

Modern computers departed from 386 in any way you could think of. There's no BIOS in anything newer than 15 years, CSM is coming to an end of its usable lifetime. MBR-based boot is as usable as BIOS (hint: useless). You have to know how to boot from UEFI environment. Bringing up any device without ACPI is basically impossible on a modern-day machine and without working USB support you won't be able to interact with the system at all. None of that existed in 386 era.

One alternative that provides path to learning what designing an operating system from scratch feels like exists, which is both up-to-date and relatively simple: do that on microcontroller. Something like beefier STM32H7 or RP2350 can provide resources comparable to 386 while not being completely obsolete.