Today's Agenda: Block devices Including mmap demo Block vs. character device "random" access - random depends on your perspective Block device divided into sectors Sector vs. block Using dd /dev conventions, hda, sda, nvme, etc And symbolic links Blocks are represented as a buffer Size of a block: At least a sector, no more than a memory page sector: Hardware division, property of the hard drive block: Software division, property of the operating system struct buffer_head defines one Flags for state Interlude: Memory-mapping files? Use mmap, munmap, msync, etc Demo Not for expanding files You can use write to append, and then unmap and mmap read/write/lseek sort of supports random access Not so natural as this This is *probably* faster for a lot of things bio structure "Block I/O" I/O schedulers Minimize head movement merging and sorting Independent of process scheduler - Process makes request - I/O scheduler gets around to it Merge if requests are for very close sectors Sort by proximity to head - Without reversing head direction if possible - Like an elevator - Sometimes called I/O elevator The Linus Elevator: - Default in 2.4, replaced in 2.6 - New requests are checked against all others for merging - Merge onto front OR back depending - If no merging, added to sorted point in queue - If no good sorted point, add to the end of the queue - If tail is old, new requests go at the end regardless + To prevent infinite starvation The Deadline I/O scheduler: - Writes starving reads - Requests have expiration time + 500 ms for read, 5 seconds for write - Extra queue for each, sorted by time - If a request expires in the extra queue, it is serviced - Generally services requests by expiration time The Anticipatory I/O Scheduler - Heavy write load may perform badly on Deadline scheduler - Waits a few ms after performing read before resuming write + In case the application needs something else - Keeps track of process access patterns to determine wait Completely Fair Queuing I/O Scheduler - Each process has a queue - Merging is performed, across queues (duplicates, references) - Round-robin with selection of a few items from each queue - Generally works well with non-pathological cases - Intended for multimedia originally - Default I/O scheduler as of book publication Noop I/O Scheduler - Merge requests, but don't do ANY sorting - Simpler than the others - Intended for flash memory only (no head position)