From the book of Tanenbaum "Modern operating systems" I learned that, for example, a hard disk can handle incoming requests not in the order in which they are transferred, if it allows to process them more optimally.

There was a question: Suppose we have a process A and B, both of them want to read data from disk. Process A initiates a request, the scheduler switches execution to Process B, and he also initiates a read request from the same disk. Now the disk tells the interrupt controller that it has executed the request for B. Now, how does the kernel understand that it is necessary to inform process B that data is ready for it, and not A. Where is this information stored or transmitted? Everywhere it is simply written that the interrupt handler is called to the corresponding line where the interrupt is set, but there is absolutely no information on how this determines the process for which the information is ready.

  • Such devices in response are sent back along with the data (and other status info) and the request ID to which they respond. It is for this ID that the system finds the chain of control blocks that leads to the process that initiated the request. - avp
  • In response to whom? What channel is the transmission of status information and request ID? After all, the interrupt line is essentially 0/1 to notify that the device has fulfilled some kind of request, or can it directly overtake data? Do not tell me where you can read in more detail even with some example? - Ellochka Cannibal
  • By lines HBA (Host Bus Adapter) i. via the data transfer channel, naturally (via SAS, SATA, FC, SCSI, USB or whatever you have there), and not via the interrupt line. - avp

1 answer 1

Queuing order

Magnetic hard drives perform faster such requests, the data of which lie closer to the read head

Yes, it allows you to more effectively use the movement of the head on the disk. Such actions, for example, implements the NCQ hardware algorithm, because why turn the head 500 times, sequentially turning over the queue, when can we take the data that is closer? This minimizes the number of head movements and the waiting for the desired sector on the track. There are hardware algorithms and software implementation algorithms for ordering.


Real life example

Passengers get into the elevator on the first floor and press the floors they need, the elevator goes sequentially according to the number of floors, not distinguishing the difference between the one who pressed the first or the last, effectively serving the current queue. And if he served first those who pressed the first, imagine how many extra kilometers would the elevator be winding? First at 6, then at 2, and so on.


You'd be surprised, but there are I / O controllers that can combine your two requests into one , but you won't even know about it. The I / O manager does everything to optimize the work time for the request and the number of work performed.

Hard disk and operating system

The input-output system is multi-level. All your requests are serviced by an I / O manager that creates a special structure (for example, on Windows, this is an IRP), which directs requests to the file system driver, and that directs requests to the hard disk driver, which works with the controller, which in turn works with a magnetic head.

Your request is transferred to the hard disk, what next? When your request has been processed by the hard disk, it generates an interrupt that is processed by the interrupt handler for the device, the device driver writes data to the system memory, and notifies the I / O manager that the data has been saved, but now you need to somehow send the data to the caller. flow so that it can copy its results to a buffer in the address space of its process.

The I / O manager knows the ID of the process that requested this data, it accesses the process address space and writes data from system memory to the process's virtual address space and notifies the process (for example, using a callback function or an alarm) that the data transfer complete and free all associated structures.

PS The description of the work of the I / O subsystem is very well described on various resources, although there are long articles describing them on the example of the operating system, here is a good description based on Windows.