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

  • 12
    The Linux kernel is crazy. It does lots of weird things for the sake of performance. Thus is much harder to read than application level c. Don't ever write code like that! – Byron Whitlock Feb 24 '11 at 6:45

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.

Grab a copy of Spinellis' Code Reading and/or Kernighan and Pike's Practice of Programming.

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.)

  • thanks larsmans, code reading book speaks a lot about c, can you suggest similar book on C++ – hue Feb 24 '11 at 9:08
  • @hue: I've never found quite such a book about C++. If someone else knows one, I'd be interested. – Fred Foo Mar 1 '11 at 1:11

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).

  • 4
    -1 for anonymous accusations. – larsmans Feb 24 '11 at 7:04
  • While I agree that comments are important, one could certainly argue that the Linux kernel was written by experts and only intended to be read/modified by other experts. Many of the comments that you and I would find helpful would probably be considered "noise" to the target audience, much like we would never write: i++ // increment i by 1 – Cody Gray Feb 24 '11 at 7:04
  • @Larsman, I present myself in my account so this is NOT an anonymous accusation. – Pierre Feb 24 '11 at 7:11
  • @Cody, the day you will write code as fast as G-WAN (I wish you could), you will understand that while I heavily comment my code, I probably do not need "i++" to be commented. – Pierre Feb 24 '11 at 7:12
  • 3
    -1. I wish I could subtract more. – Jim Balter Feb 24 '11 at 11:16

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy