What is a Kernel thread?

tijin picture tijin · Feb 28, 2012 · Viewed 27k times · Source

i am just started coding of device driver and new to threading, went through many documents for getting an idea about threads. i still have some doubts.

  1. what is a kernel thread ?.
  2. how it differs from user thread ?.
  3. what is the relationship between the two threads ?.
  4. how can i implement kernel threads ?.
  5. where can i see the output of the implementation?.

Can anyone help me ?. thanks.

Answer

Mircea picture Mircea · Feb 28, 2012
  1. A kernel thread is a task_struct with no userspace components.
  2. Besides the lack of userspace, it has different ancestors (kthreadd kernel thread instead of the init process) and is created by a kernel-only API instead of sequences of clone from fork/exec system calls.
  3. Two kernel threads have kthreadd as a parent. Apart from that, kernel threads enjoy the same "independence" one from another as userspace processes.
  4. Use the kthread_run function/macro from the kthread.h header You will most probably have to write a kernel module in order to call this function, so you should take a look a the Linux Device Drivers
  5. If you are referring to the text output of your implementation (via printk calls), you can see this output in the kernel log using the dmesg command.