块和字符是两种不同的访问设备的策略;同一个设备可以同时支持块和字符两种访问策略;
字符设备只能顺序访问(如串口发送数据顺序),而块设备可以随机访问(不连续块访问)。
Nand、SD卡(电储存)等随机访问效率等同于顺序访问。
传统的机械式块设备(如硬盘、DVD)虽然可以随机访问,但是连续访问效率更高,因此块设备驱动中有排序逻辑将用户的随机访问重新调整成尽量连续访问以提升效率。
操作系统对于块设备的管理要比对字符设备的管理更加复杂。
块设备通过系统缓存进行读取,不是直接和物理磁盘读取。字符设备可以直接物理磁盘读取,不经过系统缓存。
块设备 Block Device
对应的有块驱动 Block Device Driver
字符设备 Character Device
对应的有字符设备驱动 Character Device Driver
Character Driver:
- Character devices are accessed as a stream of sequential data, one byte after another.
- No buffering is required.
- Seeking is not allowed.
- Managing char driver within a kernel require less care, preparation and work done.
- Character driver has only one position current one. It can’t move back and forth.
- The read() and write() calls do not return until the operation is complete.
- The kernel doesn’t have to provide an entire subsystem to the character device.
- sources for character devices are kept in …/kernel/chr_drv/.
- Less complex than block driver.
- Eg: Keyboard device, serial port.
Block Driver:
- Block Device are hardware devices which randomly access fixed-sized chunks of data.
- Fixed size chunk of data is called blocks.
- Accessed through a cache so buffering is required.
- Seeking is possible.
- Managing block driver within a kernel requires more care, preparation and work done.
- Block driver navigates back and forth between any location on media.
- Reads and write done by buffer cache mechanism by bread(), bwrite().These request may be asynchronous.
- The kernel does have to provide an entire subsystem to block device.
- Sources for block devices are kept in …/kernel/blk_drv/.
- More complex than character driver.
- 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