Threads: Why must all user threads be mapped to a kernel thread?

A kernel thread is a thread object maintained by the operating system. It is an actual thread that is capable of being scheduled and executed by the processor. Typically, kernel threads are heavyweight objects with permissions settings, priorities, etc. The kernel thread scheduler is in charge of scheduling kernel threads.

User programs can make their own thread schedulers too. They can make their own “threads” and simulate context-switches to switch between them. However, these threads aren’t kernel threads. Each user thread can’t actually run on its own, and the only way for a user thread to run is if a kernel thread is actually told to execute the code contained in a user thread. That said, user threads have major advantages over kernel threads. They can be a lot more lightweight, since they don’t necessarily need to have their own priorities, can be managed by a single process (which might have better info about what threads need to run when), and don’t create lots of kernel objects for purposes of security and locking.

The reason that user threads have to be associated with kernel threads is that by itself a user thread is just a bunch of data in a user program. Kernel threads are the real threads in the system, so for a user thread to make progress the user program has to have its scheduler take a user thread and then run it on a kernel thread. The mapping between user threads and kernel threads doesn’t have to be one-to-one (1 : 1); you can have multiple user threads share the same kernel thread (only one of those user threads runs at a time), and you can have a single user thread which is rotated across different kernel threads in a 1 : n mapping.

Leave a Comment