Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does linux schedule a process or a thread?

After reading this SO question I got a few doubts. Please help in understanding.

Scheduling involves deciding when to run a process and for what quantum of time.

  1. Does linux kernel schedule a thread or a process? As process and thread are not differentiated inside kernel how a scheduler treats them?

  2. How quantum for each thread is decided? a. If a quantum of time (say 100us) is decided for a process is that getting shared between all the threads of the process? or b. A quantum for each thread is decided by the scheduler?

Note: Questions 1 and 2 are related and may look the same but just wanted to be clear on how things are working posted them both here.

like image 466
prasannatsm Avatar asked Mar 24 '13 16:03

prasannatsm


People also ask

Are threads or processes scheduled?

Threads are scheduled for execution based on their priority. Even though threads are executing within the runtime, all threads are assigned processor time slices by the operating system. The details of the scheduling algorithm used to determine the order in which threads are executed varies with each operating system.

Is a Linux process a thread?

Differences Between Process and Thread A process is heavyweight. A thread is a lightweight process also called an LWP. A process has its own memory. A thread shares the memory with the parent process and other threads within the process.

Is Linux scheduler a process?

Process Scheduler uses Scheduling Algorithms that helps in deciding the process to be executed. In LINUX, there are two types of processes namely - Real-time processes and Normal processes. O(n) scheduler divides the processor's time into a unit called epochs. Each task is allowed to use at max 1 epoch.

How does Linux scheduling work?

A scheduler chooses the next task to be run, and maintains the order, which all the processes on the system should be run in, as well. In the same way as most operating systems out there, Linux implements preemptive multitasking. Meaning, the scheduler decides when one process ceases running and the other begins.


1 Answers

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

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

Basile Starynkevitch