Consider the following case in a multicore CPU and for simplicity lets stick to linux kernel.
wait_char()
{
while (1)
{
ch = readchar();
putchar(ch);
}
}
- I open two terminals A and B and run the wait_char() code in each
- Both the process A and process B are blocked and put in I/O queue awaiting for keyboard interrupt to come. Lets say, A is ahead of B in that queue
- Now I chose terminal B specifically and input a key from my keyboard
- CPU runs the Keyboard Interrupt Service Routine in kernel mode
- Naturally I will see the inputted key echoed in console.
Now how did the kernel figure out it was for B (who was behind in the I/O queue)?