Device number and device file "Go" in Linux

Source: Internet
Author: User

This article was reproduced from: http://blog.csdn.net/ymangu666/article/details/39292651

Primary, secondary device number

The application can access the actual device by reading and writing to the device files in the/dev directory.
1) Each device file corresponds to two device numbers: Main device number, secondary device number
① Main device number: Identifies the type of the device, and also indicates the driver used by the device;
When the driver is initialized, it registers its driver and corresponding master device number to the system, which can drive the system device's main device number through the/proc/devices file.
② Secondary Device number: Represents a different hardware device that uses the same device driver. There are two LEDs, LED lights need to be opened or closed independently. Then, you can write a LED light character device driver, you can register its main device number as a 5th device, the secondary device numbers are 1 and 2, respectively. Here, the second device number represents two LED lights, respectively.
When the driver traverses the device, each time it discovers a device it can drive, it creates a device object and assigns it a secondary device number to differentiate between different devices.
The requested device number is the main device number: Alloc_chrdev_region ();

Device file type

2) Character devices, block devices, network devices
① a character device is a byte-stream device, access to the device can only be accessed sequentially in bytes, not random access, the character device does not request a buffer, and all access requests are executed sequentially. Most of the devices under Linux are character devices. An application accesses a character device through a character device node.
Device nodes are typically created in the/dev directory by the Mknod command, and the first flag of a character device file is the preceding "C" flag.
Linux uses the same method of management files to manage character devices, so each character device in the/dev/directory has a corresponding device files, namely the device node, which contains the type of the device, the main/secondary device number and access control of the device, the system through the device file to the character device operation, Each character device file has its own group structure (struct file_operations) of the same file manipulation function as the normal file. Character device drivers typically require at least the implementation of the open, release, read, and write four operations in the file manipulation function group. Common words
The device has a mouse, keyboard, serial port, console and so on.

② storage devices typically belong to block devices, block devices have request buffers, and support random access without having to sequentially access data. Disk devices under Linux are block devices, and although there is a block device node under Linux, the application typically accesses the block device through the file system and its cache, rather than reading and writing data on the block device directly through the device node. The first flag of a block device file is the preceding "B" flag.
A block device can be used as a normal bare device to hold arbitrary data, or the block device can be formatted as a file system type, and then read the data on the block device in the format of the file system type, but either way, the last access to the data on the device must be implemented by invoking the operation method of the device itself. The difference is that the former directly calls the action method of the block device, while the latter indirectly calls the action method of the block device. Common block devices have various hard disks, flash disks, RAM disks, and so on.
③ network devices differ from character devices and block devices, which are message-oriented rather than stream-oriented, and do not support random access or request buffers. In Linux a network device can also be called a network interface, such as Eth0, the application through the socket rather than the device node to access network equipment, there is no network device node in the system.
How the application operates on these 3 types of devices:

Question 1: When we access a device node, is the system aware of which device is used to drive and access which device?
A: This is done by the device number. When we create a device node, we need to specify the main device number and the secondary device number. For the device node, the name is not important, the device number is the most important, it actually specifies the corresponding driver and the corresponding device.

Device number and device file "Go" in Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.