I want to understand the linux kernel code, and I have been going through it but I am not able to get the full feel of what is happening(I am concentrating on network part of the linux kernel), so can anyone of you suggest good practices of reading C (or any other lang) code in general and specific to network part of the linux kernel. Thanks in advance
migrated from stackoverflow.com Feb 24 '11 at 14:31
This question came from our site for professional and enthusiast programmers.
If you need to learn C by reading first, then the Linux kernel isn't where you'd start: it's a rather atypical, low-level program (though I've found it to be quite well-structured).
Rather, look at the source code for some BSD utility program, e.g. those in OpenBSD. Read its manpage for a semi-formal, high-level specification of the program.
Finally, read a good book on Unix or Linux kernel internals and start reading the kernel. (I've found Maurice J. Bach's Design of the Unix Operating System (ca. 1986) to still be a good start, even for Linux.)
What I have been amazed to see when I tried to do the same thing (that you want to do) is to find so few relevant comments (if any) in such a complex code.
The documentation should be the code, right, but the comments feature exist in the C programming language for a reason.
If they have been removed, that's probably also for a reason: to keep you away from it.
I sincerely doubt that all kernel developers have to work on this cleaned-up code-base and my view of it is that (at least) the author of any given kernel "feature" (like epoll, for the sake of the discussion) keeps a private version of the code WITH all those missing comments.
Why do I belive this?
One prominent kernel developer, trying to convince me to release the source code of the G-WAN server as open-source, advised me to "make it as difficult as possible to read".
He added that this tactic worked marvellously for him during decades on all his "open-source" projects.
Keeping your grip on any critical part of a widely used "open-source" project obviously creates opportunities when your revenues exclusively come from consulting.
So, back to your question, the most useful thing that you can do to understand this code is to study it step by step and ADD the missing comments.
Then, progressively, it will start to make sense (and you will find why the comments have been removed).
What sometimes helps me is debugging through the code. The visualization of actual data and the actual paths tend to make it more understandable for me. As pointed out by other members linux kernel code is a very bad starting point for understanding c.