1
votes

E.g, I've got 4 process on linux and each contaisn 5 pthread threads, and my cpu is i7 4C8H.

question

(1) For each process, does it restrict all its threads to consume just 1 cpu thread? Could one process use all the cpu Core/threads?

(2) Does linux kernel allocate time slice for each process equally(if they're all busy, and dont' specify and priority), or allocate time slice per their thread number, the more thread of a process, the more time slice?

2

2 Answers

2
votes

1) One process could easily use all the cores/hyperthreads available on your processor, by simply spawning multiple threads or processes and making them perform any kind of computation (for example, an infinite loop). The kernel's scheduler will typically try to spread the load on all the available resources

2) I am not aware of a mechanism that allocates more time to a process that has more threads. I believe that each thread will be considered by the scheduler as a separate runnable entity, and will take part of the standard time slicing dance. In particular, the Linux's current scheduler (CFS - Completely Fair Scheduler) tries very hard to allocate time slices fairly among all processes/threads

2
votes

1) A process's threads can run in parallel in multiple CPUs/cores. Multi-threading wouldn't be nearly so useful if only one thread could run at a time! By default the kernel can and will schedule a thread on any available core. Other threads, even if part of the same process, running on another core makes no difference.

There is a cpuset system where you can lock threads to some subset of available cores. You could require a all threads of a process to run on a certain core(s). See pthread_getaffinity_np().

2) The thread, not the process, is the basic scheduling entity in Linux. So yes, processes with more threads will get a bigger share of CPU! What you are asking about is PTHREAD_SCOPE_SYSTEM vs PTHREAD_SCOPE_PROCESS See pthread_attr_setscope(). Linux threads (NPTL) does not support the latter. Also see pthread_setconcurrency(), which also does not do anything on Linux, since Linux is 1:1.

However, Linux does support a concept called "group scheduling." Look into that as a way to achieve fairness between processes with differing number of threads.