Advertisements
Advertisements
Question
Explain context switching at process level in multiprogramming system with example.
Explain
Advertisements
Solution
- Multiprogramming is the concept of expanding the use of a CPU by always having something for it to do.
- In multiprogramming, the CPU can run two or more processes at the same time. When process 1 is waiting for an external event, such as an I/O operation, the CPU runs process 2, and vice versa.
- Context switching refers to the time it takes for the CPU to switch from one process to another.

- Let A and B be the two processes that are ready for execution and require CPU time to complete. Allow CPU time to be allocated to process A, which contains instructions that depend on process B or on an external event, such as I/O activity. The operating system is then responsible for halting the execution of process A and allocating the CPU time to process B. Context switching refers to the time it takes for the CPU to shift its attention from process A to B.
- During context switching, the CPU’s state is preserved, and the registers and flags from the previous process remain in memory.
- For e.g.,
// A. CPP # include <iostream.h> # include "B.h" void main () { int a = 10, b = 20; cout << add (a,b); } // B.h # include <iostream.h> int add (int x, int y) { return (x+y); }
Here, A and B are the two processes, and A is dependent on B. When process A is executed, the CPU executes each instruction sequentially. The instruction cout << add (a,b); stops execution since its output is dependent on process B’s output.
Thus, process B must be executed. As a result, the CPU stores the contents of process A's registers and flags in the Register Save Area (RSA). It then places process B in memory. The time required for this is called context switching. The CPU then conducts process B, and the output is provided to process A. A resume from the instruction from which it was interrupted.
shaalaa.com
Is there an error in this question or solution?
2021-2022 (March) Set 1
