8

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

davidhaskins
  • 2,158
  • 2
  • 18
  • 26
  • 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 06:45

3 Answers3

10

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

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

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

  • 5
    -1 for anonymous accusations. –  Feb 24 '11 at 07: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 - on strike Feb 24 '11 at 07:04
  • @Larsman, I present myself in my account so this is NOT an anonymous accusation. –  Feb 24 '11 at 07: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. –  Feb 24 '11 at 07:12
  • I don't mean you don't identify yourself; you accuse "one prominent kernel developer" without identifying who that is. We can't verify or falsify the accusation. –  Feb 24 '11 at 07:13
  • @Pierre: Erm, I don't know who "G-WAN" is, but more importantly, I think you missed my point *entirely*. I said that you and I would consider that comment example to be pointless noise. Neither one of us write it. The Linux kernel was written for expert programmers, by expert programmers. My argument is that they would probably see the comments we write as helpful or clarifying as simply trivial noise. – Cody Gray - on strike Feb 24 '11 at 07:16
  • @Larsman, this guy was obviously talking to me because he trusted me. I doubt that he ever publicly published his tactics - or that he wanted this information to be published because this would harm him, his reputation, and his business. I have no such a goal. –  Feb 24 '11 at 07:18
  • @Cody, Seach "G-WAN" on any search-engine and you will find what it is (or consult my profile, or RTFA: read my post saying that it is a "server"). THEN, you will understand that no Linux experienced programmer EVER wrote a server as fast as G-WAN. This, probably, qualifies me as an "expert" too. –  Feb 24 '11 at 07:22
  • @Pierre: Apparently I bruised your ever-delicate ego. My apologies. I should have noticed from your answer that you're quite a blow-hard, what with the anonymous accusations and all. I've grown tired of this conversation. – Cody Gray - on strike Feb 24 '11 at 07:25
  • @Cody, you are tired of getting ashamed for your recurring lies? That's understandable. But the cure is simple: stop the practice in the first place. –  Feb 24 '11 at 07:46
  • 4
    -1. I wish I could subtract more. –  Feb 24 '11 at 11:16
  • @Jim Balter: It's at -3 currently, and that's sufficient to gray it out. I'll pile on just in case, for the reasons already mentioned. – David Thornley Feb 24 '11 at 14:35
  • Visit the [G-WAN site](http://www.trustleap.com/). You'll notice that its author, Pierre Gauthier (this answer's author), has a big personality. He's opinionated, aggressive, and *maybe* a little paranoid. Still, he's interesting and fun to read. Since he's out-of-step with most developers, it's easy to blast his opinions with down votes. I'm not sure this is helpful. This answer is just an opinion like so many other opinions. He doesn't smear anyone directly and even offers an explanation. Is it the truth? I couldn't say. But I, for one, enjoy his ability to stir things up. – Corbin March Feb 24 '11 at 15:27
0

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.

refro
  • 1,386
  • 6
  • 17