Does linux schedule a process or a thread?
Solution 1:
The Linux scheduler (on recent Linux kernels, e.g. 3.0 at least) is scheduling schedulable tasks or simply tasks.
A task may be :
- a single-threaded process (e.g. created by
fork
without any thread library) - any thread inside a multi-threaded process (including its main thread), in particular Posix threads (pthreads)
- kernel tasks, which are started internally in the kernel and stay in kernel land (e.g.
kworker
,nfsiod
,kjournald
,kauditd
,kswapd
etc etc...)
In other words, threads inside multi-threaded processes are scheduled like non-threaded -i.e. single threaded- processes.
The low-level clone(2) syscall creates user-land schedulable tasks (and can be used both for creating fork
-ed process or for implementation of thread libraries, like pthread). Unless you are a low-level thread library implementor, you don't want to use clone
directly.
AFAIK, for multi-threaded processes, the kernel is (almost) not scheduling the process, but each individual thread inside (including the main thread).
Actually, there is some notion of thread groups and affinity in the scheduling, but I don't know them well
These days, processors have generally more than one core, and each core is running a task (at some given instant) so you do have several tasks running in parallel.
CPU quantum times are given to tasks, not to processes
Solution 2:
The NPTL implementation of POSIX thread specifications sees thread as a different process inside kernel, having unique task_struct
(and therefore pid
too) so each thread is schedulable in itself as mentioned. Therefore each thread gets its own timeslice and is scheduled just like processes as mentioned above.
Just to add, Currently Linux scheduler is also capable of scheduling not only single tasks ( a simple process), but groups of processes or even users ( all processes, belonging to a user) as a whole. This allows implementing of group scheduling, where CPU time is first divided between process groups and then distributed within those groups to single threads.
Linux threads does not directly operate on processes or threads, but works with schedulable entities. Represented by struct sched_entity
.
It's fair to say that every process/thread is a sched_entity
but the converse might not be true.
To know detailed process scheduling, refer here