Linux kernel threads-a simple introduction.

Submitted by likhin
on July 25, 2009 - 10:42am

Hi I am new to kernel programming.Recently I decided to try out kernel threads in linux.I googled, and got several articles.But most of them are confusing for a newbie in kernel programming.Especially since there are lot of ways to do things inside a kernel. So I am putting my words here, for all those who want a simple introduction in linux kernel threads for 2.6 kernel.

As most of you know, kernel threads are part of kernel, which gets executed by linux scheduler like a real(userland) thread.The main part of a kernel thread module is, the thread function itself.Lets use some simple prototype like int thread(void *).Below is the thread function.

int thread(void *data)
{

while(1)
{

printk("Hi I am kernel thread!\n");

msleep(100);
if (kthread_should_stop())
break;

}

This function continuously prints a message to the kernel buffer using printk() function.For delay, I have used a function msleep(), where we can give delay required in milliseconds, as a parameter.

The part which follows is a tricky one.The job of kthread_should_stop(function is to check whether the thread should stop(As a result of calling kthread_stop() for this kernel thread).This function will return TRUE if it should stop.In that case we should break from the loop, using a break statement.

The most important part is OK.Now we can look at how to start and stop this kernel thread.

As a reference for the kernel thread, we need a task_struct structure variable. (This is the data structure, which is used inside linux kernel, for representing any thread).

struct task_struct *ts;

For starting the kernel thread, just call kthread_run() and assign to the above created ts variable.I am calling it from the module_init() function

int init_module(void)
{
printk(KERN_INFO "init_module() called\n");
ts=kthread_run(thread,NULL,"kthread");
return 0;
}

And finally when we are exiting we should call the kthread_stop() function,to stop the kernel thread.Pass the ts variable, as an argument to kthread_stop function.

void cleanup_module(void)
{
printk(KERN_INFO "cleanup_module() called\n");
kthread_stop(ts);
}

We are done with the source part of our kernel thread!.Save that file with any name. I am just assuming it as kthread.c Now create a makefile for our module, and compile it by issuing a make command at the terminal window. (Here I am assuming that you have basic knowledge on compiling kernel modules for 2.6 kernels.So I am not elaborating).

If everything went fine, then we will have the final kernel module image,kthread.ko ready to be load in to the kernel!.Do that by issuing insmod kthread.ko

lo! Our kernel thread is running inside the kernel!Confirm it by checking kernel buffer using dmesg.It should show as follows.

Hi I am kernel thread!
Hi I am kernel thread!
Hi I am kernel thread!
Hi I am kernel thread!
Hi I am kernel thread!
Hi I am kernel thread!
.....................
.....................

Finally stop the kernel thread by unloading the module, using rmmod kthread.ko.I am attaching the source file and makefile
here.
http://www.2shared.com/file/6842331/a58e3b37/kthread_filestar.html
Try it. Best of luck!