1

Since user threads are mapped to kernel threads, why don't we create all threads as kernel threads that is a process with zero user thread. Doesn't mapping of user thread to kernel thread create overhead?

1

That is what is typically done. Most modern platforms don't really have user threads any more. You will find two technologies that are similar to user threads, coroutines and strands/fibers. But they're not pre-emptive. So real user threading has pretty much died out in favor of kernel threads.

(To avoid confusion, by "kernel threads", we mean threads that are scheduled by the kernel, not threads that execute in kernel space. By "user threads", we mean threads that are not individually scheduled by the kernel but where a user space scheduler decides which user space thread to run.)

|improve this answer|||||
  • 1
    Disagree that "most modern platforms don't really have user threads any more" -- there are plenty of platforms that have a model where multiple user threads can be mapped to a single kernel thread, including Windows (see user mode scheduling) and Solaris (Solaris threads are user-mode structures mapped to kernel-mode "LWP"s [ie threads], although the mapping is more automatic than as far Windows UMS threads). While neither Linux nor OSX have a similar system, at least in Linux there are user... – Jules Aug 22 '16 at 21:46
  • ... threading libraries that can be used to provide more efficient management of system-level threads. And there are many design patterns (e.g. worker thread pools, work queues, etc) that provide a similar benefit (i.e. multiplexing work into a smaller number of kernel threads than would otherwise be needed). – Jules Aug 22 '16 at 21:47
  • @Jules Those aren't really user mode threads, they're fibers. As I said, they're not pre-emptive. (Think about it -- the whole point is to avoid going to the kernel to change threads. But how can you pre-empt or block non-cooperatively without already being in the kernel?) – David Schwartz Aug 23 '16 at 0:23
  • Both Windows UMS and Solaris threads have a way of converting blocking IO actions to non-blocking, then reentering the user-mode scheduler to continue executing. They effectively multiplex a large number of user threads onto a smaller number of kernel threads. The only scenario they don't handle is CPU-intensive operations that don't explicitly yield, but that's not the most common use case of threads. They are also pretty clearly the kind of user-mode thread the asker was asking about, because otherwise the question doesn't make any sense. – Jules Aug 23 '16 at 8:09
  • Think about this -- Windows NT has had fibers since the very beginning. Why would a new API have been added in Windows 7 to provide similar features if there hadn't been some additional benefit? – Jules Aug 23 '16 at 8:11
1

Why don't we create all threads as kernel thread

What you are suggesting is called the one to one model in literature and it’s mostly used. For example JVM (Solaris) originally used many to one model, it created only one kernel thread for many user threads. But then it switched to the one to one model because of performance issues.

Doesn't mapping of user thread to kernel thread create overhead?

It does. But that overhead is in the user space that is because user thread creation and management is done by some library such as JVM And it does not require much evolvement of kernel (read less system calls). But when we create one kernel thread for each user thread the overhead shifts to the kernel space, because now kernel must create additional data structures for each kernel thread (read clone system call in Linux). Also more kernel threads mean more load on the schedular (depending on the scheduling policy).

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.