1

Consider the following case in a multicore CPU and for simplicity lets stick to linux kernel.

wait_char()
{
   while (1)
   {
       ch = readchar();
       putchar(ch);
   }
}
  1. I open two terminals A and B and run the wait_char() code in each
  2. 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
  3. Now I chose terminal B specifically and input a key from my keyboard
  4. CPU runs the Keyboard Interrupt Service Routine in kernel mode
  5. 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)?

visweshn92
  • 301
  • 1
  • 3
  • 13
  • I will guess as I am not 100% sure. The bash will `fork` and `execv` to the program you typed. The child and parent process share all entries in `PD` table including `stdin` and `stdout`. If you enter your key in first opened bash which you ran program A in, and type there your input, the bash will interrupt and this way the OS take care of that input. Only program A and that bash received that input. Not program B/Second opened bash. – Tony Tannous Feb 14 '17 at 17:08

1 Answers1

0

Processes are not in a queue waiting for I/O. When you enter something on Terminal B, the device driver knows that it came from Terminal B. It is likely the device will trigger an interrupt. The OS will then send the data to whoever has a read request pending on Terminal B.

If you had two processes reading from Terminal B, then it is random which one gets the character.

user3344003
  • 20,574
  • 3
  • 26
  • 62