How Linux handles threads and process scheduling

whitefox picture whitefox · Dec 11, 2011 · Viewed 36.5k times · Source

I'm trying to understand how Linux handles process scheduling and thread scheduling. I read that Linux can schedule both processes and threads.

Does Linux have a thread scheduler AND a process scheduler? If yes, how do they cooperate?

Answer

Basile Starynkevitch picture Basile Starynkevitch · Dec 11, 2011

The Linux kernel scheduler is actually scheduling tasks, and these are either threads or (single-threaded) processes.

So a task (a task_struct inside the kernel), in the context of the scheduler, is the thing being scheduled, and can be some kernel thread like kworker or kswapd, some user thread of a multi-threaded process (like firefox), or the single-thread of a single-threaded process (like bash), identified with that single-threaded process.

A process is a non-empty finite set (sometimes a singleton) of threads sharing the same virtual address space (and other things like file descriptors, working directory, etc etc...). See also credentials(7), capabilities(7) etc....

Threads on Linux are kernel threads (in the sense of being managed by the kernel, which also creates its own threads), created by the Linux specific clone syscall (which can also be used to create processes on Linux). The pthread_create function is probably built (on Linux) above clone inside NPTL and Gnu Libc (which integrated NPTL on Linux) and musl-libc.