Difference between User-level threads & Kernal-level threads.

What is the thread?

  • A thread is a small unit of execution within a process. A process can have one or more threads running concurrently, and each thread can perform a specific task within the process.

  • Multithreading refers to the ability of a program to create multiple threads within a single process. This allows multiple tasks to be performed simultaneously within the same program, increasing efficiency and responsiveness.

  • To give an analogy, imagine a chef working in a kitchen. The chef is like the process, and each task the chef performs (chopping vegetables, boiling pasta, etc.) is like a thread within that process. If the chef were able to perform multiple tasks at the same time (i.e. be a "multithreaded" chef), the meal would be prepared more quickly and efficiently.

Imagine you have a computer program that needs to perform multiple tasks simultaneously. One way to achieve this is by using threads, which are like lightweight processes that can run concurrently within the same program.

There are two approaches to managing threads: user-level threads and kernel-level threads.

  • user-level threads, the program manages the threads entirely in user space, without any direct support from the operating system kernel. For example, imagine you have a program with three user-level threads: Thread 1, Thread 2, and Thread 3. When the program runs, each thread runs independently, and the program manages their scheduling, context switching, and synchronization. For instance, if Thread 1 needs to wait for input from the user, the program can switch to Thread 2 or Thread 3 to keep the program running.

  • However, one downside of user-level threads is that if one thread blocks, it can prevent other threads in the same process from running, since the kernel sees only one thread per process. For example, if Thread 1 is blocked waiting for input, the program may not be able to switch to Thread 2 or Thread 3 until Thread 1 unblocks.

  • kernel-level threads, the operating system kernel manages the threads directly. For example, imagine you have a program with three kernel-level threads: Thread A, Thread B, and Thread C. When the program runs, each thread is managed as a separate entity in the kernel, and the kernel can schedule, switch, and synchronize threads directly. This means that if Thread A blocks, the kernel can still run Thread B or Thread C on another processor.

  • However, creating and switching kernel-level threads is more expensive than user-level threads, since it involves system calls to the kernel. In addition, kernel-level threads may have more overhead since they are managed by the kernel.

In summary, user-level threads are managed by the program itself, while kernel-level threads are managed by the operating system kernel. User-level threads are lightweight and fast, but may have limitations, while kernel-level threads are more powerful but also more expensive to create and switch.

Did you find this article valuable?

Support Gaurav-Jethuri by becoming a sponsor. Any amount is appreciated!