0
2.8kviews
Context Switch in UNIX process
1 Answer
0
50views

There are 4 situations under which kernel permits a context switch are :

  • When a process is in sleep mode
  • When a process goes to exit
  • When a process returns from a system call to user mode but is not the most eligible process to run
  • When a process returns from an interrupt handler to user mode but is not the most eligible process to run

The process followed for a context switch is almost the same as that of a system calls and interrupts. The only difference is that the context layer of a particular process is restored instead of the previous layer of the same process. The choice of which process to restore is a policy decision and it is independent of the mechanism of the context switch.

Implementation of the context switch on UNIX systems is usually somewhat complicated to understand in the operating system because function calls give the appearance of not returning on some occasions and materializing from nowhere on the others.

This is because of the kernel, in many implementations, stores the process context at one point in the code but continue to execute the context switch and scheduling algorithms in the context of the "old" process. When it later restores the context of the process, it resumes execution on the basis of the context which is saved previously.

To differentiate between the case where the kernel resumes the context of a new process and the case where it continues to execute in the old context after having saved it, the return values of critical functions may vary, or the program counter where the kernel executes may be set artificially.

The pseudo code for a context switch is given below:

if (context_save()) // save context of executing process
{
    // take up another process to run
    .
    .
    .
    resume_context (new_process);
    // never gets here
}
// resuming process executes from here

Suppose, process A is in a context switch phase, the function context_save() will store the context of process A and return the value 1. The current program counter is also saved. The value 0, to be returned from context_save() is also saved. In spite of saving the context, the kernel continues to execute in the context of process A, and takes another process to schedule, suppose process B.

And then calls resume_context() for process B. Then the context of process B is restored and it starts executing, hence the comment "never gets here" has given. When in the end, the process A is selected for scheduling, the context of process A is restored, it begins executing in the function context_save() and returns the value 0.

Hence instead of going in the if block resuming execution from below the if block, the comment "resuming process executes from here" has given.

Saving Context for Abortive Returns

Sometimes the situations occur when the kernel needs to abort the current execution sequence and restore a previously saved context.

The algorithm setjmp is used for saving the context and the algorithm longjmp is used to restore the previous context. setjmp is similar to context_save but the only difference is that context_save pushes a new context layer whereas setjmp saves the current context in the u-area and continues to execute in the old context layer. When the kernel wants to restore the context saved in setjmp, it does a longjmp, restoring the context saved in the u-area and returning 1 from setjmp.

Copying Data Between System and User Address Space

The process executes in either kernel mode or in user mode. But the system calls need to copy the data from user to kernel address space and vice-a-versa.

Addresses in kernel address space are of course, not accessible from user address space. The kernel copies data from user address space to kernel address space and vice-a-versa. The kernel when accessing addresses in user address space must ensure that the address is in the user space, otherwise, it could inadvertently, write on some kernel data structures and corrupt the system.

Therefore, copying data between kernel and user address space is a complicated task because it requires more than one instruction.

Please log in to add an answer.