2
$\begingroup$

I know this might sound like a newbie question but let's say we have a priority based scheduling algorithm (non-preemptive) where all processes arrive at time 0 and based on priority the order is P1, P2, P3 ...

The processes will run P1, P2, P3 without having to block one to let another one run, each processes will run until it is finished.

I am not sure here: when switching from process P1 to P2 is a context switch necessary? I know that : In computing, a context switch is the process of storing and restoring the state (more specifically, the execution context) of a process or thread so that execution can be resumed from the same point at a later time. But since we don't save P1's state do we still need a context switch? I tend to say we do since a context switch also happens when you start up a new process from scratch which will have its own context, right?

I just want to make sure I get these things straight! Any tip would be greatly appreciated!

$\endgroup$
2
$\begingroup$

Yes, a context switch happens. After P1 ends the state of P2 must be loaded. The only difference with a regular context switch is that the state of P1 does not need to be saved as it is not needed for a later restore.

Whether the OS actually applies this optimization or simply performs a regular context switch and then later discards the saved context for P1 depends on the implementation.

$\endgroup$
  • $\begingroup$ For this operation, I'd say that guaranteed correctness is more important than speed. When you kill a process, it's either running or not. The easiest thing is to do a context switch if it is running, so now you only have to handle the case of killing a process that is not running, you'd probably also mark the process so that no context switch will switch to that process. $\endgroup$ – gnasher729 Apr 17 '17 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.