It's much much easier to explain those in reverse order because a procedure switch always involves a subject switch.
You are watching: Describe the actions taken by a kernel to context-switch between processes.
A typical thread context switch on a single-core CPU happens prefer this:
All paper definition switches space initiated by one 'interrupt'. This can be an really hardware interrupt the runs a driver, (eg. From a network card, keyboard, memory-management or timer hardware), or a software program call, (system call), the performs a hardware-interrupt-like call sequence to go into the OS. In the instance of a driver interrupt, the OS offers an entry point that the driver can call instead of performing the 'normal' straight interrupt-return & so allows a driver to departure via the OS scheduler if it demands the OS to set a object ready, (eg. It has actually signaled a semaphore).
Non-trivial solution will need to initiate a hardware-protection-level change to get in a kernel-state so that the kernel code/data etc. Can be accessed.
The main point state because that the interrupted thread needs to be saved. Top top a simple embedded system, this could just be pushing every registers top top the subject stack and saving the stack guideline in that is Thread regulate Block (TCB).
Many systems switch to an OS-dedicated stack at this phase so that the mass of OS-internal stack needs are no inflicted ~ above the ridge of every thread.
It might be important to note the object stack position where the change to interrupt-state occurred to allow for nested interrupts.
The driver/system speak to runs and may change the collection of ready threads by adding/removing TCB's from internal queues for the various thread priorities, eg. Network map driver may have collection an occasion or signaled a semaphore that another thread was wait on, so that thread will certainly be added to the ready set, or a running thread might have called sleep() and so chosen to remove itself from the ready set.
The OS scheduler algorithm is run to decide which object to operation next, commonly the highest-priority prepared thread the is at the prior of the queue for that priority. If the next-to-run subject belongs to a different process to the previously-run thread, some extra ingredient is necessary here, (see later).
The conserved stack tip from the TCB for that thread is retrieved and also loaded into the hardware stack pointer.
The core state because that the selected subject is restored. ~ above my simple system, the registers would certainly be popped indigenous the ridge of the selected thread. More complex systems will need to handle a go back to user-level protection.
An interrupt-return is performed, so carrying execution come the selected thread.
In the case of a multicore CPU, points are much more complex. The scheduler may decide the a thread the is currently running on an additional core may need to be stopped and replaced through a object that has actually just come to be ready. It deserve to do this by making use of its interprocessor driver to a hardware interrupt the core running the thread that needs to be stopped. The complexities of this operation, on peak of all the various other stuff, is a great reason to stop writing OS kernels :)
A typical process context move happens like this:
Process paper definition switches room initiated through a thread-context switch, so every one of the above, 1-9, is walking to need to happen.
At step 5 above, the scheduler decides to run a subject belonging to a different procedure from the one the owned the formerly running thread.
The memory-management hardware has to be loaded through the address-space because that the brand-new process, ie whatever selectors/segments/flags/whatever that allow the thread/s the the brand-new process to access its memory.
The context of any type of FPU hardware needs to it is in saved/restored from the PCB.
See more: 57 Leaf Clover Risk Of Rain 2 57 Leaf Clover: How To Obtain, I Created A Handy Guide To The 57
There might be other process-dedicated hardware that demands to be saved/restored.
On any kind of real system, the mechanisms room architecture-dependent and the over is a rough and also incomplete guide to the effects of either context switch. There are other overheads created by a process-switch that space not strictly part of the switch - there may be extra cache-flushes and also page-faults after a process-switch because some that its storage may have been paged the end in favor of pages belonging to the process owning the thread that was to run before