Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the linux scheduler needs to be context switched?

I have a general question about the linux scheduler and some other similar kernel system calls.

Is the linux scheduler considered a "process" and every call to the scheduler requires a context switch like its just another process?

Say we have a clock tick which interrupts the current running user mode process, and we now have to call the scheduler. Does the call to the scheduler itself provokes a context switch? Does the scheduler has its own set of registers and U-area and whatnot which it has to restore at every call?

And the said question applies to many other system calls. Do kernel processes behave like regular processes in regard to context switching, the only difference is that they have more permissions and access to the cpu?

I ask this because context switch overhead is expensive. And it sounds odd that calling the scheduler itself provokes a context switch to restore the scheduler state, and after that the scheduler calls another process to run and again another context switch.

like image 253
Jenny B Avatar asked May 31 '12 18:05

Jenny B


People also ask

How does scheduler work in Linux?

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.

What is context switching in Linux?

A context switch is described as the kernel suspending execution of one process on the CPU and resuming execution of some other process that had previously been suspended. A context switch is required for every interrupt and every task that the scheduler picks.

Which is the situation under which context switching will not be done?

You can't directly switch a process from the running state to the ready state. You have to save the context of that process. If you are not saving the context of any process P then after some time, when the process P comes in the CPU for execution again, then the process will start executing from starting.

In which of these situations a context switch of a process happens?

A context switch occurs when a computer's CPU switches from one process or thread to a different process or thread. Context switching allows for one CPU to handle numerous processes or threads without the need for additional processors.


2 Answers

That's a very good question, and the answer to it would be "yes" except for the fact that the hardware is aware of the concept of an OS and task scheduler.

In the hardware, you'll find registers that are restricted to "supervisor" mode. Without going into too much detail about the internal CPU architecture, there's a copy of the basic program execution registers for "user mode" and "supervisor mode," the latter of which can only be accessed by the OS itself (via a flag in a control register that the kernel sets which says whether or not the kernel or a user mode application is currently running).

So the "context switch" you speak of is the process of swapping/resetting the user mode registers (instruction register, stack pointer register, etc.) etc. but the system registers don't need to be swapped out because they're stored apart from the user ones.

For instance, the user mode stack in x86 is USP - A7, whereas the supervisor mode stack is SSP - A7. So the kernel itself (which contains the task scheduler) would use the supervisor mode stack and other supervisor mode registers to run itself, setting the supervisor mode flag to 1 when it's running, then perform a context switch on the user mode hardware to swap between apps and setting the supervisor mode flag to 0.

But prior to the idea of OSes and task scheduling, if you wanted to do a multitasking system then you'd have had to use the basic concept that you outlined in your question: use a hardware interrupt to call the task scheduler every x cycles, then swap out the app for the task scheduler, then swap in the new app. But in most cases the timer interrupt would be your actual task scheduler itself and it would have been heavily optimized to make it less of a context switch and more of a simple interrupt handler routine.

like image 183
Mahmoud Al-Qudsi Avatar answered Oct 04 '22 02:10

Mahmoud Al-Qudsi


Actually you can check the code for the schedule() function in kernel/sched.c. It is admirably well-written and should answer most of your question.

But bottom-line is that the Linux scheduler is invoked by calling schedule(), which does the job using the context of its caller. Thus there is no dedicated "scheduler" process. This would make things more difficult actually - if the scheduler was a process, it would also have to schedule itself!

When schedule() is invoked explicitly, it just switches the contexts of the caller thread A with the one of the selected runnable thread B such as it will return into B (by restoring register values and stack pointers, the return address of schedule() will become the one of B instead of A).

like image 22
Gnurou Avatar answered Oct 04 '22 00:10

Gnurou