Monday, June 4, 2012

Process, Process States and Context Switch


A process is a execution of a program(program is,  say, an executable file) by CPU and many processes appear to execute simultaneously as the kernel schedules them for execution, and several processes may be instances of one program. The kernel loads an executable file into memory during an exec system call, and the loaded process consists of at least three parts, called regions: text, data and the stack
  • The text region corresponds to program code,
  • The data region corresponds to variables in the code
  •  But the stack region is automatically created and it's size is dynamically adjusted by the kernel at run time. The stack consists of logical stack frames that are pushed when calling a function and popped when returning. Because a process in Unix system can execute in two modes, kernel or user, it uses a separate stack for each mode.
A processor(CPU) can execute only one process at a time and the process my be executing in user mode or kernel mode

Process States

The lifetime of a process can be divided into set of states, each with certain characteristics that describe the process. 

Here is the complete set of process states
  1. The process is currently executing in user mode
  2. The process is currently executing in kernel mode
  3. The process is not executing but ready to run as soon as the kernel(scheduler) schedules it
  4. The process is not executing and ready to run, but the process must be swapped into main memory before the kernel can schedule it to execute.
  5. The process is sleeping and resides in main memory. A process puts itself into sleep when it can no longer continue executing , such as when it is waiting for an I/O to complete. When a process wakes up from sleep state, it is transitioned into "ready to run" state,where it is eligible for later scheduling; it does not execute immediately. Sleeping process do not consume CPU resources.
  6. The process is sleeping, but has been swapped into secondary storage to make room for other processes in main memory.
  7. The process is returning from kernel mode to user mode, but the kernel preempts it and does a context switch to schedule another process.
  8. The process is newly created and is in transition state - the process exists, but it is not ready to run, nor is it sleeping. This state is the start state for all processes.
  9. The process executed the exit system call and is in the zombie state. The process no longer exists, but it has an entry in process table. zombie state is the final state of a process and it consumes no cpu resources.
Context Switch

Context switch occurs when a kernel decides it should execute another process. When doing a context switch from one process to another process, the kernel saves enough information about the current executing process so that it can later switch back to it and resume it's execution.
  • Moving between a user and kernel mode is a change in mode and not a context switch.
  • When a process is executing in kernel mode, context switch to another process cannot occur. Thus, processes running in kernel mode cannot be preempted by other processes.
Kernel permits context switch under four circumstances
  • When a process puts itself into sleep
  • When a process exits
  • When a process returns from kernel mode to user execution mode, but is not the most eligible process to run
  • When a process returns to user mode after kernel completes handling an interrupt(an interrupt is always handled in the context of currently executing process), but is not the most eligible process to run.
Procedure  for context switch
  • Decide whether to do a context switch, and whether a context switch is permissible now.
  • Save the context of "old" process.
  • Find the "best" process to schedule for execution by using process scheduling algorithm and restore it's context.

How to find the context switches happening in the system?

The field "cs" in vmstat command under "system" gives the interrupts being processed in the system.

No comments:

Post a Comment