who is running kernel if cpu is running processes?

Suppose in a two process environment, one process is scheduled for execution by the kernel, and it demanded for some data which is not available in the RAM. So the cpu will indicate the kernel that something is not available and the process will be suspended. Then after kernel loads the second process for execution through the CPU and start investigating about the data in secondary memory location (say virtual memory) and gets it, puts it back to main memory by a swap to the memory data which is currently inactive, and puts the process back in the ready queue for execution.

We know that everything in computer system is get manipulated by CPU only and if CPU is busy executing continuously the process code then who is executing the kernel code to perform the tasks done by kernel? Please let me know if i am able to explain the scenario.


At any point in time, CPU (/s) will be

  • Running a process in User Mode.
  • Running on behalf of a process in Kernel Mode to execute previleged instruction or access hardware (for example when system call read / write is issued).
  • Running in repsonse to a hardware interrupt. ie running in interrupt context. (Not associated with any process in particular) and yes in kernel mode.
  • Running some kernel threads to serve deferred work like soft irq. (Tasklet / Softirq)
  • Running CPU idle thread if nothing is there to execute.
  • If you are in particular asking about scheduling, then

  • Suppose a process is running and now it has issued a read call to retrieve data from hard disk, say, then process is removed from cpu and kernel invokes schedule() functions. So here, first process issues read system call, which results in switching from user mode to kernel mode. The kernel which is running on behalf of the process prepares for the hard disk read operation and then calls schedule() function
  • Suppose a hardware interrupt has come, then currently running process is removed, and interrupt service handler for that interrupt begins to execute in kernel mode (obviously).
  • Basically, kernel runs in between user processes !!

    Clear now ?

    Shash


    The kernel runs either as a result of a hardware interrupt, or as a result of being invoked by a process to do something. In both cases the code which was executing at that moment stops running until the kernel finishes its job.

    It is similar to a function call: when function A calls function B, function A has to wait until function B is done doing what it does, and returns control to function A. You do not need multiple CPUs, or any kind of magic to accomplish this.


    The CPU is not continuously executing process code. The CPU is interrupted to perform various operations. Interrupts can occur for various reasons: a resource becomes available, a previous action completes, or simply a timer goes off.

    I recommend this series of videos for more in-depth information: http://academicearth.org/courses/operating-systems-and-system-programming

    链接地址: http://www.djcxy.com/p/79822.html

    上一篇: 测量子进程的CPU和RAM使用情况

    下一篇: 如果cpu正在运行进程,谁在运行内核?