Kernel-level thread and user-level thread, kernel-level thread user

Source: Internet
Author: User

Kernel-level thread and user-level thread, kernel-level thread user

When I wrote this blog these two days, I reviewed the operating system materials and encountered a problem that I did not understand before, that is, about kernel-level threads and user-level threads. After reading some documents, I will share my personal profile.

Threads have been implemented in many systems. By then, the implementation methods of various operating systems are different.
In some systems, for example, some database management systems, such as IBM infomix systems, implement user-level Threads (ULT ); other systems, such as Mac OS's predecessor Macintosh and OS/2, implement Kernel-Supported threads (KST) and Solaris operating systems, the two types of threads are implemented at the same time.


KST:

The kernel supports the implementation of threads in the core space. The kernel sets a thread control block for each thread in the core space, the information used to register the thread identifier, register value, status, priority, and other information. All operations on the thread, such as creation, undo, and switch, all are completed by the corresponding handler in the kernel through system function calls. The system that sets the kernel to support threads is scheduled in the thread unit.

Advantages:
In a multi-processor system, the kernel can schedule multiple threads in the same process to run in parallel to multiple processors. If one thread in the process is blocked, the kernel can schedule another thread in the same process. The kernel supports a thread with a small data structure and stack, which leads to fast thread switching and low overhead; the kernel itself can also be implemented using multiple threads.
Disadvantages:
Even if the CPU switches between multiple threads of the same process, it also needs to fall into the kernel, so its speed and efficiency are not as good as that of user-level threads.


ULT:

User-level threads exist only in user space and have nothing to do with the kernel. In terms of the kernel, it only manages conventional processes-single-threaded processes, but does not perceive the existence of user-level threads; each thread control block is set in the user space, and all operations on the thread are also completed by functions in the thread library in the user space, without the help of the kernel; if a user-level thread is set for a system, the scheduling is still in process units.
Advantages:
Thread switching does not need to fall into the kernel, so the switching overhead is small and the speed is very fast. The scheduling algorithm of the thread Library to the user thread is irrelevant to the OS scheduling algorithm. Therefore, the thread Library provides various scheduling algorithms for application selection. user-level thread implementation is not related to the operating system platform.
Disadvantages:
Blocking of system calls: For applications, blocking of one thread will lead to blocking of all threads in the whole process. multi-threaded applications cannot enjoy the benefits of multiple processors in a multi-processor system.

Combination Method:

The kernel supports the establishment, scheduling, and management of multiple KST threads. At the same time, user applications are allowed to create, schedule, and manage user-level threads. Some Kernel support threads correspond to multiple user-level threads, programmers can adjust the number of threads supported by the kernel based on application needs and machine configurations to achieve better results.
Advantages:
Multiple threads in the same process can be executed concurrently on the multi-processor at the same time. When blocking a thread, the whole process does not need to be blocked.


Comparison:

(1) Kernel support
User-level threads can be implemented in an OS that does not support threads;
The kernel supports threads and requires OS Kernel support.
(2) processor allocation
In a multi-processor environment, for user-level threads, the kernel allocates only one processor to one process at a time, and the process cannot enjoy the benefits of the multi-processor;
When a kernel supports threads is set, the kernel can schedule multiple threads in an application to run concurrently on multiple processors, improving the program execution speed and efficiency.

(3) scheduling and thread execution time
The scheduling method and algorithm of a system with Kernel support threads are similar to that of a process, except that the scheduling unit is a thread;
For a system that only sets user-level threads, the scheduling unit is still process.
Therefore, when the conditions are the same, the threads supported by the kernel usually get more CPU execution time than the user-level threads.
(4) switching speed
User-level thread switching usually occurs between various threads of an application, instead of the kernel, and the switching rules are also very simple, the switching speed is at least one order of magnitude faster than the Kernel support thread.

(5) system call
In typical OS, many system calls may cause blocking. When a user-level thread executes these system calls, the entire process is blocked. When a kernel supports threads to execute these system calls, the kernel only blocks this thread, however, other threads can still schedule the execution of the process to which it belongs.


Many of them are copied from the CC ppt. I hope it will help!



Computer Operating System Problems: What are the differences between user-level threads and kernel-level threads?

A kernel-level thread is a thread that implements, manages, and schedules the operating system kernel. Because of the operating system management, the operating system knows the existence of threads and schedules time slices for them to manage kernel objects related to them. Because kernel-level threads are managed by the kernel, a mode switching routine must be executed for each thread creation and switching. Therefore, kernel-level threads are less efficient, in addition, kernel-level thread scheduling is determined by the operating system designer, so there is a lack of flexibility. However, a kernel-level thread is blocked when a thread of a process is blocked due to a system call or page disconnection.

Advantages of kernel-level threads: good parallel capability. Blocking of threads in a process does not affect other threads in the process.
Disadvantages of kernel-level threads: excessive thread management overhead and lack of flexibility.

User-level threads are managed by running in user-mode runtime libraries. The advantage is that all threads (including scheduling and creation) can be decided by the user, therefore, it has high flexibility. In addition, because the management is in the user mode, it saves the kernel management overhead, so it is highly efficient. However, a user-level thread has a fatal drawback: blocking of a thread in a process will cause all threads in the whole process to be blocked. Because user-level threads do not have the concept of time slice, each thread must use the CPU for another thread after running for a period of time. Otherwise, the thread will exclusively occupy the CPU.

Advantages of user-level threads: high flexibility and efficiency
User-level thread disadvantages: Poor concurrency

Because both threads cannot meet users' needs, some researchers began to study new thread systems, A good thread system is the thread implementation method of the scheduling program activation mechanism proposed by several foreign researchers in 1993.

This method is a thread between a kernel-level thread and a user-level thread. It also has the advantages of two threads. To learn more

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.