Readit News logoReadit News
tmalsburg2 · 2 years ago
I learned a lot about this from the book "The design of the Unix operating system" by Maurice J. Bach.¹ It's an old book and many details deviate from actual present-day Linux, but it nonetheless gives a great overview of the key components and ideas.

¹ https://books.google.de/books/about/The_Design_of_the_UNIX_O...

cpach · 2 years ago
Seems to be available on the Internet Archive: https://archive.org/details/DesignUNIXOperatingSystem
guerrilla · 2 years ago
This is one of my favorite books. A true classic. There are follow-ups in that style for Linux and FreeBSD as well. I think Robert Love wrote the former.
temeya · 2 years ago
And Marshall Kirk McKusick wrote the latter, "The Design and Implementation of the FreeBSD Operating System"
bigfatfrock · 2 years ago
Thank you! I was going to ask for the latest linux variant - are you speaking of Love's "Linux Kernel Development", or "Linux in a Nutshell"?

I've been a primary linux user for a couple decades now but I'm not too keen on digging into kernel hacking but love details like the OPs post.

cookiengineer · 2 years ago
I can recommend taking a look at /proc and /sys, because that will clear up a lot of how things are intertwined and connected.

procfs is what's used by pretty much all tools that do something with processes, like ps, top etc.

The everything is a file philosophy becomes much more clear then. Even low level syscalls and their structs are offered by the kernel as file paths so you can interact with them by parsing those files as a struct, without actually needing to use kernel headers for compilation.

eBPF and its bytecode VM are a little over the top, but are essential to known about in the upcoming years cause a lot of tools is moving towards using their own bpf modules.

Cloudef · 2 years ago
> The everything is a file philosophy becomes much more clear then.

To be honest, everything is a file is kind of a lie in unix. /proc and /sys are pretty much plan9 inspiration.

arghwhat · 2 years ago
A more accurate term is that everything is a file descriptor.

The main difference is that plan9 uses read and write for everything, whereas Linux and BSD uses ioctls on file descriptors for everything.

wolletd · 2 years ago
Also, a lot of devices require very specific ioctl() commands to work with and don't provide everything as a file.

For example, you can't set the baudrate of a serial port by writing it to some /proc node.

richardwhiuk · 2 years ago
I don't understand what the boxes on this diagram are meant to represent.

It feels like an elaborate mechanism to draw something wrong in the hopes people will correct it.

projektfu · 2 years ago
FWIW, I also don't really understand what the boxes are supposed to represent, given that the arrows represent dependencies like PID <-- process. I thought a PID was an attribute of a process?

To me, a block diagram might show [CPU Scheduler], [Virtual Device Manager], [VFS Manager], [Memory Manager], [Interrupt Handlers], etc...

Of course, my knowledge of Linux internals is limited and perhaps it has a separation of the concept of PID and process where there is a literal dependency.

sevagh · 2 years ago
Interview prep.

Dead Comment

dfc · 2 years ago
My mental model of Linux does not have the CPU/Memory in user space. What am I missing?
suprjami · 2 years ago
Nor does mine.

Userspace assembly runs directly on the CPU* executing in the unprivileged ring. When the userspace program makes a system call by calling a kernel entry function which is mapped into the process's address space by the dynamic loader, then part of that entry into kernelspace is to put the CPU into the privileged ring and kernel assembly then runs on the CPU.

The process scheduler can stop execution to kick a task off the CPU and switch to another one, depending on OS and kernel some things can be kicked off the CPU and some cannot.

Userspace memory allocations are serviced by virtual memory where the page tables track the translation of virtual memory pages into physical memory pages using the MMU.

The kernel is involved during allocation and page fault, but iiuc a regular successful virtual memory access is a hardware operation only.

I don't have a diagram of how this works. Neither processes nor memory are my usual area of kernel.

You'd be better to read the x86 version of the XV6 book to learn how this stuff really works. It's really well written and implements enough to be tangibly useful. Reading the code is optional when just learning concepts. Reading the XV6 code will hopefully help you understand the O'Reilly Linux books better, which will hopefully help you understand the actual Linux kernel better.

(*yes I'm aware CPUs don't directly execute assembly anymore, but the microcode guarantees the observable CPU state at any Instruction Pointer matches the expectation as if you were running assembly on a PDP or C64, or close enough for 99.999% purposes and definitely enough for debugging your program in gdb)

imetatroll · 2 years ago
Do you happen to have any other suggestions about reading?

I am currently reading "Asynchronous Programming in Rust: Learn asynchronous programming by building working examples of futures, green threads, and runtimes" and there is vague talk about how cpus process things, but it really would like to know more. I am even curious about what is actually happening in hardware. It seems hard to determine where to start.

I don't have formal education in this field unfortunately.

Koshkin · 2 years ago
> execute assembly

I always thought that assembly was a type of programming language.

vbezhenar · 2 years ago
Userspace program directly uses CPU and memory (unless you're using VM). In contrast to that, your userspace program does not directly access your network device or SSD, but uses kernel routines to access those indirectly.
sophacles · 2 years ago
It doesn't directly access memory. The addresses in your userspace program are not the actual addresses of memory in the ram sticks - there is a table of mappings that the kernel sets up. When your process asks the kernel for memory, it says "i need 5KB, please put that at address XYZ". The kerenel goes and finds 5KB unused, probably at some other address ABC, and creates a mapping in the table that says XYZ translates to ABC. Then the kernel sets the MMU of the CPU to use the table for your process, and switches back to unpriveleged mode, letting your process run again. Your process in unpriveleged mode sends an instruction to write to the memory at XYZ, but the cpu will translate that instruction to ABC and write there instead.

VMs (well not emulated vms, but if you're doing an x86 vm on x86 or an arm vm on arm) do something similar - an inaccurate (but useful for the concept) way to think of it is that the cpu does 2 layers of MMU for user processes in a vm.

The kernel code isn't running directly when your program accesses memory, but it sets up the cpu so that the kernel still has control over your memory, and you only have access to what the kernel allows - its mediated by the kernel.

persnickety · 2 years ago
If you're using a hypervisor, then the userspace program inside the VM is also using the CPU and memory directly. You'd have to do full emulation to avoid that.

Even with full emulation, I'd say memory is being accessed directly, unless you really go out of your way to make it weird.

akira2501 · 2 years ago
Ring 3 is not "directly using the CPU." And mmap is not "directly using the memory."
knorker · 2 years ago
Whenever I've made notes like this, it's never been useful to my future self nor to anyone else.

The only use I've had of this kind of documentation is that the process of writing it, made me understand it better. Basically write-only documentation.

I would call myself a Linux expert, and while I can kinda see what you mean with this diagram, it would not have been useful to me back before I was an expert.

codelobe · 2 years ago
Usually I would agree. I typically make a "Crash-Course in $PLATFORM" document while keeping notes. These I very commonly reference in order to externalize my memory since it seems to be approaching capacity. I don't care about Ruby on Rails, but once I did, and I can reference my notes if I ever need to touch that platform again.
persolb · 2 years ago
It almost resembles mind mapping. It is a useful ‘process’ to figure out what you think/know. And it might be a pretty picture. But it isn’t very useful as documentation.
falserum · 2 years ago
I found it useful. Allowed me to compare if I have similar idea to the author.

Deleted Comment

timeforcomputer · 2 years ago
Nice! I want to do something similar and map my understanding of Linux. I find some diagrams on Wikipedia fascinating (example: https://en.m.wikipedia.org/wiki/File:Linux_Graphics_Stack_20..., but more to do with the user library ecosystem rather than kernel and program runtime). These diagrams make me want to learn about each part and be able to comprehend in principle what is happening on my machine. Eventually...
Jasper_ · 2 years ago
Any diagram by ScotXW on Wikipedia is somewhere between misleading and completely wrong, and they're a constant pain on the Linux graphics community.

If you're curious about the details in this case, ScotXW confuses EGL and OpenGL, the arrows aren't quite right, and the labels aren't quite right either (DRM is labeled "hardware-specific" but KMS isn't? The label for "hardware specific Userspace interface to hardware specific direct rendering manager" is quite weird), and some important boxes are flat out missing. It's nitpicking for sure, but when the diagram goes out of its way to add extremely weird details, it demands nitpicking.

Nobody in the Linux graphics community would draw the diagram like this.

hn_user82179 · 2 years ago
I remember when Wikipedia first became popular, there were a lot of warnings about how you couldn't trust the information because "anyone could edit it". I feel like, at least to my level of understanding whatever I'm reading about, it's been sufficient and I've never identified something wrong/inaccurate (except for perhaps recent news or recently debated political topics). This is the first time that I've seen that downside of Wikipedia, as I use it for understanding things like this and never would've known that the diagram I was learning from was wrong. Thanks for commenting this, it's good to know

Deleted Comment

Deleted Comment

smitty1e · 2 years ago
I think it needs three areas, not two:

1. User space

2. Kernel

3. Hardware/network

The kernel protects users from hardware, and hardware from users.

topspin · 2 years ago
This is reasonable and correct. I would also have found places in that map for: dcache, block devices, character devices, scheduler, page cache and console/tty/pty. The first two replace "filesystem hierarchy". The second and third are ancient and fundamental classes of UNIX devices.
t1tos · 2 years ago
this is analagous to the fs hierarchy: root protects from the user