Managing Files for Character Devices, Block Devices, and Special Devices


As you've probably noticed as we've gone through this chapter, every device in your SUSE Linux system is represented as a file in the /dev directory. Your hard disk drive is represented as a file, such as hda (IDE) or sda (SCSI). Your floppy disk drive is represented as a file, such as fd0. Even your CD-ROM drive is represented by the cdrom file, which is actually a symbolic link to another file, such as hdb or hdc. These files allow your hardware devices to function as one of the following device types:

  • Character devices Character devices transfer data in a serial fashion, one piece of information at a time. Modems, keyboards, mice, and terminals are examples of character devices.

  • Block devices Block devices are those that, unlike a character device, provide a degree of random access to the data they contain. Hard disk drives, floppy disk drives, and CD/DVD drives are examples of block devices.

Every device in your system needs a driver for the Linux kernel to be able to interface with it. For example, your IDE hard disk drive needs a driver; all SCSI devices in your system need a device driver as well. An important point to remember is that multiple devices of the same type can be serviced by the same kernel driver. For example, if you had three SCSI hard drives in your system, a single instance of the SCSI kernel driver can manage the three physically separate devices.

The Linux kernel keeps track of these different devices using major and minor device numbers. These numbers are assigned to each device's associated file in /dev. The major number indicates the type of device associated with the file. For example, consider the files displayed in Figure 18.21.

Figure 18.21. Major and minor numbers.


In Figure 18.21, the ls -l command has been used to display the various IDE device files in /dev, denoted as hda. Notice in the middle of each line in the output of ls that two numbers are displayed. The first number is the major number. All IDE devices have a major number of 3. (SCSI devices have a major number of 8.) The major number identifies which device driver the kernel should use to interface with the particular device.

In Figure 18.21, you will also see a second number listed in the middle of each line of the output of ls. This is the minor number. For example, device file hda has a major number of 3 and a minor number of 0. The minor number identifies the specific node of the device. In Figure 18.21, you can see that device hda1 (the first partition on the master IDE hard drive on the primary IDE channel) has a major number of 3 and a minor number of 1.

In addition, the output of ls, shown in Figure 18.21, indicates the type of device represented by each file. This is indicated by the first character of each line. Notice that each hda line in Figure 18.21 begins with a b. This indicates that the IDE hard disk drives are block devices. By way of comparison, refer to Figure 18.22.

Figure 18.22. Viewing character devices.


The tty devices shown in 18.22 are serial devices. Accordingly, they have a c as the first character in each line of the output of ls, indicating that these files represent character devices.

You can create your own character or block device files. This is done with the mknod utility. The syntax of mknod is as follows:

mknod file_name device_type major_number minor_number 

For example, you could create a new character device named echo with a major number of 33 and a minor number of 0 by entering the following at the shell prompt:

mknod /dev/echo c 33 0 

Major number 33 (character) is reserved for serial cards. You can view a complete list of major numbers and the type of devices they are associated with by viewing the /usr/src/linux-kernel_version/Documentation/devices.txt file on your SUSE Linux system.



SUSE Linux 10 Unleashed
SUSE Linux 10.0 Unleashed
ISBN: 0672327260
EAN: 2147483647
Year: 2003
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net