linux中tty设备的一点理解

内核文档/Documentation/devices.txt翻译节选:

    **** 终端设备Terminal, or TTY devices are a special class of character devices. Aterminal device is any device that could act as a controlling terminalfor a session; this includes virtual consoles, serial ports, andpseudoterminals .终端或这TTY设备是一类特殊的字符设备。一个终端设备是任何对于一个会话可以作为控制终端的设备。这包括虚拟控制台、串口和伪终端(PTYs)。All terminal devices share a common set of capabilities known as linedisciplines; these include the common terminal line discipline as wellas SLIP and PPP modes.所有终端设备共享一系列常规能力-线路规程。这包含常见的终端线路规程,例如SLIP和PPP模式。All terminal devices are named similarly; this div explains thenaming and use of the various types of TTYs. Note that the namingconventions include several historical warts; some of these areLinux-specific, some were inherited from other systems, and somereflect Linux outgrowing a borrowed convention.所有终端设备的命名都比较简单。本节介绍不同类型TTY的命名和用途。注意命名的约定包含了一些历史需求:某些是Linux特定的,某些是从其他的系统中继承下来的,还有一些则反映了Linux从借鉴来的约定中发展而来的。A hash mark (#) in a device name is used here to indicate a decimalnumber without leading zeroes.设备名中的(#)标志用于标识一个不以0开头的10进制数。

Virtual consoles and the console device虚拟控制台和控制台设备

    Virtual consoles are full-screen terminal displays on the system videomonitor. Virtual consoles are named /dev/tty#, with numberingstarting at /dev/tty1; /dev/tty0 is the current virtual console./dev/tty0 is the device that should be used to access the system videocard on those architectures for which the frame buffer devices(/dev/fb*) are not applicable. Do not use /dev/consolefor this purpose.虚拟控制台是在系统视频监视器上全屏的显示终端。虚拟控制台设备名为/dev/tty#,编号开始于/dev/tty1。/dev/tty0是当前虚拟控制台。/dev/tty0在那些帧缓冲设备(/dev/fb*)不适用的构架下可以被用来访问系统显卡。而/dev/console并不用于此目的。The console device, /dev/console, is the device to which systemmessages should be sent, and on which logins should be permitted insingle-user mode. Starting with Linux 2.1.71, /dev/console is managedby the kernel; for previous versions it should be a symbolic link toeither /dev/tty0, a specific virtual console such as /dev/tty1, or toa serial port primary (tty*, not cu*) device, depending on theconfiguration of the system.控制台设备/dev/console是一个接受系统信息并在单用户模式下允许登录的设备。从Linux 2.1.71开始,/dev/console由内核管理,而以前的版本是一个到/dev/tty0、一个特定的虚拟控制台(如/dev/tty1)或者一个串口主(tty*,非cu*)设备动态链接,这些依赖系统配置。

Serial ports串行端口

    Serial ports are RS-232 serial ports and any device which simulatesone, either in hardware (such as internal modems) or in software (suchas the ISDN driver.) Under Linux, each serial ports has two devicenames, the primary or callin device and the alternate or callout one.Each kind of device is indicated by a different letter. For anyletter X, the names of the devices are /dev/ttyX# and /dev/cux#,respectively; for historical reasons, /dev/ttyS# and /dev/ttyC#correspond to /dev/cua# and /dev/cub#. In the future, it should beexpected that multiple letters will be used; all letters will be uppercase for the "tty" device (e.g. /dev/ttyDP#) and lower case for the"cu" device (e.g. /dev/cudp#).串行端口是RS-232串口和任何类似的设备,无论是硬件的(如内部调制解调器)或者软件(如ISDN驱动)。在Linux下,每个串口有两个设备名,主要的(callin设备)和备用的(callout设备),每类设备都通过不同的字母标识。对于任何字母X,设备名分别是/dev/ttyX# 和/dev/cux#;由于历史原因,/dev/ttyS#和/dev/ttyC#对应于/dev/cua#和/dev/cub#。未来,对于“tty”多字母的名字将会被使用,所有的字母都将是大写(如/dev/ttyDP#),对于"cu"设备则使用小写字母(如/dev/cudp#)。The names /dev/ttyQ# and /dev/cuq# are reserved for local use.名字(/dev/ttyQ#和/dev/cuq#)保留,用于本地使用。The alternate devices provide for kernel-based exclusion and somewhatdifferent defaults than the primary devices. Their main purpose is toallow the use of serial ports with programs with no inherent or brokensupport for serial ports. Their use is deprecated, and they may beremoved from a future version of Linux.备用设备提供基于内核的exclusion和某些与主要设备不同的默认配置。他们的主要目的是允许那些对于串口并非内部支持或是有一定问题的程序使用串口。他们的使用已经过时,他们可能会从未来的Linux版本中删除。Arbitration of serial ports is provided by the use of lock files withthe names /var/lock/LCK..ttyX#. The contents of the lock file shouldbe the PID of the locking process as an ASCII number.串口的仲裁是通过锁文件(/var/lock/LCK..ttyX#)来提供的。锁文件的内容应该是锁定进程PID的ASCII码。It is common practice to install links such as /dev/modemwhich point to serial ports. In order to ensure proper locking in thepresence of these links, it is recommended that software chasesymlinks and lock all possible names; additionally, it is recommendedthat a lock file be installed with the corresponding alternatedevice. In order to avoid deadlocks, it is recommended that the locksare acquired in the following order, and released in the reverse:安装一个例如/dev/modem的链接来指向串口是常见的做法。为了确保适当锁定在这些环节的存在,建议软件追踪符号并锁定所有可能的名字;此外,建议为相应的备用设备安装一个锁文件。为了避免死锁,建议按以下顺序获取锁,并按反向的顺序释放:1. The symbolic link name, if any (/var/lock/LCK..modem)2. The "tty" name (/var/lock/LCK..ttyS2)3. The alternate device name (/var/lock/LCK..cua2)1、符号链接名,如果有(/var/lock/LCK..modem)2、“tty”名(/var/lock/LCK..ttyS2)3、备用设备名(/var/lock/LCK..cua2)In the case of nested symbolic links, the lock files should beinstalled in the order the symlinks are resolved.在符号链接嵌套的情况下,锁定文件应按照符号链接的顺序来安装以解决问题。Under no circumstances should an application hold a lock while waitingfor another to be released. In addition, applications which attemptto create lock files for the corresponding alternate device namesshould take into account the possibility of being used on a non-serialport TTY, for which no alternate device would exist.在任何情况下,应用程序应该等待另一个程序释放锁后,持有这个锁。此外,试图为相应的备用设备名创建锁文件的应用程序应考虑被用于非串口的TTY端口的可能性,此时没有备用设备存在。

Pseudoterminals (PTYs)伪终端(PTYs)

    Pseudoterminals, or PTYs, are used to create login sessions or provideother capabilities requiring a TTY line discipline (including SLIP orPPP capability) to arbitrary data-generation processes. Each PTY hasa master side, named /dev/pty[p-za-e][0-9a-f], and a slave side, named/dev/tty[p-za-e][0-9a-f]. The kernel arbitrates the use of PTYs byallowing each master side to be opened only once.伪终端(或PTYs)用于创建登录会话或提供给其他需要tty线路规程(包括SLIP或PPP能力)能力以生成数据的进程。每个PTY有一个主端(/dev/pty[p-za-e][0-9a-f])和一个从端(/dev/tty[p-za-e][0-9a-f])。内核通过只允许每个主端仅允许打开一次来仲裁PTY的使用。Once the master side has been opened, the corresponding slave devicecan be used in the same manner as any TTY device. The master andslave devices are connected by the kernel, generating the equivalentof a bidirectional pipe with TTY capabilities.一旦主端被打开,相应的从设备可以像任何TTY设备一样的方式被使用。主从设备都和内核连接,产生相当于一个带TTY功能的双向管道。Recent versions of the Linux kernels and GNU libc contain support forthe System V/Unix98 naming scheme for PTYs, which assigns a commondevice, /dev/ptmx, to all the masters (opening it will automaticallygive you a previously unassigned PTY) and a subdirectory, /dev/pts,for the slaves; the slaves are named with decimal integers (/dev/pts/#in our notation). This removes the problem of exhausting thenamespace and enables the kernel to automatically create the devicenodes for the slaves on demand using the "devpts" filesystem.Linux内核的最近版本和GNU库包含了对于System V和Unix98对PTY命名方式的支持。它分配一个共用的设备(/dev/ptmx)给所有的主端(打开它会自动给你一个以前未分配的PTY)和一个子目录(/dev/pts)用于从端;从端通过十进制整数(/dev/pts/#)命名。这消除了命名空间枯竭的问题,并使内核通过“devpts”文件系统按需自动为从端动创建设备节点。

对于TTY系统的理解(图解):

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

《Linux C编程一站式学习》—-第 34 章 终端、作业控制与守护进程—1. 终端

wiki百科关于终端的网页:Computer terminal|System console|Linux console

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

以上是我参考了网上的资料后对tty的认识整理,参考资料如下:

linux tty pty pts 概念 区别

终端 /dev/console /dev/tty tty

终端tty、虚拟控制台、FrameBuffer的切换过程详解

LINUX下的tty,console与串口分析

linux下tty,控制台,虚拟终端,串口,console(控制台终端)详解

LINXU下的TTY、CONSOLE、串口

Linux下的console和terminal

重新开始吧!下次我会吸取教训,不让自己犯同样的错误的;

linux中tty设备的一点理解

相关文章:

你感兴趣的文章:

标签云: