Context Switching

  • It is switching the context of the process from user space to kernel space.
  • It involves saving the current context of a process before moving the control of execution to kernel space.
  • The current context of the process that is the current register content, program counter all are saved so that it can be reloaded once the control switches back to the user space program.
  • When programs run, they build up a great deal of state in CPU caches, TLBs. Switching to another job causes this state to be flushed and a new state relevant to the currently-running job to be brought in, which may exact a noticeable performance cost.
  • Context switching is the basis of multiprogramming or multi-tasking.

Before the context is switched to the kernel space, the complete process control block(PCB) which is nothing but a data structure to holds the information’s about the process is saved onto the stack.

Important information present in PCB which are saved onto the stack is as follows:

  • The process state
  • The program counter
  • The values of different registers.
  • The CPU scheduling information.
  • Memory Management information about the process
  • I/O status information.

When the PCB of the currently executing process is saved, the operating system loads the PCB of the next process that has to be run on CPU. This is a heavy task and it takes a lot of time.

Thus having frequent context switching is an overhead and involves the scheduler picking ready jobs to be processed.

Overhead of Context Switching:

Direct Factors:

  • Latency
  • Saving/restoring contexts
  • Finding the next process to execute, calling the scheduler

Indirect Factors:

  • TLB needs to be reloaded.
  • Processors pipeline to be Flushed.

Diag-1: Context Switching

untitled image

Related Posts:



Categories: Operating system (OS)

5 replies

Trackbacks

  1. Thread Introduction - Tech Access Info
  2. Pros and Cons of Thread - Tech Access
  3. Index of Operating System - Tech Access
  4. Process Control Block(PCB) - Tech Access
  5. Characteristics of a Thread - Tech Access

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: