title: Simple kernel multithreading
body: <p>I am new to kernel module programming and for my work I need to write a multithreaded kernel module. So I tried some primary uses of kernel threads. I wrote the following. It is supposed to  print 1 in one thread and 2 in another thread, both 10 times.</p>
  
  <pre><code>#include &lt;linux/module.h&gt;
  #include &lt;linux/moduleparam.h&gt;
  #include &lt;linux/kernel.h&gt;
  #include &lt;linux/udp.h&gt;
  #include &lt;linux/mm.h&gt;
  #include &lt;linux/init.h&gt;
  #include &lt;linux/kthread.h&gt;
  
  struct task_struct *task1;
  struct task_struct *task2;
  
  
  static void thread_func(void* data)
  {   
      int *n;
      n = (int *)data;
      int i = 0;
      while(i &lt; 10){
          printk("%d\n", *n);
          i++;
      }
      //do_exit();
  }
  
  static int t_start(void)
  {
      printk("Module starting ... ... ..\n");
      int *p1, *p2;
      int one = 1, two = 2;
      p1 = &amp;one;
      p2 = &amp;two;
      task1 = kthread_run(&amp;thread_func, (void*)p1, "thread_func_1");
      task2 = kthread_run(&amp;thread_func, (void*)p2, "thread_func_2");
      return 0;
  }
  
  static void t_end (void)
  {
      printk("Module terminating ... ... ...\n");
      kthread_stop(task1);
      kthread_stop(task2);
  }
  
  module_init(t_start);
  module_exit(t_end);
  
  MODULE_AUTHOR("Md. Taufique Hussain");
  MODULE_DESCRIPTION("Testing kernel threads");
  MODULE_LICENSE("GPL");
  </code></pre>
  
  <p>But I am facing following problems. - </p>
  
  <ol>
  <li>First thread is printing all ten 1s and then second thread is
  executing. I wanted to run these two in interleaved manner.</li>
  <li>First thread is printing all 1s OK but second is not printing 2s. It
  is printing 0s. Probably parameter is not passing to second thread
  properly.</li>
  <li>When I am inserting module it is running but when I am removing
      module the machine hangs</li>
  </ol>
  
  <p>What are the problems? How can I solve them.</p>