5

I am new to this OS stuff. Since the kernel controls the execution of all other programs and the resources they need, I think it should also be executed by the CPU. If so, where does it gets executed? and if i think that what CPU should execute is controlled by the kernel, then how does kernel controls the CPU if the CPU is executing the kernel itself!!!..

It seems like a paradox for me... plz explain... and by the way i didn't get these CPU modes at all... if kernel is controlling all the processes... why are these CPU modes then? if they are there, then are they implemented by the software(OS) or the hardware itself??

thanq...

nitish712
  • 19,504
  • 5
  • 26
  • 34

2 Answers2

5

A quick answer. On platforms like x86, the kernel has full control of the CPU's interrupt and context-switching abilities. So, although the kernel is not running most of the time, every so often it has a chance to decide which program the CPU will switch to and allow some running for that program. This part of the kernel is called the scheduler. Other than that the kernel gets a chance to execute every time a program makes a system call (such as a request to access some hardware, e.g. disk drive, etc.)

P.S The fact that the kernel can stop a running program, seize control of the CPU and schedule a different program is called preemptive multitasking

UPDATE: About CPU modes, I assume you mean the x86-style rings? These are permission levels on the CPU for currently executing code, allowing the CPU to decide whether the program that is currently running is "the kernel" and can do whatever it wants, or perhaps it is a lower-permission-level program that cannot do certain things (such as force a context switch or fiddle with virtual memory)

sinelaw
  • 16,205
  • 3
  • 49
  • 80
  • I got you, but where does these cpu modes come into play? are these modes controlled by the kernel itself? in the first place, why are these neede actually? – nitish712 Aug 10 '12 at 13:15
  • So, you say that the CPU changes its modes very frequently, based on the instructions which it is executing? Correct me if i am wrong... – nitish712 Aug 10 '12 at 13:21
  • It depends a lot on the type of the CPU and OS. Here's a related answer regarding x86 specifically: http://stackoverflow.com/a/2484895/562906 – sinelaw Aug 10 '12 at 13:27
  • One small doubt again... if scheduler tells the processor which process to execute... then who executes the scheduler?????(after all scheduler is also a program right?) Does all these user-process execution, kernel execution and scheduler execution.. all are done so fastly by switching of the processor??? – nitish712 Aug 10 '12 at 13:39
  • Computers have a hardware timer that can send signals to the CPU in the form of interrupts. During kernel setup this hardware timer is programmed to send a periodic signal and the CPU interrupt vectors are set up to map to the kernel code that does periodic activity, the most important one being deciding if the currently running process shall continue to run or be suspended and another process be activated instead. – Analog File Aug 10 '12 at 14:02
  • @nitishrao, yes, the scheduler is also a program and it executes on the very same CPU. The CPU switches from running one program to another, and the kernel is the only "program" that can control how and when this happens. – sinelaw Aug 10 '12 at 14:30
2

There is no paradox:

The kernel is a "program" that runs on the machine it controls. It is loaded by the boot loader at the startup of the machine.

Its task is to provide services to applications and control applications. To do so, it must control the machine that it is running on.

For details, read here: http://en.wikipedia.org/wiki/Operating_System

Black
  • 5,022
  • 2
  • 22
  • 37