块设备与字符设备

块和字符是两种不同的访问设备的策略;同一个设备可以同时支持块和字符两种访问策略;

字符设备只能顺序访问(如串口发送数据顺序),而块设备可以随机访问(不连续块访问)。

Nand、SD卡(电储存)等随机访问效率等同于顺序访问。

传统的机械式块设备(如硬盘、DVD)虽然可以随机访问,但是连续访问效率更高,因此块设备驱动中有排序逻辑将用户的随机访问重新调整成尽量连续访问以提升效率。

操作系统对于块设备的管理要比对字符设备的管理更加复杂。

块设备通过系统缓存进行读取,不是直接和物理磁盘读取。字符设备可以直接物理磁盘读取,不经过系统缓存。

块设备 Block Device

对应的有块驱动 Block Device Driver

字符设备 Character Device

对应的有字符设备驱动 Character Device Driver

Character Driver:

  1. Character devices are accessed as a stream of sequential data, one byte after another.
  2. No buffering is required.
  3. Seeking is not allowed.
  4. Managing char driver within a kernel require less care, preparation and work done.
  5. Character driver has only one position current one. It can’t move back and forth.
  6. The read() and write() calls do not return until the operation is complete.
  7. The kernel doesn’t have to provide an entire subsystem to the character device.
  8. sources for character devices are kept in …/kernel/chr_drv/.
  9. Less complex than block driver.
  10. Eg: Keyboard device, serial port.

 

Block Driver:

  1. Block Device are hardware devices which randomly access fixed-sized chunks of data.
    • Fixed size chunk of data is called blocks.
  2. Accessed through a cache so buffering is required.
  3. Seeking is possible.
  4. Managing block driver within a kernel requires more care, preparation and work done.
  5. Block driver navigates back and forth between any location on media.
  6. Reads and write done by buffer cache mechanism by bread(), bwrite().These request may be asynchronous.
  7. The kernel does have to provide an entire subsystem to block device.
  8. Sources for block devices are kept in …/kernel/blk_drv/.
  9. More complex than character driver.
  10. Eg: Hard disk, Pen drive, Blue-rays.


参考链接:

https://www.cnblogs.com/qlee/archive/2011/07/27/2118406.html

https://www.cnblogs.com/xuyh/p/5333086.html
https://blog.csdn.net/jianchi88/article/details/7212370
http://blog.chinaunix.net/uid-7295895-id-3011241.html
块设备相关的几个单位:https://www.cnblogs.com/deng-tao/p/6412687.html

知行合一
原文地址:https://www.cnblogs.com/grooovvve/p/14477633.html