2

Supposedly I have a file that's distributed among different sectors. As an example, assuming both physical and logical sectors are 512B. A user process issues a request for the kernel to read the file. Let's say this file uses 3 distributed sectors on the hard drive.

1) Does the hard drive read all of the sectors at once and send the whole (512 * 3) data to the kernel?

*The 3 sectors are read in full by the hard drive first and then the data gets transferred to the kernel, 512 * 3 bytes gets transferred from HDD to kernel.

2) Do HDDs read multiple sectors at once? This seems more of a hardware related question though.

2

It depends. Modern kernels of Unix-like operating systems tend to have pretty complex code to make I/O faster. The best known feature is caching: if a sector has been read in the past, there may still be a copy in memory, in which case no request is sent to the hard drive at all. Other typical acceleration features include out-of-order access (when there are many pending requests, the kernel may issue them in a different order to minimize seeks or to better exploit parallelism allowed by the hardware), and prefetching (the kernel assumes that if sectors 0 through 5 of a file have been read then it's likely that sector 6 will be read, to it will read sector 6 into the cache if it has nothing better to do).

Whether it's possible to issue the request to read sector 1 before the request to read sector 0 has been completed depends on the capabilities of the disk bus and of the disk. There's no general answer.

  • 1
    @direprobs some (many perhaps nowadays) are, look up “scatter-gather”. – Stephen Kitt Jul 31 '17 at 10:12
  • 1
    Data reads and writes are initiated by sending sending commands over the SATA or SCSI (or whatever interface is used). The commands include the logical block address (LBA) of the first sector to be read, and the number of consecutive sectors to be transferred. Typically Direct Memory Access (DMA) is used: the host controller is programmed with the memory address where to/from the data is read, without involving the CPU at all. When the transfer is complete, an interrupt is sent to the CPU to inform it that the transfer is complete. – Johan Myréen Jul 31 '17 at 10:16
  • 1
    @direprobs scatter-gather is implemented by chipsets and/or drives, it’s not (just) related to the kernel. – Stephen Kitt Jul 31 '17 at 10:22
  • 1
    Modern drives and host controllers also support command queues, which means there can be several commands "in flight" on the drive, and the drive can reorder them to optimise disk access. – Johan Myréen Jul 31 '17 at 10:23
  • 1
    @direprobs It depends on the bus type, on the host controller and on the drive chipset and firmware (and on the driver's ability to fully exploit all the possibilities). That's why I'm not being more precise. – Gilles Jul 31 '17 at 10: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.