Home  >  Article  >  System Tutorial  >  Linux /dev directory: introduction and application of special device files

Linux /dev directory: introduction and application of special device files

PHPz
PHPzforward
2024-02-11 20:45:241232browse

In a Linux system, everything is a file. This means that in addition to ordinary text files and binary files, various devices in the system can also be represented and accessed in the form of files. These device files are usually stored in the /dev directory. They have some special attributes and functions, such as major device number, minor device number, character device, block device, etc. What are the common special device files in Linux systems? What are their functions and uses? This article will introduce the special device files in the Linux /dev directory in detail from the perspective of concepts and examples.

Linux /dev目录:特殊设备文件的介绍与应用

Linux is a file system, and all hardware such as software will have corresponding file representations under the corresponding directory. For the dev directory, we know that the files under it represent Linux devices. In Windows systems, devices are well understood by everyone, like hard drives, and disks refer to actual hardware. Under the Linux file system, there are files associated with these devices. Accessing them can be placed on actual hardware. Think about it, Linux is more flexible. Turn it into a file, how simple the operation would be. There is no need to call the previous com, prt and other interfaces. Directly read and write files to send read or write operations to the device. According to the way of reading, writing and storing data, we can divide the devices into the following types: character devices, block devices, and pseudo devices.

1. Equipment classification

  • Character device

Character device refers to a device that transmits 1 character to the system at a time. These device nodes usually provide streaming communication services for devices such as faxes, virtual terminals, and serial modems and keyboards. They usually do not support random access data. When character devices are implemented, most do not use buffers. The system reads/writes each character directly from the device. For example, a device such as a keyboard provides a data stream. When you type the string "cnblogs", the keyboard driver will return this seven-character data stream in exactly the same order as the input. They are sequential, returning c first and s last.

  • Block device

Block devices refer to devices that use blocks to move data between systems. These device nodes typically represent addressable devices such as hard disks, CD-ROMs, and memory areas.

Block devices usually support random access and addressing, and use buffers. The operating system allocates buffers for input and output to store a block of data. When a program sends a request to read or write data to the device, the system stores each character in the data in the appropriate cache. When the cache fills up, appropriate action is taken (the data is transferred away), and the system clears the cache. The difference between it and character devices is whether it supports random storage. Character type is in stream form and is stored one by one.

  • Pseudo device

In Unix-like operating systems, device nodes do not necessarily correspond to physical devices. Devices without this correspondence are pseudo-devices. Operating systems make use of the various functions they provide. Some commonly used pseudo devices include: null, zero, full, loop, random, urandom

2. Special equipment and use

The special devices mentioned here are except the hard disk, motherboard, etc., but they have special functions in the Linux shell command, so they are taken out separately. These devices are:

/dev/stdin

/dev/stdout

/dev/stderr

/dev/null

/dev/zero

/dev/full

/dev/random,urandom

/dev/fd

/dev/tcp|upd

/dev/loop

1. Standard output and input devices

Remember what I said last time, Linux redirection? You can read: Linux shell data redirection (input redirection and output redirection) detailed analysis. They correspond to several special file descriptors, fd0, fd1, fd2 (stdin, stdout, stderr)

like:

[chengmo@centos5 shell]$ cat>teststdin/stdin
test
#ctrl+D
#cat从/dev/stdin获得数据,然后将标准输出,输入的到teststdin文件
[chengmo@centos5 shell]$ cat teststdin 
test
  
[chengmo@centos5 shell]$ cat>teststdin
test
#ctrl+D
#不指定输入,默认输入设备就是/dev/stdinn

/dev/stdin refers to the keyboard device

[chengmo@centos5 shell]$ cat test.sh >/dev/stdout |grep 'echo'
echo "very good!";
echo "good!";
echo "pass!";
echo "no pass!"
#/dev/stdout指向就是标准输出,因此重定向给它的数据,最终发送到屏幕上(fd1)
      
[chengmo@centos5 shell]$ cat test.sh  |grep 'echo'          
echo "very good!";
echo "good!";
echo "pass!";
echo "no pass!";
  
      
[chengmo@centos5 shell]$ cat test.sh >/dev/stderr |grep 'echo' 
#!/bin/sh
  
scores=40;
if [[ $scores -gt 90 ]]; then
    echo "very good!";
elif [[ $scores -gt 80 ]]; then
    echo "good!";
elif [[ $scores -gt 60 ]]; then
    echo "pass!";
else
    echo "no pass!";
fi;
#/dev/stderr 指是错误输出,默认也是输出到屏幕上面,但是它的内容不能通过管道传递给grep,管道只能传递标准输出

/dev/null device

is a black hole device, which discards all data written to it. Empty devices are usually used to discard unnecessary output streams. I remember that when I was using Windows, there was a similar device: NUL, which had the same function. Any data written to the device will be discarded. Reading data from this returns empty. Frequently send some unused content to this device and discard unnecessary data.

like:

[chengmo@centos5 shell]$ cat /dev/null
[chengmo@centos5 shell]$ cat test.sh >/dev/null
 
#读该设备为空,写入该设备数据都丢弃了

/dev/zero device

In UNIX-like operating systems, /dev/zero is a special file that provides unlimited null characters (NULL, ASCII NUL, 0×00) when you read it. One typical use is to overwrite information with the character stream it provides. Another common use is to generate a blank file of a specific size.

like:

[chengmo@centos5 shell]$ dd if=/dev/zero of=testzero count=1024 bs=1024
1024+0 records in
1024+0 records out
1048576 bytes (1.0 MB) copied, 0.0107194 seconds, 97.8 MB/s
#创建一个大小为1M文件,该文件一个块是1024字节,一共是1024块(刚好1M),用/dev/zero文件内容填充它。输出创建到:testzero文件
  
  
[chengmo@centos5 shell]$dd if=/dev/zero of=/dev/磁盘分区
#这个命令一定不要随便用,有点象windows里面的粉碎文件工具。不过它是用x00填充整个分区。这样做数据是不可以恢复的了。
  
[chengmo@centos5 shell]$cat /dev/zero>testinputzero
#这个命令也不能随便使用咯,/dev/zero设备一个特效是,如果你读取的话,是一个死循环会输出无穷的\x00,这样你将创建一个用\x00填充的文件。如果你没有限制该用户磁盘配额。它将耗尽整个磁盘空间。

在linux资源配额限制里面,如果没有现在普通用户的磁盘空间利用,或内存使用。一个普通用户就可以通过上面方法一会就塞满整个磁盘。也可以通过while(true) {fork……}类程序,启动无限线程,耗尽整个系统内存。

/dev/full设备

类Unix系统中,/dev/full(常满设备)是一个特殊设备文件,总是在向其写入时返回设备无剩余空间(错误码为ENOSPC),读取时则与/dev/zero相似,返回无限的空字符(NULL, ASCII NUL, 0×00)。这个设备通常被用来测试程序在遇到磁盘无剩余空间错误时的行为。

如:

[chengmo@centos5 shell]$ echo 'chengmo' >/dev/full
-bash: echo: write error: 设备上没有空间
[chengmo@centos5 shell]$ echo $?
1
#命令执行返回错误

/dev/random[urandom]

在类UNIX操作系统中,/dev/random是一个特殊的设备文件,可以用作随机数发生器或伪随机数发生器。它允许程序访问来自设备驱动程序或其它来源的背景噪声。常用作随机数发生器。具体参考:linux shell实现随机数多种方法(date,random,uuid)

/dev/fd

记录用户打开的文件描述符

[chengmo@centos5 shell]$ ls /dev/fd/
0 1 2 3

详细参考:

linux shell数据重定向(输入重定向与输出重定向)详细分析 文件描述符介绍。

/dev/tcp[udp]/host/port

读取该类形式设备,将会创建一个连接host主机port端口的tcp[upd]连接。打开一个socket通讯接口。

详细使用可以参考:

linux shell 脚本实现tcp/upd协议通讯(重定向应用)

/dev/loop

在类UNIX操作系统中,Loop设备 可以把loop 文件,作为块设备挂载使用。

如:

[chengmo@centos5 shell]$mount -o loop example.img /home/chengmo/img

#将img镜像文件挂载到/home/chengmo/img目录下面,有了这个设备,我们不需要通过虚拟光驱就可以读虚拟磁盘格式的文件。

本文介绍了Linux /dev目录下的特殊设备文件,包括它们的分类、属性、命名规则等。我们还了解了一些常见的特殊设备文件,如/dev/null、/dev/zero、/dev/random、/dev/tty、/dev/sda等,以及它们的作用和用法。通过本文,我们可以更清楚地了解Linux系统中的设备管理和访问机制。

The above is the detailed content of Linux /dev directory: introduction and application of special device files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete