I wrote the following code to create a kernel thread:
#include<linux/init.h>
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/kthread.h>
#include<linux/sched.h>
struct task_struct *task;
int data;
int ret;
int thread_function(void *data)
{
int var;
var = 10;
return var;
}
static int kernel_init(void)
{
data = 20;
printk(KERN_INFO"--------------------------------------------");
task = kthread_create(&thread_function,(void *)data,"pradeep");
task = kthread_run(&thread_function,(void *)data,"pradeep");
printk(KERN_INFO"Kernel Thread : %s\n",task->comm);
return 0;
}
static void kernel_exit(void)
{
ret = kthread_stop(task);
}
module_init(kernel_init);
module_exit(kernel_exit);
On giving the insmod command, I am able to create a kernel thread named "pradeep" and I can see the new thread using the
ps -ef
command as follows
root 6071 2 0 10:21 ? 00:00:00 [pradeep]
and its parent is kthreadd whose PID is 2.
But I am not able to stop this thread on giving rmmod
command. It is giving the following output:
ERROR: Removing 'pradeep': Device or resource busy.
Can somebody please tell me how to kill this thread?