Kernel-level threads and user-level threads

Source: Internet
Author: User

These two days in writing this blog, incidentally review the operating system information, encountered a previously not understand the problem, is about the kernel-level threads and user-level threads. After reviewing some of the information, I'll publish my personal profile.

Threads have been implemented in many systems, and by that time each operating system was not implemented exactly the same way.
For example, in some systems, special times are some database management systems such as IBM's INFOMIX system, the implementation of user-level threads (UserLevel Threads, ULT), while others such as (MAC OS predecessor Macintosh and os/ 2 operating system) is implemented by kernel support threads (Kernel supported threads, KST), and also some Solaris operating systems, which implement both types of threads.


KST:

Kernel Support threads are implemented in the core space, and the kernel sets up a thread control block in the core space for each thread to register the thread identifier, register value, state, priority, and so on, all operations on the thread, such as Create, undo, and toggle, etc. is done by the corresponding handler in the kernel through the system function call, and the system with kernel support thread is set up in the thread unit.

Advantages:
In multiprocessor systems, the kernel can simultaneously schedule multiple threads in the same process to execute concurrently to multiple processors, and if one thread in the process is blocked, the kernel may dispatch another thread in the same process; The kernel support thread has a small data structure and stack, the thread switches relatively fast, and the switching overhead is small. The kernel itself can also be implemented using multithreading.
Disadvantages:
Even if the CPU switches between multiple threads in the same process, it needs to fall into the kernel, so it is not as fast or efficient as a user-level thread.


ULT:

A user-level thread exists only in user space and is not kernel-independent; As far as the kernel is concerned, it simply manages the regular process-single-threaded process, and does not perceive the existence of user-level threads ; Each thread control block is set in user space, and all operations on the thread are done in user space by functions in the thread library. There is no need for kernel help ; a system with a user-level thread is set, and its dispatch is still in process.
Advantages:
Thread switching does not need to fall into the kernel, so the switching overhead is small, the speed is very fast ; the thread libraries is independent of the scheduling algorithm of the OS, so the line libraries can provide a variety of scheduling algorithms for the application selection ; the implementation of the user-level thread is independent of the operating system platform.
Disadvantages:
Blocking issues with system calls: for applications, blocking of one thread will cause all threads in the process to block, and multithreaded applications cannot enjoy the benefits of multiple processors in multiprocessor system.

combination mode:

The kernel supports the establishment, scheduling, and management of multi-KST threads, while also allowing user applications to establish, schedule, and manage user-level threads ; some kernel support threads correspond to multiple user-level threads, and programmers can adjust the number of kernel support threads according to application needs and machine configuration to achieve better results.
Advantages:
Multiple threads within the same process can execute concurrently on multiple processors, and there is no need to block the entire process when blocking a single thread .


Compare the two:

(1) Kernel support
User-level threads can be implemented in an OS that does not support threading;
Kernel support threads need to be supported by the OS kernel.
(2) Processor assignment
In multiprocessor environments, for user-level threading, the kernel assigns only one processor to a process at a time, and the process does not enjoy the benefits of multiprocessor;
When a kernel support thread is set, the kernel can schedule multiple threads in one application to run concurrently on multiple processors, improving program execution speed and efficiency.

(3) Scheduling and thread execution time
Set up a system with kernel support thread, its scheduling method and algorithm and process scheduling is very similar, but the scheduling unit is a thread;
For a system that only has a user-level thread set, the dispatched unit is still a process.
Therefore, in the same condition, the kernel-supported threads usually get more CPU execution time than the user-level thread.
(4) switching speed
User-level thread switching typically occurs between threads of an application without having to fall into the kernel, and the rules for switching are simple, with switching speeds at least one order of magnitude faster than kernel support threads.

(5) System call
In a typical OS, many system calls can cause blocking. When a user-level thread executes these system calls, the entire process is blocked, and when a kernel support thread executes these system calls, the kernel blocks only that thread, but it can still dispatch other threads of the process it belongs to.


Many are copied from cc ppt, hope will help!


Kernel-level threads and user-level threads

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.