Sei sulla pagina 1di 3

Character vs.

block devices
There are two main types of devices under all systems, character and block devices.
Character devices are those for which no buffering is performed, and block devices are
those which are accessed through a cache. Block devices must be random access, but
character devices are not required to be, though some are. Filesystems can only be
mounted if they are on block devices.
Character devices are read from and written to with two
function: foo_read() and foo_write(). The read() and write() calls do not return until the
operation is complete. By contrast, block devices do not even implement
the read() and write() functions, and instead have a function which has historically been
called the ``strategy routine.'' Reads and writes are done through the buffer cache
mechanism by the generic functionsbread(), breada(), and bwrite(). These functions go
through the buffer cache, and so may or may not actually call the strategy routine,
depending on whether or not the block requested is in the buffer cache (for reads) or on
whether or not the buffer cache is full (for writes). A request may be
asyncronous: breada() can request the strategy routine to schedule reads that have not
been asked for, and to do it asyncronously, in the background, in the hopes that they will
be needed later. A more complete explanation of the buffer cache is presented below in
Section [When that section is written...]
The sources for character devices are kept in .../kernel/chr_drv/, and the sources for
block devices are kept in .../kernel/blk_drv/. They have similar interfaces, and are very
much alike, except for reading and writing. Because of the difference in reading and
writing, initialization is different, as block devices have to register a strategy routine,
which is registered in a different way than the foo_read() and foo_write() routines of a
character device driver. Specifics are dealt with in Section and Section

Q. Can you explain me what is device files and how do I access or see device files? Why UNIX / Linux has
device files?
A. Under Linux and UNIX each and every hardware device treated as a file. A device file allows to
accesses hardware devices so that end users do not need to get technical details about hardware.
In short, a device file (also called as a special file) is an interface for a device driver that appears in a file
system as if it were an ordinary file. This allows software to interact with the device driver using standard
input/output system calls, which simplifies many tasks.

Device file two types


There are two types of device files based upon how data written to them and read from them is processed

by the operating system and hardware:


Character special files or Character devices
Block special files or Block devices

Understanding Character special files or Character


devices
Talks to devices in a character by character (1 byte at a time)
Examples: Virtual terminals, terminals and serial modems etc

Understanding Block special files or Block devices


Talks to devices 1 block at a time ( 1 block = 512 bytes to 32KB)
Examples: Hard disk, DVD/CD ROM, and memory regions etc

Why use device files?


Device file allows transparent communication between user space applications and computer hardware.

Device file location


All device files are stored in /dev directory. Use cd and ls command to browse the directory:
cd /dev/
ls -l

How do I find out the device file type?


Simply use ls -l command:
ls -l /dev
Look for file's type in the first column output.
A character device is marked with a c as the first letter of the permissions strings.
$ ls -l /dev/console
A block device is marked with a b as the first letter of the permissions strings:
$ ls -l /dev/sdb1

Block devices (also called block special files) usually behave a lot like ordinary files:
they are an array of bytes, and the value that is read at a given location is the value that
was last written there. Data from block device can be cached in memory and read back
from cache; writes can be buffered. Block devices are normally seekable (i.e. there is a
notion of position inside the file which the application can change). The name block
device comes from the fact that the corresponding hardware typically reads and writes

a whole block at a time (e.g. a sector on a hard disk).


Character devices (also called character special files) behave like pipes, serial ports,
etc. Writing or reading to them is an immediate action. What the driver does with the
data is its own business. Writing a byte to a character device might cause it to be
displayed on screen, output on a serial port, converted into a sound, ... Reading a byte
from a device might cause the serial port to wait for input, might return a random byte
(/dev/urandom), ... The name character device comes from the fact that each character
is handled individually.
See Wikipedia and Understanding /de

Potrebbero piacerti anche