Weird and Cool things from Linux Kernel Development

Recently I picked up Linux Kernel Development by Robert Love since I like his posts on Quora. Here are the things that I thought were cool or just surprised me.

Threaded Trees

Are cool. They act both like trees and linked lists.

Child processes vs Threads

The only difference between a child process and a thread in Linux is whether the CLONE_VM flag was passed to clone(), which copies the parent’s address space which allows the child to access the parent’s memory.

Struct access is just like array access

For someone with not too much grounding in C, the struct data type seems like any other general-purpose object you’d see in JavaScript or Ruby, for instance. Turns out they’re really simple, and work just like arrays. C calculates the location of a member of a struct by simply adding the sizes of everything before that member. Here’s an example using array notation.

// Define the structure
struct taco {
  int lettuce,
  int beef,
  int cheese,
  int guac
};

taco ourTaco = {
  lettuce: 2,
  beef: 2,
  cheese: 2,
  guac: 2
};

// Fetch it the normal way
int servingsOfCheese = taco->cheese;

// Fetch it the super h5xxor way
int offsetBytes = 0;
offsetBytes += sizeof taco->lettuce;
offsetBytes += sizeof taco->beef;
offsetBytes += sizeof taco->cheese;

// Add the pointer to the beginning of the ourTaco struct
// and the number of bytes needed to reach the beginning of
// ourTaco->cheese. Get the value by dereferencing.
int servingsOfCheese2 = *(taco + offsetBytes);