0

How is context switch made in linux kernel when process exits before timer interrupt?

I know that if the process is running and timer interrupt occurs then schedule function is called automatically if the flag is set, schedule function then selects next process to run. Basically in this case the schedule function runs in the context of current process but what happens when process exits even before timer interrupt? who calls schedule function in this case? And in what context does it run?

Note: I had asked this question originally on stackoverflow but I am asking it here again as there was no response on stackoverflow.

2

The timer interrupt is not related to any given process. When it is serviced, the kernel executes an interrupt service routine. That code, and all the functions that it calls, execute in an "interrupt context".

An interrupt can be serviced in the middle of almost anything: the execution of kernel code, including code servicing another interrupt, can be suspended to service an interrupt.

When the scheduler is called, it simply takes into consideration the processes which exist at that time and are runnable.

  • I understand the timer interrupt part but what happens when running process exits even before timer interrupt? How does context switch happen in that case? – Nullpointer Oct 4 '16 at 21:53
  • Thousands of processes exited before the latest timer interrupt that's happening in your machine now. Some of them exited today, some yesterday, some the day before, .,.. a process isn't the system object that "takes" an interrupt. (Even if it was, the system would arrange for the next available process to take it.) When a process exits, another one is dispatched in its place. – Kaz Oct 4 '16 at 22:14
  • Now in Linux, there is always a process that is current (on every core). The CURRENT macro resolves to its struct task_struct *. Even in interrupt time, we can access CURRENT. Regardless of processes exiting, when some interrupt goes off, it finds that some task is CURRENT (sometimes that is just the idle task). I should add that to the answer, I suppose. But it's not important: in an OS which doesn't always have a current task, there can still be a timer interrupt which pre-empts tasks. It sometimes finds that nothing is current; in that case, the sched can make something current. – Kaz Oct 4 '16 at 22:17

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.