TrumanWong

mknod

Create character device files and block device files

Supplementary instructions

mknod command is used to create character device files and block device files in Linux.

grammar

mknod(options)(parameters)

Options

-Z: Set a safe context;
-m: Set permission mode;
-help: display help information;
--version: Display version information.

Parameters

  • File name: the name of the device file to be created;
  • Type: Specify the type of device file to be created;
  • Major device number: specifies the major device number of the device file;
  • Minor device number: Specify the minor device number of the device file.

Example

ls -la /dev/ttyUSB*
crw-rw——- 1 root dialout 188, 0 2008-02-13 18:32 /dev/ttyUSB0
mknod /dev/ttyUSB32 c 188 32

Expand knowledge

Linux device management is closely integrated with the file system. Various devices are stored in the /dev directory in the form of files, called device files. Applications can open, close, read and write these device files, and complete operations on the device just like operating ordinary data files.

In order to manage these devices, the system numbers the devices, and each device number is divided into a major device number and a minor device number. Major device numbers are used to distinguish different types of devices, while minor device numbers are used to distinguish multiple devices of the same type. For commonly used devices, Linux has conventional numbers. For example, the major device number of a hard disk is 3.

Linux provides a unified operation function interface for all device files by using the data structure struct file_operations. This data structure includes pointers to many operating functions, such as open(), close(), read(), write(), etc. However, due to the large number of peripherals, the operating methods are different. The members in the Struct file_operations structure are a series of interface functions, such as read/write functions for reading/writing and ioctl for control.

To open a file is to call the open operation in file_operations of this file. Different types of files have different file_operations member functions, such as ordinary disk data files. The interface function completes the disk data block reading and writing operations; for various device files, the I/O functions in the respective drivers are ultimately called to perform specific device operations. operation. In this way, the application does not have to consider whether it is operating a device or an ordinary file. It can be treated as a file and has a very clear and unified I/O interface. So file_operations is the file-level I/O interface.