10 ms·
Exploring the Internals of Linux v0.01
- qwertox 3y agoRegarding the following code, ``` printk("Kernel panic: %s\n\r",s); for(;;); ```, could the for-loop be damaging to the CPU by over-utilizing a small portion over and over again, in terms of it heating up a tiny space on it?
- chucklenorris 3y agoI remember win95 didn't use hlt instruction in the idle thread, it just did the same as linux. Power management wasn't a thing back then. I think ACPI and hlt came with winnt only.
- anthk 3y agoYou could use Rain to cool down your CPU. That tool was useful under VM's and DOSBox too.
- gattilorenz 3y agoOn Pentium or higher. 486s and earlier didn’t really have an HLT instruction, iirc
- justsomehnguy 3y ago>> All x86 processors from the 8086 onward had the HLT instruction, but it was not used by MS-DOS prior to 6.0[2] and was not specifically designed to reduce power consumption until the release of the Intel DX4 processor in 1994. MS-DOS 6.0 provided a POWER.EXE that could be installed in CONFIG.SYS and in Microsoft's tests it saved 5%.[3]
- jandrese 3y agoIdle loops are harder to implement when your system doesn't have multitasking.
- Veliladon 3y agoEven single tasked systems like MS-DOS still had interrupts. You could HLT the processor and a keyboard interrupt could wake it straight back up and resume execution anywhere in the MS-DOS kernel. It's just that the typical TDP of a CPU back then was a couple of watts so there was literally no point in HLTing instead of busy-waiting so nobody bothered.
- gattilorenz 3y agoI stand corrected, I was under the impression that the original Pentium was the first architecture that had HLT, but maybe that was the first architecture I ran Rain on, since it had benefits (having ran Win95 on a 586, but never DOS on a 486 laptop)
- flatiron 3y agoevery x86 has had halt. win95 was just not using it even though you could write a 10 line program to get context switched in when idle that would halt it. it was one of my first programs as a child on a 486 66 dx2. i just had chat gpt generate said program and i think its very similar to what I wrote. I'm unsure if it ever did anything but i've always been interested in efficiency: #include <stdio.h> #include <windows.h> void main() { printf("Setting process priority to low...\n"); SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS); printf("Halting the processor when no other programs are running...\n"); while (1) { __asm { hlt } } }
- qingcharles 3y agoNo.
- Farmadupe 3y agoIm assuming the "halt and catch fire" thing is only really possible on pre-microprocessor machines built of discrete components, where the individual parts are small enough to overheat by themselves if driven wrong. I'd guess the physical CPU package of an i386 would cope just fine with the few (hundreds?) of gates toggling in that small loop. I wonder if it might be possible to do on a modern FPGA, if you artifically create some unstable circuit and pack deliberately it in a corner of the die? There's probably some AVX512 concoction that would be the closest equivalent on a modern X64. There's probably an easy experiment -- if that concoction makes CPU freq drop and also makes whole-package thermals drop, it /could/ be due to localized die heating.
- deleted 3y ago[deleted]
- TillE 3y agoIt's not a risk or anything, but it does waste power compared to using the HLT instruction.
- irdc 3y agoThat’s pretty much what DOS did as well.
- loeg 3y agoNo. CPUs need to be designed to tolerate busy loops without damaging themselves (with appropriate cooling). CPUs of that era did not measure their own temperature, but they also weren't trying to squeeze as much juice through as the later Pentium 4s that ran very hot. Modern CPUs are self-regulating and try very hard to avoid damaging themselves even with inadequate cooling.
- bragr 3y agoNo. Intel CPU's, even way before Linux, were microcoded, so you're still using the full instruction fetch, decode, and microcode system for every step in your infinite for loop. You aren't wearing out the CPU any more than running any other code.
- sedatk 3y agoMy AMD 386DX/40 didn’t even have a fan or a heatsink.
- School-Cotton 3y agoNone of the instructions likely to be emitted by that loop will be microcoded, and the instruction will always be fetched from L1 cache. That said, this won’t be an issue simply because CPUs are designed and tested to be able to handle hot loops.
- bediger4000 3y agoMaybe not the for-loop, but there has been research into damaging CPUs by repeated execution of some particular instructions: https://www.semanticscholar.org/paper/MAGIC%3A-Malicious-Aging-in-Circuits-Cores-Karimi-Kanuparthi/2f5e7346c76bb1e09d11dc188505b29b2aa4a709 https://www.semanticscholar.org/paper/MAGIC%3A-Malicious-Agi...
- userbinator 3y agoAll CPUs didn't consume much power back then even at full load (several watts[1]), and thus leaving the CPU in a busy loop was the norm. [1] http://www.cocoon-culture.com/lib/noise-report/external-docs/Intel-CPU-powerconsumption-data.htm http://www.cocoon-culture.com/lib/noise-report/external-docs...
- colonwqbang 3y agoThe CPU would probably run cooler since it's not doing anything. Most of the circuit would be static, not flipping from 0->1 or 1->0 which is what tends to expend the most power.
- junon 3y agoNope. They're built for it. Typically now on x86 at least you'd do a CLI then for(;;)HLT. That'd park the CPU unless a non-maskable interrupt was latched.
- _8j50 3y agoI had an askhn about a "linux internals" book (lookup "windows internals"), I would be glad to contribute to crowdfunding such an effort, but not strictly for the kernel but every major userspace subsystem as well. https://news.ycombinator.com/item?id=33728590 https://news.ycombinator.com/item?id=33728590
- peter_d_sherman 3y ago>"Linus didn't have a machine with 8MB RAM: '* For those with more memory than 8 Mb - tough luck. I've * not got it, why should you :-) The source is here. Change * it. (Seriously - it shouldn't be too difficult. ...' Today, machines with 8GB RAM are very common. Furthermore, 8GB is not enough at all for software engineers ;)" Bill Gates, 1981: "640K ought to be enough for anyone..." Linus Torvalds, 1991: "8MB ought to be enough for anyone..." Jen-Hsun Huang, 2023: "144TB ought to be enough for anyone..." :-) <g> :-) (Disclaimer: The above quotes are written for comedy purposes only! The above individuals referenced probably didn't actually say those things! <g>)
- cobertos 3y agoWhat is <g>? My first thought was https://developer.mozilla.org/en-US/docs/Web/SVG/Element/g https://developer.mozilla.org/en-US/docs/Web/SVG/Element/g but that cant be right
- robin_reala 3y agoGrin, if I remember rightly.
- throwaway29303 3y agoIt means to grin. Sometimes it shows up as *g* too.
- anthk 3y agoOn 8MB, it was the minimal usable in mid late 90's for minimal systems such as today's 1GB systems with some light X environment. There was even a Howto for 4MB laptops. If you wanted to run X confortabily, even with FVWM and RXVT, you needed 16MB.
- rwmj 3y agoWell, 1993 or 94, I had Linux, X11, fvwm(2?) and emacs running in 5MB. To be fair it was not very fast, and one later and extremely expensive £600 upgrade to 16MB made it perform much better.
- anthk 3y agoIf you had 5MB you would just spawn Emacs in a tty and use SVGALib stuff to display images. Today I do the same with Gemini/Gopher/light HTTP pages and nsxiv to display the pictures and mpv+yt-dlp to watch videos/music/podcasts. That on an Atom N270, which today is like having 4MB and a 386 back in the day in 1999.
- dansalvato 3y agoWow, an Atom N270? I'm envious, I've always wanted to find a fun way to make use of old hardware like that. I have a Google Cr-48 chromebook with a similar CPU and 2GB of RAM. I'd be interested in learning more about your setup to see if I could get something similar going.
- Jenda_ 3y agoI have an EEE 1005HA laptop with N270, 2 GB RAM and an old SSD. I'm running the latest Debian (it is the last version to support x86-32) with Xfce and I'm using it to read books in fbreader and Okular, play podcasts and SD videos with MPV, read IRC (via SSH). Actually it is completely useful except web browsers -- even simple pages which have not changed much since the laptop was new (such as this site or Wikipedie) are pretty slow in modern Chromium/Firefox; "modern" webpages are almost unusable. You can use links2 -g (in graphics mode) or Dillo, they are fast, but the usability is lacking.
- lanstin 3y agoWhen Redhat IPOed (or soon after? quarterly report? I can't remember) they sent out a poster with the 0.01 source code to their share holders. It fits and is fun to read from time to time.
- stevefolta 3y ago> I thought GCC (or C itself) has good backward compatibility, but it's not sufficient. Ha! It's been a long time since GCC was even able to compile older versions of _itself_.
- SubjectToChange 3y agoPar for the course honestly. Obviously major C compilers are only becoming more strict about acceptable code. For instance when -fno-common was made the default in GCC 10 it ended up breaking an rather incredible number of packages.
- yjftsjthsd-h 3y agoSetting stricter defaults makes sense, but not being able to compile older code even with options to disable checks seems like a regression.
- kccqzy 3y agoYears of language-lawyering really refined our own understanding of what is and isn't correct code. Any older code is just so full of UB that they can barely be said to be valid code. It's just a product of the times.
- yjftsjthsd-h 3y agoThat's besides the point. The latest release of, let's say Linux, has bugs in it (statistically, unless it's the first perfect bug-free release ever). Would it be reasonable for a compiler to refuse to compile it? The compiler's job isn't to refuse to compile bad code if it could compile it, only to warn the user and try to help fix it if possible.
- tinus_hn 3y agoFor code that has undefined behavior, the compiler may refuse to compile it or compile it into literally anything. A common mistake is expecting code that has undefined behavior to be compiled into something that makes logical sense. You may disagree with it but that’s how compilers work and how the standard describes they should work.
- penguin_booze 3y agoI don't know if what's shown is the actual source code, but I notice spaces were used instead of tabs. Maybe Torvalds became a tab advocate only further down the line.
- sedatk 3y agoThey were trying to save storage space and build times as the source grew in size.
- parasti 3y agoAny truth to this? Somebody must have ran a benchmark at least?
- water-your-self 3y agoA parse of the topic on the style guide doesnt suggest anything of the sort https://www.kernel.org/doc/html/v4.10/process/coding-style.html https://www.kernel.org/doc/html/v4.10/process/coding-style.h...
- benj111 3y agoI hope the parent was being humourous. 1 tab is 1 byte, 4 spaces is 4 bytes, so it doesn't actually work anyway. And you can't rely on it being multiples of 4 anyway, so you'd have to iterate and check the bytes to check they are actually spaces. So no, it's be slower, but not enough to matter. Edit: or is the parent proposing the opposite? I sti hope they're being humourous as it still doesn't really matter
- sedatk 3y agoNo, that was my actual theory for what happened, but apparently not true at all: https://news.ycombinator.com/item?id=37104012 https://news.ycombinator.com/item?id=37104012
- sedatk 3y agoI actually went ahead and asked Linus Torvalds himself. He said he'd never use spaces, and always used tabs from the get go. So, my theory fails :) > What? No. > It always used tabs. > And by "always used tabs" I mean "mistakes probably happened, and there may be spaces in some places", but afaik I've always used hard-tabs as the normal indentation. > If you see something else, you probably have some archive that has been reformatted.
- quietbritishjim 3y ago> counter = (counter >> 1) + priority ... Note that counter >> 1 is a faster way to divide by 2 ... This means that if a task is waiting for I/O for a long time, and its priority is higher than 2, counter value will monotonically increase when counter is updated. Actually the effect is that counter will exponentially decay up to 2 * priority (if the task is not runnable). You can sanity check this by looking at the limit: if counter is 2*priority then that expression will leave it at its current value.
- fasterik 3y agoJust like you read the classics in literature, I think it would be interesting to read some of the classic codebases. They should fit the following criteria: - The project was highly successful or influential - Freely available source code - Ideally the bulk of the project was written by one person So far my list includes Linux, the John Carmack id releases (wolf3d, DOOM, Quake), SQLite, and vim. Any others I'm missing?
- ansbalin 3y agoThe Apollo guidance computer source code? https://github.com/virtualagc/virtualagc https://github.com/virtualagc/virtualagc
- fasterik 3y agoVery cool. Unfortunately it's probably not very relevant to current programming practice, but it definitely fits the bill for historical impact.
- nurettin 3y agohttps://github.com/virtualagc/virtualagc/blob/master/Solarium055/PINBALL_GAME__BUTTONS_AND_LIGHTS.agc https://github.com/virtualagc/virtualagc/blob/master/Solariu...
- School-Cotton 3y agoThe original releases of gcc and GNU emacs both fit all your criteria.
- pseudostem 3y agoI am not a programmer (yet). I am an openBSD fanboy. I've read stories of people reading the code and raving about the quality. It does tick 2 of the checkboxes you mentioned (not just one person). Does this qualify?
- fasterik 3y agoYes, I think so. Being written by one person isn't a hard requirement, it's just that I feel like you get a better sense of programming style and someone's approach to problem solving when you read code that hasn't been touched by too many people. Projects with a maintainer who strictly enforces code style and quality would still fit the description. From what I've heard, OpenBSD falls under this category. I'll add it to my list.
- urbandw311er 3y agoI really enjoyed this, thanks for sharing. I’m pleased to see the frequency and depth of the comments in the code too, makes it all very accessible. Anyone managed to get it to compile?
- bergkvist 3y agoI feel like looking at the first working versions of a big successful project is a great way to understand how it works. Usually it will only contain the most important core features without a lot of abstractions/generalizations. So it is actually manageable to read through all of the code in a couple of days.
- SubjectToChange 3y agoI don’t know about this. A lot of the success of a project has to do with how they react to changing conditions and the needs of their users. Moreover I would say that most projects owe a great deal of their success to the nontechnical factors, e.g. licensing or other social factors. Reading the initial working versions of a successful project will inform you on how that project used to work. In a sense it’s akin to reading a working draft of a great piece of literature without reading the final product.
- capr 3y agoThe strcpy implementation is probably similar, just moved to gcc.
- farnulfo 3y agoTo read with the recent https://cpu.land/ https://cpu.land/
- ranting-moth 3y agoYou might also be interested in the "A Heavily Commented Linux Kernel Source Code": http://www.oldlinux.org/download/ECLK-5.0-WithCover.pdf http://www.oldlinux.org/download/ECLK-5.0-WithCover.pdf
- acyou 3y agoSo, modern Linux is 4500x the lines of code of Linux v0.01. Does 36M lines of code in modern Linux mean that the complexity demon is winning? Or do we really, actually need that many? I would love to see a graphical breakdown of how many lines of code, how many functions, etc are used for each major software module. Are there existing tools that do this?
- malux85 3y agoModern linux has 4500x the features as v0.01 - multi CPU architecutre support, better scheduling, better memory allocators, different filesystems, permissions and isolation, more advanced networking, drivers, security. While there is bloat in all software systems, comparing them difficult because one is a MVP and the other is the starship enterprise.
- sthuck 3y agoYou can use plain old 'wc' to count lines. Most of the Linux kernel codebase is drivers. 2nd most is architecture specific code. I don't know what's the actual count of lines of code that I actually have on the kernel compiled on my specific x64 machine, but I think considering how many significant changes were made to the Linux kernel over the years, I don't think it's a victim of unnecessary bloat. The various virtualization features alone were industry changing features.
- khazhoux 3y agoIf one wanted to write their own little OS today, what would you recommend in terms of: * Books or articles to read? Hopefully with a hands-on and practical approach. * Target environment: VM or real hardware, and what specifically?
- pfranz 3y agoI believe the traditional resource is Dinosaur book (OS Concepts by Silberschatz). I've also seen Operating Systems: Three Easy Pieces https://pages.cs.wisc.edu/~remzi/OSTEP/ https://pages.cs.wisc.edu/~remzi/OSTEP/ come up. Another common recommendation is to find a hackable existing OS/kernel. You already have a working OS, but you can inspect and rewrite portions without having to do everything yourself upfront. http://www.minix3.org/ http://www.minix3.org/ seems to be a popular one. Various BSDs seem to be mentioned, as well.
- khazhoux 3y agoThanks. I just flipped through a lot of Dinosaur book (10th ed) and it's not hands-on, though. Back in undergrad in the early 90s, our OS course was we spent the semester building a crappy x86 OS which could boot, run a couple of processes with basic I/O and simple signals and memory management. Even though it had nearly zero concepts relevant to modern OS's, it was so much fun. It didn't set me up in any way to be a serious OS designer, but it was the essence of programming a CPU and some devices, with nothing in between us. I'd love to experience that again. But I don't know where to find resources for the lowest levels: booting into a raw CPU and connecting directly to devices (including display and keyboard). To be sure, I can find plenty of OS texts on advanced memory and process management schemes and so on, but what about the lowest level?
- johndoe0815 3y agoOne example is Stephen Marz' "The Adventures of OS: Making a RISC-V Operating System using Rust": https://osblog.stephenmarz.com https://osblog.stephenmarz.com Unfortunately, this isn't updated to work with current RISC V specs, so you will run into a number of problems which can be frustrating...
- zer8k 3y agoFascinating. I always love seeing the start of things. Often times we put people like Linus above the critical eye because of their great accomplishments. Things like this remind us they, too, are human. What an interesting look at how OS dev was so long ago.
- cies 3y agoWould Linux 0.01 boot work in a virtual environment? (VirtualBox or QEmu) If not it might be interesting to fork it into a linux-zero-dot-zero-one project that merely tries to make minimal adjustments so it runs virtualized on modern hardware.
- deleted 3y ago[deleted]