Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does linux user level (pthread) thread run on multiple cores?

I know there are :

1) User level thread - Inside the same address space of the process but with different stacks.

2) Kernel level thread - Inside the kernel memory stack (I am guessing here).

So When I create user level threads, Kernels are not aware of them [1]. So how does the kernel know, how to schedule the different user level threads in different cores. This question is regards to pthread. If pthread is user level thread, how can it run on multiple core?

Future Answer Seeker read : ( thank you to all who contributed )

1) Ziffusion's answer (below)

2) David Schwartz's answer

3) Tutorial point link

like image 477
pokche Avatar asked Jan 20 '16 00:01

pokche


People also ask

Can user threads run on different cores?

In short: yes, a thread can run on different cores. Not at the same time, of course - it's only one thread of execution — but it could execute on core C0 at time T0, and then on core C1 at time T1.

Do multiple threads run on different cores?

Yes, threads and processes can run concurrently on multi-core CPUs, so this works as you describe (regardless of how you create those threads and processes, OpenMP or otherwise). A single process or thread only runs on a single core at a time.

Can we run a multi threaded program user level threads on multiple processors in parallel explain?

Only one thread can access the Kernel at a time, so multiple threads are unable to run in parallel on multiprocessors. If the user-level thread libraries are implemented in the operating system in such a way that the system does not support them, then the Kernel threads use the many-to-one relationship modes.

Is Pthread a user level thread?

Pthreads are implemented as user threads by the runtime library. Most portable implementation since it requires no kernel support. Fast context switches between user threads because it is handled entirely in user space without the overhead of the kernel implementing a process-level context switch.


1 Answers

So When I create user level threads, Kernels are not aware of them

This is not entirely true. At least not in the way you are thinking.

A user space thread library can choose to implement threads that do not map one to one to kernel threads - BUT - each one of these user space threads runs on a kernel thread (when it does run). This just means that the library can do it's own scheduling in user space, and decide to map the user space threads (ones it deems ready to run) to kernel threads from a pool. In this sense, the kernel is not aware of the user space threads - BUT - it is very much aware of the kernel threads that are used to run these user space threads.

The kernel manages and schedules the kernel threads. It can run them on multiple CPUs if it so decides. In doing so, it causes the user space threads mapped to such kernel threads to run on these CPUs as well.

This can be seen in many systems in fact. Threads in Java or Python, greenlets in Python, goroutines in golang - all use this mechanism.

Pthreads used to be like this too, but their implementation was changed to map each pthread to a dedicated kernel thread. But it is quite possible for a pthreads implementation to still use the user space thread model.

There is another model where user space threads can be completely a user space abstraction, which the kernel is entirely unaware of. For example, it is possible to use setcontext() and getcontext() to implement user space threads that live inside a single process.

like image 108
Ziffusion Avatar answered Sep 20 '22 23:09

Ziffusion