Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Kernel thread?

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.

like image 653
tijin Avatar asked Feb 28 '12 11:02

tijin


People also ask

How do kernel threads work?

A kernel thread is a way to implement background tasks inside the kernel. The task can be busy handling asynchronous events or sleep-waiting for an event to occur. Kernel threads are similar to user processes, except that they live in kernel space and have access to kernel functions and data structures.

What is difference between user thread and kernel thread?

Difference between User-Level & Kernel-Level ThreadUser-level threads are faster to create and manage. Kernel-level threads are slower to create and manage. Implementation is by a thread library at the user level. Operating system supports creation of Kernel threads.

How many kernel threads are there?

These are the three kinds of threads. The kernel maintains thread- and process-related information in two types of structures. A process is always created with one thread, called the initial thread. The initial thread provides compatibility with previous single-threaded processes.


1 Answers

  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.
like image 152
Mircea Avatar answered Sep 25 '22 01:09

Mircea