Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Programming isn't my main job, though I enjoy it and sometimes get paid for it. For many years now I've been hearing about Linux and my friends have shown to me many *nixes (or *nici?), though I stick with Mac OS.

Do you think there are any parts of the Linux kernel that I could enjoy looking at, that would help me understand what's the whole stuff about? For example, how Linux is different from Darwin?

I've grown up with assembler and DOS, so things like interrupts or low-level C shouldn't be barriers to understanding. But in the end I'm more interested in high-level concepts, like threading or networking stack - I know different operating systems do them differently. And I'm looking for something fun, easy and enjoyable, like late-night reading.

(Note: made a CW, just in case)

Update: I looked for some docs and started reading:

share|improve this question
2  
Probably better off reading the code for BSD (an operating system which, like Linux, is based on Unix). Not only is it much better designed, but Mac OSX is based on BSD... so you'll also be reading a large portion of the OSX kernel! –  BlueRaja - Danny Pflughoeft Jun 22 '10 at 21:21
1  
See also Rusty's Unreliable Guide to Kernel Hacking, if you're looking for fun, it has a nice section on funny comments found on the kernel. –  ninjalj Jun 22 '10 at 21:28

12 Answers 12

up vote 23 down vote accepted

I would recommend looking at LXR. It makes it easier to follow the flow of the code (you do not have to search for each function that is called — well, you have, but the site does it for you).

Some starting points, for the current version (2.6.30):

  • start_kernel() — think of it as the kernel equivalent of main(). This function initializes almost all the kernel subsystems; follow it to see in code what you see scrolling on the screen during the boot.
  • entry_32.S — system calls and interrupts (x86-32 version, which should be nearer what you know; note the use of the AT&T assembly dialect instead of the Intel dialect you might be more used to).
  • head_32.S — the kernel entry point. This is where the kernel starts after switching to protected mode; in the end, it will call start_kernel().
  • arch/x86/boot — the real-mode bootstrap code. It starts in assembly (boot/header.S), but quickly jumps into C code (starting at boot/main.c). Does the real-mode initialization (mostly BIOS calls which have to be done before switching to protected mode); it is compiled using a weird GCC trick (.code16gcc), which allows the generation of 32-bit real-mode code.
  • arch/x86/boot/compressed — if you ever wondered where does the "Decompressing Linux..." message comes from, it is from here.
share|improve this answer
    
I think the LXR url you have has changed. -> lxr.free-electrons.com/source/kernel –  Ben Mezger Aug 26 '14 at 13:18

Myself, I've always found the task scheduling code a bit of a hoot :-/

Methinks you need to get yourself a hobby outside the industry. Or a life :-)

share|improve this answer
3  
+1 from OP. You're right :) –  ilya n. Jun 19 '09 at 18:08

The comments in the kernel can be pretty funny. There's some tips on where to find the best ones on kerneltrap.

arch/sparc/lib/checksum.S- /* Sun, you just can't beat me, you just can't. Stop trying, arch/sparc/lib/checksum.S: * give up. I'm serious, I am going to kick the living shit arch/sparc/lib/checksum.S- * out of you, game over, lights out.*/

share|improve this answer
    
I was going to suggest this as well! –  James Thompson Jun 18 '09 at 4:55

linux-0.01.tar.gz is Historic Kernel and good for start
it is simple and tiny and better for start reading
(also it have void main(void) Instead of start_kernel() lol :D )

share|improve this answer

You might want to read or skim a book that describes the Linux Kernel before looking deep into the Linux kernel.

The books that come to mind are:

share|improve this answer
    
What would you recommend among things that are (1) shorter, (2) on the Internet? –  ilya n. Jun 18 '09 at 4:24
    
The second book ("The Design of the UNIX Operating System") is good, but won't tell you anything about Linux. It was written more than twenty years ago---well before the creation of Linux. What it does give you is a nice tour of the UNIX kernel of the time, describing the various modules and algorithms without diving into the code. It's out of date, but because it describes a simpler version of the kernel it's also makes it easier to learn about many of the key concepts. –  Keith Smith Jun 18 '09 at 13:30

You'd probably get more out of reading a book on OS theory. As far as source code goes: I've got no idea, but you could easily download the Linux kernel source and see if you can find anything that appeals.

share|improve this answer
    
Me like English not... me like C:) –  ilya n. Jun 18 '09 at 4:23
    
oh, then Pax is right - you need a life :-) –  David Johnstone Jun 18 '09 at 4:32

This should turn up some interesting code when run in the src directory:

grep -ir "fixme" *

also try with other comical terms, crap, shit, f***, penguin, etc.

share|improve this answer

You need to re-define the word 'fun' in your context. :)

That said, the Linux kernel may be too much of a monster to take on. You may want to start with some academic or more primitive kernels to get the hang of what's going on, first. You may also want to consider the Jolix book.

share|improve this answer

It's been recommended by quite a few people that v0.0.1 of linux is the easiest to understand.

Though, if your looking for good kernel source to read, I wouldn't go with linux, it's a beast of a hack(about like saying the GCC sources are "fun") Instead, you may wish to try Minix or one of the BSDs(Darwin is basically a branch of NetBSD iirc) or even one of the many free DOS clones if everything else is a little too scary..

share|improve this answer

For fun I guess you could also see Minix, it isn't exactly linux but Modern Operating systems by tenenbaum is a good read.

share|improve this answer

Try reading the code that implements these character devices: /dev/zero /dev/null /dev/full

And maybe the random number generators if you are inclined. The code is straightforward and simpler than all other device drivers since it does not touch any hardware.

Start at drivers/char/mem.*

share|improve this answer

kernel.h

Some simple tricks we can learn, such as

#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
...
#define min(x, y) ...
...
#define container_of
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.