Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is kernel a special program that executes always? and why are these CPU modes?

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...

like image 797
nitish712 Avatar asked Aug 10 '12 13:08

nitish712


2 Answers

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)

like image 178
sinelaw Avatar answered Oct 08 '22 21:10

sinelaw


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

like image 2
Black Avatar answered Oct 08 '22 21:10

Black