Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of linux device node

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of what the concept of linux device node is, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on the concept of linux device node. Let's take a look at it.

The linux device node is a bridge between the application and the device driver; the device node is created in "/ dev", which is the hub connecting the kernel and the user layer, which is equivalent to the inode of the hard disk, recording the location and information of the hardware device. The device node enables the user to communicate with the kernel hardware, read and write devices, and other operations.

The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

What is a device node?

The communication bridge between people is language. Similarly, the communication between applications and device drivers also needs a bridge. This bridge is the equipment node.

For Linux systems, all IO resources are files, including files, directories, hard drives, devices, and so on. Then, the keyboard as an input device in the computer system, the operating system also abstracts it from the file. If you want to get the data input from the keyboard, you only need to read the device node provided by the keyboard.

In the Linux system, the keyboard is an input device, and its corresponding device node is located under "/ dev/input". There are many files under this folder that start with event, and these are the device nodes of all input devices. How to determine which is the device node of the keyboard? Connect the keyboard to the raspberry pie, open the terminal, execute "sudo cat / dev/input/event0", tap the keyboard, and if there is no output, change the next node until you find the output node, which is the device node corresponding to the keyboard.

The device node is created under / dev and is the hub connecting the kernel and the user layer, that is, the device is connected to which ID of the corresponding interface. Something like the inode of a hard disk, recording the location and information of the hardware device

In Linux, all devices are stored in the / dev directory in the form of files, and are accessed through files. The device node is the abstraction of the device by the Linux kernel, and a device node is a file. The application accesses the device through a standardized set of calls independent of any particular driver. The driver is responsible for mapping these standard calls to the specific operations of the actual hardware.

The role of device nodes

The device node enables users to communicate with the kernel in hardware, read and write devices, and other operations.

In linux, devices exist like ordinary files, and accessing a device is like accessing a file.

The primary device number represents a kind of device, and the secondary device number represents different individuals of the same type of device. Here, we may not know the existence form of the device node.

The existence form of device node

In addition, there is a concept in linux, that is, inode and block, that is, the blocks and nodes on one side of the hard disk, the inode in the hard disk is equivalent to a file or folder, it records the location of the file under this file, and the location of the file is aligned with block size, for example, some systems are 4K in size, while the size of inode is limited, so there is a saying that a single file cannot exceed 4G. In my personal understanding, the node in the linux driver can also be regarded as something similar to the inode of the hard disk, in which the location of the hardware device and other information can be recorded. When the user needs to access the device, it refers to the information recorded by the device node to access the device.

How to get data from a device node

The reason why the operating system abstracts the IO into a file, the biggest advantage is that it can access the file through a unified interface, thus communicating with different devices. These unified interfaces are a set of system calls provided by the operating system for file operations: open function, read function, write function and so on. For example, if you need to obtain data from a device, you only need to call the read function to read the device node corresponding to the device. Of course, before read, you need to call the open function to open it. Now let's take getting keyboard input as an example.

1. Open the device node

Before reading the data of the device node, call the open function to open the device node. The specific usage of the open function can be found in the link. A brief description is as follows:

Function declaration:

Int open (const char * pathname, int flags)

Header files that need to be included:

# include

Parameters:

* the first parameter (const char * pathname): indicates the path of the file to be opened.

* the second parameter (int flags): indicates how to open the file, for example, "O_RDONLY"-read-only open; "O_WRONLY"-write-only open; "O_RDWR"-read, write open, etc.

Return value:

If the opening is successful, the file descriptor of the file is returned for use by functions such as read,write. Otherwise, return-1.

Then, to open the device file for the keyboard (assuming "/ dev/input/even10"), you need the following code:

Int keys_fd; keys_fd = open ("/ dev/input/even10", O_RDONLY); if (keys_fd

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report