248 points · by mpweiher · 8y ago · 56 comments
I think smaller projects are better for learning purposes. If you are interested in reading some smaller projects, check out my project here https://github.com/CodeReaderMe/awesome-code-reading.
Some ways you can start:
- Here is start_kernel(), the kernel entry point after booting up and handling the lowest level stuff in asm: https://github.com/torvalds/linux/blob/v4.19/init/main.c#L53...
- grep for SYSCALL_DEFINE to find definitions of syscalls, e.g. this is open(): https://github.com/torvalds/linux/blob/v4.19/fs/open.c#L1076
(understanding how the I/O and networking system calls work is quite helpful for application developers, even if you work in node.js, python or another high level language)
- this is the struct that represents each process in the system, you can pick some interesting field and search for where it is used and where updated: https://github.com/torvalds/linux/blob/v4.19/include/linux/s...
Finally, the linux-insides book is pretty helpful: https://0xax.gitbooks.io/linux-insides/
Being able to semi-quickly figure out how the kernel actually performs some I/O operations and what the exact semantics are is tremendously useful when working with low-level I/O applications (e.g. database-like applications).
I'd read it the way it was written: from the beginning. What's Linux 0.01 look like? What's the next changeset after that release look like? What was necessary to add the driver for your favorite device? What changes were made for your particular CPU?
Programs are not static works (except maybe TeX and Metafont). They exist in the form they do in order to be amenable to changes. So look at the changes that drove it.
I read Bitcoin 0.1.5 [1], which only has 15,000LOC and is the first tagged version on Github. Compared with the current Bitcoin codebase with 320,000LOC, it's much less daunting!
[1]: https://github.com/CodeReaderMe/awesome-code-reading/issues/...
I can't decipher this 1000-line function, but it came from somewhere. What did it start out as? That's what the author originally intended it to be. What caused it to grow? That's what features someone else thought it needed.
Instead I would suggest that you try to debug a problem, or try to understand what a particular syscall does rather that starting from init/main.c. If you have a goal in mind (trying to solve a problem or understand a specific aspect of the kernel) you are far more likely to get useful information out of the exercise.
Another useful hint is that you should just ignore the majority of code that looks alien. Linux uses a lot of macros and synchronisation primitives that you probably aren't familiar with (RCU for instance). It's much more useful to take note of the things you don't understand and just move on to the code code that you do understand. Most of the macros and synchronisation primitives are used all over the kernel so you're very likely to get to grips with them over time.
I really hope that bpftrace will make it easier for people to get into kernel debugging and thus have a nicer "in" to kernel development.
https://github.com/markjandrews/wrk-v1.2/blob/master/base/nt...
Almost every line of code has comments.
Worked fine for me when getting into media subsystem to write camera and camera soc interface drivers.
http://www.leancrew.com/all-this/2011/12/more-shell-less-egg...
Knuth's solution has its own elegance and interest including various efficiencies and from-scratch facilities.
McIlroy's will make you wonder how much you're wasting in time and complexity.
https://www.cs.tufts.edu/~nr/cs257/archive/don-knuth/pearls-...
(ACM ref, too: https://dl.acm.org/citation.cfm?id=315654 )
Also, I found a blog post which links to a C version of the exercise in the spirit of Knuth's approach, and a Haskell version in the spirit of McIlroy's:
https://franklinchen.com/blog/2011/12/08/revisiting-knuth-an...
C2 was interesting in that software developers and IT professionals tried to categorize debates and disagreements over software engineering issues. Almost nothing was ever settled, but you got different perspectives. For example, is the "best" software about maximizing economics & psychology (human grokking), or symbolic parsimony? Is it better to hire 5 geniuses or 10 ordinary engineers?
It's honestly pretty upsetting that one vandal is enough to ruin one of the oldest and most significant institutions on the web for everybody.
On Topic: there was a discussion a year ago about what the progress block is: https://github.com/WardCunningham/remodeling/issues/24
It seems that he's stuck with some files that are mixed-encoding and can't repair them, it's likely the wiki will be read-only until that problem is solved, unfortunately.
On Topic again: c2.com is great, I'd love if someone would take ownership and keep maintaining it.
https://lucene.apache.org/core/7_5_0/core/index.html
From my experience, studying the API Docs for Java and using it as a guide to the code makes understanding the code much easier.
The code is here:
And I don't think it is just that Knuth does it that catches my attention either. Ideally, I think (if I understand the premise of literate programming), literate programming would force the programmer to take the time to explain the code being written, and ideally, the rationale behind why that code solves the problem at hand. And to do that, the programmer must first understand the code being written, and I think upon attempting to do that, they'll find they have not sufficiently thought the problem through.
I fear too many of the coders I know would not honestly attempt it; they are in too much of a hurry to get the code written.
Now, I don't think literate programming is the only way to accomplish that; simply writing good docstrings and comments might suffice to a good degree (and good ones: I too often review code where the docstring is a basic repetition of the function name and arguments, or where the comments explain what is being done, but not why). But far too often I see no comments, no docstrings, no tests, just miles and miles of code, and so it is no wonder that Knuth is what he is.
I also just looked at your profile, and it resonates a lot. Tell me what you think of mine: http://akkartik.name/post/about
And though you didn't ask me, I read it (having been annoyed by your LP post for a few years now) and it strongly resonates with me — the mission, and everything until the start of the “Constraints on a solution” section. IMO the way to give more people more understanding of programs is not to write an entire new programming language / operating system and hope that enough people switch to it, but to work on delivering that “understanding” for existing programs in existing languages. It may be harder, but is more likely to be useful, and you also get feedback on what kind of understanding people want and lack.
If you have a typical TeX distribution installed, you can see an approximation by running `texdoc tex`, which brings up the PDF that is also available online e.g. here: http://texdoc.net/texmf-dist/doc/generic/knuth/tex/tex.pdf -- but it is a bit hard to make sense of if you're not familiar with the conventions of WEB (the book starts with a "How to read a WEB") -- as a workaround maybe you can read webman (http://texdoc.net/texmf-dist/doc/generic/knuth/web/webman.pd...) though that's meant for the programmer (working with the raw .web files) rather than the reader (working with the typeset output).
And it helps to be familiar with Pascal, and the typical conventions of programming style in the 1970s (e.g. that of the P4 (Pascal) Compiler and Interpreter, also published as a book and also available online: https://homepages.cwi.nl/~steven/pascal/ )
I've been reading the TeX program in bits and pieces for over a year now, and only now do I have a rough sense of it all :-)
The bad thing about a print book is that you would have to type the code to run it... How would you go about running TeX? I find the most effective way to read source code is to run it (to answer questions like: "what's the output if the input is this thing", etc.)
I wish someone who's familiar with the moving pieces would put together a version that runs on a browser.
I have read the main method where it attempts to launch pid1 of /bin/bash, etc.
Is there a really good place to start reading? For example how does Linux talk to the hard drive?
What's the first thing that happens in the kernel?
Once you have mental map of major areas of the kernel it will be easier to relate to the sourcetree.
There used to be a number of great books on fairly esoteric issues of the kernel (like, comprehensive explanations of network stack). However most of them seem to be stuck at 2.6 and are no longer very up to date.
But as others suggest, taking an older version, or even simpler unices(xv6,netbsd) might be easier.
Indeed! It took me a while a appreciate (because it can be painful). Reading bad programs can be very educational, especially when you try to infer the decisions or processes that caused it to be bad in a particular way.
It might be a bit old (I honestly don't remember the specifics) but most of the code was already "mature" at the time so I think it would still be valid.
Agree on nginx - I wanted to make a crazy change and found it extremely easy to understand how I could have done it and why doing it would be stupid :)