5

I read wikipedia about process scheduler:

The process scheduler is a part of the operating system that decides which process runs at a certain point in time.

I don't really understand what that means. What is a process scheduler in Linux? Is it part of the kernel? What exactly does it do?

2

1 Answer 1

7

In Linux, the process scheduler is a core part of the kernel. It doesn’t exist as a separate thread, or module, it’s implemented as a function, __schedule().

The job of the process scheduler is to decide which process should run next. Each processor in the system has a runqueue, which is the list of processes which are waiting to run on the CPU. When it’s invoked, the scheduler looks at this list of processes, and decides which one to run next; this can be the process (or rather, thread) which was running previously, or it could be another one. Various other systems in the kernel add and remove tasks from the runqueue, or move them from one CPU’s runqueue to another.

The kernel will reschedule processes in a number of circumstances: whenever a process is blocked (by a semaphore, mutex etc.), and whenever a reschedule has been requested and the system transitions from userspace to the kernel or back. The timer tick doesn’t directly invoke the scheduler, it requests a reschedule.

4
  • Hi Stephen, when you say "this can be the process (or rather, thread)" do you mean that it is threads that are actually scheduled instead of processes? Classical literature of operating systems always talk about processes, so that confuses me a bit Commented Oct 17, 2024 at 11:31
  • 1
    @pochopsp the Linux kernel schedules threads, yes. This Q&A might clarify things for you. Commented Oct 17, 2024 at 11:46
  • thanks, now it looks more clear. I have a follow-up question: does all the stuff that classical literature say about scheduling (like short/mid/long term scheduling or algorithms RR / FCFS) stand for threads too (since you said that is threads what is actually scheduled) ? Commented Oct 17, 2024 at 14:42
  • 1
    Not everything, no. Linux has its implementation of threads, which is just one possible implementation. Even on Linux you’ll find other thread schedulers, for example in some language runtimes with “lightweight” (aka “green”) threads; but those don’t change the main scheduler or how pids/tids are defined. Then there’s sched_ext which allows Linux thread schedulers written as BPF programs to be loaded at runtime. Commented Oct 17, 2024 at 15:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.