How does OS scheduler regains control of CPU?
I recently started to learn how the CPU and the operating system works, and I am bit confused about the operation of a single-CPU machine with an operating system that provides multitasking.
As such, supposing my machine has a single CPU, this would mean that, at any given time, only one process could be running.
Now, I can only assume that the scheduler used by the operating system to control the access to the precious CPU time is also a process.
Thus, in this machine, either the user process or the scheduling system process is running at any given point in time, but not both.
So here's a question:
Once the scheduler gives up control of the CPU to another process, how can it regain CPU time to run itself again to do its scheduling work? I mean, if any given process currently running does not relinquish (yield) the CPU, how could the scheduler itself ever run again and ensure proper multitasking?
So far, I had been thinking, well, if the user process requests an I/O operation through a system call, then in the system call we could ensure the scheduler is allocated some CPU time again. But I am not even sure if this works in this way.
On the other hand, if the user process in question were inherently CPU-bound, then, from this point of view, it could run forever, never letting other processes, not even the scheduler run again.
Supposing a time-sliced scheduling, I have no idea how the scheduler could slice the time for the execution of another process, when it is not even running?
I would really appreciate any insight or references that you can provide in this regard.
The OS sets up a hardware timer (Programmable interval timer or PIT) that generate an interrupt every N milliseconds. That interrupt is delivered to the kernel and user-code is interrupted.
It works like any other hardware interrupt. For example your disk will force a switch to the kernel when it has completed an IO.
Google 'interrupts'. Interrupts are at the centre of multithreading, preemptive kernels like Linux/Windows. With no interrupts, the OS will never do anything.
While investigating/learning, Try to ignore any explanations that mention 'timer interrupt', 'round-robin' and 'time-slice', 'quantum' in the first paragraph - they are dangerously misleading, if not actually wrong.
Interrupts, in OS terms, come in two flavours:
Hardware interrupts - those initiated by an actual hardware signal from a peripheral device. These can happen at, (nearly) any time and switch execution from whatever thread might be running to code in a driver.
Software interrupts - those initiated by OS calls from currently running threads.
Either interrupt may request the scheduler to make threads that were waiting ready/running or cause threads that were waiting/running to be preempted.
THE MOST IMPORTANT INTERRUPTS ARE THOSE HARDWARE INTERRUPTS FROM PERIPHERAL DRIVERS, - those that make threads ready that were waiting on IO from disks, NIC cards, mice, keyboards, USB etc etc. The overriding reason for using preemptive kernels, and all the problems of locking, synchronization, signaling etc, is that such systems have VERY GOOD IO PERFORMANCE BECAUSE HARDWARE PERIPHERALS CAN RAPIDLY MAKE THREADS READY/RUNNING THAT WARE WAITING FOR DATA FROM THAT HARDWARE, WITHOUT ANY LATENCY RESULTING FROM THREADS THAT DO NOT YIELD OR WAITING FOR A PERIODIC TIMER RESCHEDULE.
The hardware timer interrupt that causes periodic scheduling runs is important because many system calls have timeouts in case, say, a response from a peripheral takes longer than it should.
On multicore systems, the OS has an interprocessor driver that can cause a hardware interrupt on other cores, so allowing the OS to interrupt/schedule/dispatch threads onto multiple cores.
On seriously overloaded boxes, or those running CPU-intensive apps, (a small minority), the OS can use the periodic timer interrupts, and so resulting scheduling, to cycle through a set of ready threads that is larger than the number of available cores and so allow each a share of available CPU resources. On most systems, this happens rarely and is of little importance.
I'm sorry about the shouting, but every time I see 'quantum', 'give up the remainder of their time-slice', 'round-robin' and similar, I just cringe..
To complement @usr's answer, quoting from Understanding the Linux Kernel:
The schedule( ) Function
schedule( ) implements the scheduler. Its objective is to find a process in the runqueue list and then assign the CPU to it. It is invoked, directly or in a lazy way, by several kernel routines. [...]
Lazy invocation
The scheduler can also be invoked in a lazy way by setting the need_resched field of current [process] to 1. Since a check on the value of this field is always made before resuming the execution of a User Mode process (see the section "Returning from Interrupts and Exceptions" in Chapter 4), schedule( ) will definitely be invoked at some close future time.
链接地址: http://www.djcxy.com/p/43614.html上一篇: 抢占和上下文切换之间的区别