Tuesday, April 30, 2013

Programming A Simple Kernel Program / Module

What is kernel?

A kernel is an important part of operating system, which acts as a bridge between user level applications and computer hardware. Kernel allows user level applications, to use system resources such as CPU, Memory, I/O Devices etc.

Why / When to write kernel Programs / Modules?

We know that there are two levels of programming. 1). User space level 2). Kernel level. We already writing so many user space level programs using C, C++, Java, D ...etc. Then why to write a kernel level programs/modules? The reasons are as follows

1). If your program / module excessively using low-level resources.
2). If you are defining a new interface / driver for hardware, which can not build on user level.
3). If you are developing something that is used by kernel subsystems.

Writing a Kernel Program / Module

Create a folder some where in your home directory and name that folder to USB_Module. Now create a C source file called usb_driver.c in that folder with the following source code.

// File Name: usb_driver.c
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/init.h>

static int usb_driver_module_load(void)
     printk(KERN_INFO "USB-Driver Module Loaded...!!!\n");
     return 0;
static void usb_driver_module_unload(void)
     printk(KERN_INFO "USB-Driver Module UnLoaded...!!!\n");


MODULE_AUTHOR("Reniguntla Sambaiah http://umencs.blogspot.in");
MODULE_DESCRIPTION("Kernel module to load USB Driver");
// code end

Now create a Makefile in the same folder with the following code.


KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

.PHONY: build clean

         $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

         rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c

$(info Building with KERNELRELEASE = ${KERNELRELEASE})
obj-m :=    usb_driver.o


Note: Becareful that Makefiles require indentation. So, use Tab character, not a multiple spaces below build and clean options.

Compiling Kernel Program / Module

If you created above two files, then compile the kernel module using following Command.
Once you execute the above command it will create a bunch of files in USB_Module folder. In that bunch of files you can find usb_driver.ko which is a kernel module.

Loading Kernel Module / Program

To load usb_driver.ko kernel module use following command
>sudo insmod usb_driver.ko

 Now you must be thinking why printk in usb_driver.c didn't print "USB-Driver Module Loaded...!!!" on console. Because you are looking at wrong location. It is a Kernel level programming, you can find "USB-Driver Module Loaded...!!!" in syslog. To see this message run following command.
>dmesg | tail

 UnLoading Kernel Module / Program

To unload usb_driver.ko kernel module use following command
>sudo rmmod usb_driver.ko

To see "USB-Driver Module UnLoaded...!!!" message use following command
>dmesg | tail

Getting Kernel Module / Program details

use following command to know the details of any kernel module
>/sbin/modinfo usb_driver.ko


  1. aise hi achche achche program post karte raha kar...SAMBA. It was worth reading...


  2. Thank you, sir!
    Very nicely explained.