Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between CPU threads, Kernel threads, OS threads, user threads

I've been reading a lot about how parallelism is different than concurrency, but I'm having a lot of questions like how can I achieve a parallelism with a programming language like Java?

What I understood so far is with a VM like Java we can only achieve concurrency and parallelism is not in scope of JVM, it's threads mapped to ( I'm confused here ) OS threads or Kernel threads?

To understand the multi-threading one needs to understand how the threads of a VM ( JVM ) or a process are actually mapped down to the CPU hardware. Please help here.

Correct me if I'm wrong, here are few things I think I understand right:

1. CPU threads ( Intel hyper-threading ): Hardware threads provided by adding more registers in CPU having same ALU, so do not actually behave as a dual-CPU ( if 2 threads are there in CPU ) and a "just"-concurrent use of ALU can just mask ( hide by an interleaved MUXing ) a principal inability to deliver indeed a true parallel code-execution.

2. Kernel threads: Started by Kernel and these can be more in number than CPU threads but are a bit expensive to start.

3. OS threads: I don't understand it. I'm guessing this is what you mean when you call native threads and probably same as previous #2 ie Kernel threads.

4. User threads: Just like new Thread() in JVM or .NET. I think they are not recognized as threads by OS so nothing much can be done here from the parallelism perspective.

I need to understand how threads created in #4 are mapped to #1, may be with examples to Windows/Linux/MacOS.

Guys, I understand how wide this question can be but I can live with brief info and link to any detailed documentation. Thanks in advance.

like image 533
Sachin Verma Avatar asked Oct 07 '17 10:10

Sachin Verma


People also ask

What is the difference between CPU thread and OS thread?

Threads are individual sequences of instructions. Old/small CPUs only do one instruction at a time hence support “single threaded” execution, newer multicore CPUs can handle multiple threads executing at the same time.

What is difference between user thread and kernel thread?

Difference between User-Level & Kernel-Level Thread User-level threads are faster to create and manage. Kernel-level threads are slower to create and manage. Implementation is by a thread library at the user level. Operating system supports creation of Kernel threads.

What are kernel threads in OS?

A kernel thread is a kernel entity, like processes and interrupt handlers; it is the entity handled by the system scheduler. A kernel thread runs within a process, but can be referenced by any other thread in the system.

What are the different types of threads in operating system?

There are two types of threads to be managed in a modern system: User threads and kernel threads.


1 Answers

1. Intel Hyperthreading - It is a technology developed by Intel to improve the efficiency of one core, because in a lot of its execution time, the CPU waits for retrieving data & code from RAM. The operating system views hyperthreading-cores as two separate logical processors, and each contains separate registers.

2. Kernel Threads - This are managed by the OS-kernel and are scheduled in kernel-mode only. They are attached to registers & EIP along with some attributes. These are created and managed by the process by using some "defined" interfaces by the kernel.

3. User Threads - These are created by the userspace process are executed on a kernel thread (because only a kernel one can be scheduled). But multiple user-threads can take turns for executing on a kernel thread, and giving higher performance for 'temporary' threads that go & come, due to time saved by not doing a system call.

4. OS Threads - OS threads don't have a proper definition in my knowledge. They can be threads used by the OS, or just kernel threads.

There are multiple models by which user-threads execute on kernel-threads - one-to-one, m-to-n & all-to-one, you can google these.

like image 156
Shukant Pal Avatar answered Oct 26 '22 07:10

Shukant Pal