Recent Updates RSS Hide threads | Keyboard Shortcuts

  • How to Write a Hello World Module of Linux Kernel

    Manish 11:14 pm on May 17, 2009 | 0 Permalink | Log in to leave a Comment
    Tags: embedded Linux, , Kernel Programming

    Every Journey starts with a single step. So does the kernel programming. In Linux kernel many vital functions are arranged in the form of modules, that can be loaded when needed. Here a small module that prints  “Hello World” when loaded into the kernel is made. When the module is removed from the kernel, “Good Bye” is printed.

     

    Here is the code for FirstModule.c :

     

    /*

     * Hello world Module

     *

     * Manish Shrestha, June 5, 2008

     * */

     

    #include <linux/module.h>

    #include <linux/kernel.h>

     

    /*init_module is called whenever the module is loaded into the kernel*/

    int init_module(void)

    {

        printk(“Hello World from Manish Shrestha\n”);

     

        return 0;

    }        

     

    /*clean_module is called whenever the module is unloaded from the kernel*/

    void cleanup_module(void)

    {      

        printk(KERN_INFO “Good Bye From Manish.\n”);

     

    }    

     

     

    In order to compile the module, create a Makefile with the following content:

     

    obj-m += FirstModule.o

    all:

            make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

    clean:

            make -C  /lib/modules/$(shell uname -r)/build M=$(PWD) clean

     

     

     

    Now run the makefile by running the following command in the shell as root user:

    #make

     

    This will create a module file FirstModule.ko. To inset this into the kernel module issue the following command:

    [root@localhost module]# /sbin/insmod FirstModule.ko

     

    To check if the module has really been inserted and the init_module() has been called, use dmesg and lsmod commands.

    [root@localhost module]# dmesg | tail

    Hello World from Manish Shrestha

     

    [root@localhost module]# /sbin/lsmod

    Module                  Size  Used by

     

    FirstModule             5376  0

     

    vfat                   13249  0

     

     

    Now lets use rmmod and see if the cleanup_module() has been really called and the module has been removed from the kernel:

    [root@localhost module]# /sbin/rmmod FirstModule

    [root@localhost module]# dmesg | tail

    Hello World from Manish Shrestha

     

    Good Bye From Manish.

     

     

     

    Issuing the /sbin/lsmod command will also tell reveal that there is no module name FirstModule in the kernel.

     

     
  • Welcome all to this Nepalese Technical Blog

    admin 6:38 pm on February 10, 2009 | 1 Permalink | Log in to leave a Comment
    Tags: , mechanics, nepalese blog, software, technical blog

    Welcome to this Technical Blog.  I would like to invite everybody intreseted in technical field related to electronics, mechanics, software, embedded system, biomedical system or any other technical field to participcate in this Blog. This Blog has been established to promote the discussion and information exchange related to technology available in Nepal. 

     

    You are all very much welcome to participate in this Blog. Please register yourself. If you want to write an article for this blog, please write me a short introduction of yourself, I will promote you to the contributer of this blog.

     

    Everyone thanks again,

    Manish Shrestha

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
esc
cancel