Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Linux handles threads and process scheduling

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?

like image 659
whitefox Avatar asked Dec 11 '11 11:12

whitefox


People also ask

How scheduling is handled in Linux system?

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.

How is scheduling handled when threads are used?

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.

How are threads handled in Linux?

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.

Who is responsible for scheduling of threads?

Process scheduler is responsible for scheduling the threads in a multithreading | Course Hero.


2 Answers

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.

like image 109
Basile Starynkevitch Avatar answered Oct 20 '22 22:10

Basile Starynkevitch


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

like image 41
nurio Avatar answered Oct 20 '22 23:10

nurio