Pick a Free OS

Introduction to Linux kernel modules

Let us now program a simple module to review its life cycle. The init_module is called when the module is inserted into the kernel where as the cleanup_module is called just before removing it from the kernel. In the following program, the init_module and the cleanup_module functions are demonstrated.

/* Simple Linux kernel module Feb'2001







/ initialise the module /

int init_module()


printk("init_module invokedn");

printk("the message is printed from the kernel spacen");

/ if the non zero value is returned, then it means that the init_module failed and the kernel module can't be loaded /

return 0;


/ cleanup / end of module life cycle */

void cleanup_module()


printk("cleanup_module invokedn");

printk("module is now going to be

unloaded from the kerneln");


Compile the above program using the following:

#gcc -Wall -DMODULE -D__KERNEL__ -DLINUX -O -c simpelModule.c

Run the compiled module using the following:

#insmod simpleModule.o

Remember, you have to run the above command from the Linux shell at raw console (not from the console in Xwindows environment) at root login.

Now check the status of the module using


Then remove the module using

#rmmod simpleModule

If you have not seen any of the module-initiated console printing (implemented using `printk') about the status of the module, use the following command to see the kernel messages.

dmesg | less

In the above, commands insmod, lsmod and rmmod are used to load and unload modules to the Linux kernel. The details are discussed in the following section.

Loading modules

insmod loads the `loadable kernel modules' in the running kernel. insmod tries to link a module into the running kernel by resolving all the symbols from the kernel's exported `symbol table'.



Technically, no, this introductory course won't cover the intricacies of device drivers but let me expand on that.

This course is meant to be an introduction to the basics of kernel programming; that is, it's going to cover the foundations of what every beginning kernel programmer needs to know in terms of kernel structure, configuring and building a new kernel, how to write loadable kernel modules, transferring data between user space and kernel space, simple debugging and so on. Clearly, none of that represents writing an actual device driver, but it's all essential information you'll need when you want to start writing specific types of drivers.

There are already plans for followup courses such as "How to write a PCI driver" and "How to write a USB driver", courses that will (probably) consist of perhaps a dozen lessons each and will cost about the same amount as this introductory course (or maybe slightly less, depending on length).

So if you feel that you already know kernel programming at a basic level, you can just wait for the driver course you're interested in. On the other hand, if you're truly new to kernel programming, you will probably need this intro course as a lead-in to the driver course.