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