English

Explain context switching at process level in multiprogramming system with example. - Computer Science 1

Advertisements
Advertisements

Question

Explain context switching at process level in multiprogramming system with example.

Explain
Advertisements

Solution

  1. Multiprogramming is the concept of expanding the use of C.P.U. by always having something for it to do.
  2. 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, C.P.U. runs process 2, and vice versa.
  3. Context switching refers to the amount of time it takes for C.P.U. to switch from one process to another.
  4. Let A and B be the two processes that are ready for execution and require C.P.U. time to complete. Allow CPU time to be allocated to process A, which contains certain instructions that are dependent on process B or on an external event such as an I/O activity. The operating system is then responsible for halting the execution of process A and allocating C.P.U. time to process B. Context switching refers to the time it takes for C.P.U. to shift its attention from process A to B.
  5. During context switching, the status of C.P.U. registers and flags from the previous process remain in memory.
  6. 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 one by one. 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, C.P.U. stores the contents of process A's registers and flags in RSA (Register Save Area). It then places process B in memory. The time necessary for this is referred to as context switching. The CPU then conducts process B, and the output is provided to process A. A resumes from the instruction from which it was interrupted.

shaalaa.com
  Is there an error in this question or solution?
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×