/dev/ttyn是进程虚拟控制台,他们共享同一个真实的物理控制台。 如果在进程里打开一个这样的文件且该文件不是其他进程的控制台时,那该文件就是这个进程的控制台。进程printf数据会输出到这里。在PC上,用户可以使用alt+Fn切换控制台,看起来感觉存在多个屏幕,这种虚拟控制台对应tty1~n,其中 :/dev/tty1等代表第一个虚拟控制台 例如当使用ALT+F2进行切换时,系统的虚拟控制台为/dev/tty2 ,当前控制台(/dev/tty)则指向/dev/tty2 在UNIX系统中,计算机显示器通常被称为控制台(Console)。它仿真了类型为Linux的一种终端,并且有一些设备特殊文件与之相关联:tty0、tty1、tty2等。当你在控制台上登录时,使用的是tty1。使用Alt+[F1—F6]组合键时,我们就可以切换到tty2、tty3等上面去。 你可以登录到不同的虚拟控制台上去,因而可以让系统同时有几个不同的会话存在。 而比较特殊的是/dev/tty0,他代表当前虚拟控制台,是当前所使用虚拟控制台的一个别名。因此不管当前正在使用哪个虚拟控制台(注意:这里是虚拟控制台,不包括伪终端),系统信息都会发送到/dev/tty0上。只有系统或超级用户root可以向/dev/tty0进行写操作。tty0是系统自动打开的,但不用于用户登录。在Framebuffer设备没有启用的系统中,可以使用/dev/tty0访问显卡。 2、 伪终端pty(pseudo-tty) 伪终端(Pseudo Terminal)是终端的发展,为满足现在需求(比如网络登陆、xwindow窗口的管理)。它是成对出现的逻辑终端设备(即master和slave设备, 对master的操作会反映到slave上)。它多用于模拟终端程序,是远程登陆(telnet、ssh、xterm等)后创建的控制台设备。历史上,有两套伪终端软件接口: BSD接口:较简单,master为/dev/pty[p-za-e][0-9a-f] ;slave为 /dev/tty[p-za-e][0-9a-f] ,它们都是配对的出现的。例如/dev/ptyp3和/dev/ttyp3。但由于在编程时要找到一个合适的终端需要逐个尝试,所以逐渐被放弃。 Unix 98接口:使用一个/dev/ptmx作为master设备,在每次打开操作时会得到一个master设备fd,并在/dev/pts/目录下得到一个slave设备(如 /dev/pts/3和/dev/ptmx),这样就避免了逐个尝试的麻烦。由于可能有好几千个用户登陆,所以/dev/pts/*是动态生成的,不象其他设备文件是构建系统时就已经产生的硬盘节点(如果未使用devfs、udev、mdev等) 。第一个用户登陆,设备文件为/dev/pts/0,第二个为/dev/pts/1,以此类推。它们并不与实际物理设备直接相关。现在大多数系统是通过此接口实现pty。 我们在X Window下打开的终端或使用telnet 或ssh等方式登录Linux主机,此时均通过pty设备。例如,如果某人在网上使用telnet程序连接到你的计算机上,则telnet程序就可能会打开/dev/ptmx设备获取一个fd。此时一个getty程序就应该运行在对应的/dev/pts/*上。当telnet从远端获取了一个字符时,该字符就会通过ptmx、pts/*传递给 getty程序,而getty程序就会通过pts/*、ptmx和telnet程序往网络上返回“login:”字符串信息。这样,登录程序与telnet程序就通过“伪终端”进行通信。 telnet<--->/dev/ptmx(master)<--->pts/*(slave)<--->getty 如果一个程序把 pts/*看作是一个串行端口设备,则它对该端口的读/写操作会反映在该逻辑终端设备对的另一个/dev/ptmx上,而/dev/ptmx则是另一个程序用于读写操作的逻辑设备。这样,两个程序就可以通过这种逻辑设备进行互相交流,这很象是逻辑设备对之间的管道操作。对于pts/*,任何设计成使用一个串行端口设备的程序都可以使用该逻辑设备。但对于使用/dev/ptmx的程序,则需要专门设计来使用/dev/ptmx逻辑设备。通过使用适当的软件,就可以把两个甚至多个伪终端设备连接到同一个物理串行端口上。 “实验:1、在X下打开一个或N个终端窗口2、#ls /dev/pts/*3、关闭这个X下的终端窗口,再次运行;比较两次输出信息就明白了。输出为/dev/ptmx /dev/pts/1存在一(master)对多(slave)的情况 3、 串口终端(/dev/ttySn) 串行端口终端(Serial Port Terminal)是使用计算机串行端口连接的终端设备。计算机把每个串行端口都看作是一个字符设备。有段时间串行端口设备通常被称为终端设备,那时它的最大用途就是用来连接终端,所以这些串行端口所对应的设备名称是/dev/tts/0(或/dev/ttyS0)、/dev/tts/1(或/dev /ttyS1)等,设备号分别是(4,0)、(4,1)等(对应于win系统下的COM1、COM2等)。若要向一个端口发送数据,可以在命令行上把标准输出重定向到这些特殊文件名上即可。例如,在命令行提示符下键入:echo tekkaman> /dev/ttyS1会把“tekkaman”发送到连接在ttyS1(COM2)端口的设备上。在2.6以后的内核中,部分三星芯片(例如S3C24x0等)将串口终端设备节点命名为ttySACn。TI的Omap系列芯片从2.6.37开始芯片自带的UART设备开始使用专有的的omap-uart驱动,故设备节点命名为ttyOn,以区别于使用8250驱动时的设备名“ttySn”。 4、 其它类型终端 还针对很多不同的字符设备存在有很多其它种类的终端设备特殊文件,例如针对ISDN设备的/dev/ttyIn终端设备等。~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 其实在理解以上概念的时候,如果了解终端的发展历程,就可以比较容易理解tty、终端的概念。所以请大家阅读最后推荐的wiki英文网页,有助于理解上面的概念。当然,内核文档也是必不可少的参考资料,我顺手翻译了一下。 内核文档/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 section 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系统的理解(图解): ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~以上是我参考了网上的资料后对tty的认识整理,参考资料如下:linux tty pty pts 概念 区别:http://www.hugedomains.com/domain_profile.cfm?d=diybl&e=com终端 /dev/console /dev/tty tty:http://www.cnblogs.com/yxmx/articles/1612627.html终端tty、虚拟控制台、FrameBuffer的切换过程详解:http://blog.csdn.net/liusaoyue/article/details/5075745这个秋天,和百位大咖约个会!SACC2016作为国内最受欢迎的架构师盛会,2016第八届中国系统架构师大会(SACC)将于2016年10月27日-29日在北京万达索菲特大酒店撼世来袭! 大会以"架构创新之路"为主题,共设置两个主场分享时段,24个技术交流专场时段;邀请来自互联网、电子商务、金融、电信、政府、行业协会等20多个领域,150多位技术专家及行业领袖来分享他们的经验;并将吸引4000多名系统运维、架构师、及各种企业的IT决策人士参会,为他们提供最具价值的交流平台。限时折扣!8月31日前,订购SACC2016门票立享7.8折优惠!团购更有折上折!还不快来! 点击“阅读原文”立即购票!