33

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?

codeforester
  • 39,467
  • 16
  • 112
  • 140
whitefox
  • 955
  • 2
  • 10
  • 7
  • It depends on the type of threads you'd like to use. Which thread library are you using? – pajton Dec 11 '11 at 11:55
  • I'm speaking in general , about kernel threads not user threads. I don't know what's a thread library ... – whitefox Dec 11 '11 at 11:59

3 Answers3

49

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
26

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

nurio
  • 553
  • 4
  • 10
  • Thanks , that's what I wanted to know. So there is only one scheduler ( which is the process scheduler ) that schedules the "threads" ( which are actually processes that share the same ressources ). Is this a coorect conclusion ? – whitefox Dec 11 '11 at 12:33
  • 2
    I think a better terminology is that the Linux scheduler is scheduling tasks (implemented as `task_struct` inside the kernel), which may be threads or single-threaded processes or kernel-land threads... – Basile Starynkevitch Dec 14 '11 at 08:33
  • 2
    if "The scheduler does not differentiate between a thread and a process" then how the scheduler is switching context between to threads that belong to the same process and between two threads that belong to different processes? Should n't switching between threads that belong to tow different processes done in different way than threads switching? – kachanov Jun 01 '12 at 07:14
  • 1
    @kachanov To my knowledge it can be handled the same way. Each thread has it's own stack and running execution state (registers etc.) just like "normal" processes. A context switch between two threads that belong to the same process just means that the switch might be a bit faster due to shared memory that may be still in the cache. To avoid simultaneus access of the same memory, the threaded programme has to use mutexes or locks - the scheduler does not help in this case. – nurio Jun 12 '12 at 12:50
-4

Under LINUX there is no concept of threads,to make LINUX POSIX complaint thread is nothing but another process.when you try to get a process id it would display the leader process id under any thread. For more details try to refer this book "Understanding LINUX kernel".Hope

kiran
  • 1
  • Fundamentally wrong. There is a difference between processes and threads, with threads being independent flows of excution that runs separately and process encompassing 1 or more threads and its resources. – Владислав Король Aug 19 '23 at 13:56