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?
Linux, like all Unix variants and most modern operating systems, provides preemptive multitasking. In preemptive multitasking, the scheduler decides when a process is to cease running and a new process is to resume running. The act of involuntarily suspending a running process is called preemption.
Under some operating systems, the thread with the highest priority (of those threads that can be executed) is always scheduled to run first. If multiple threads with the same priority are all available, the scheduler cycles through the threads at that priority, giving each thread a fixed time slice in which to execute.
To the Linux kernel, there is no concept of a thread. Linux implements all threads as standard processes. The Linux kernel does not provide any special scheduling semantics or data structures to represent threads. Instead, a thread is merely a process that shares certain resources with other processes.
Process scheduler is responsible for scheduling the threads in a multithreading | Course Hero.
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.
Kernel threads under Linux are implemented as processes that share resources. The scheduler does not differentiate between a thread and a process
See here for more information: http://www.linuxquestions.org/linux/articles/Technical/Linux_Kernel_Thread
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With