📄 termios.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>General Terminal Interface</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><blockquote><center><h2><a name = "tag_008"> </a>General Terminal Interface</h2></center><xref type="1" name="genterm"></xref>This chapter describes a generalterminal interface that is provided tocontrolasynchronous communications ports.It is implementation-dependent whether itsupports network connections or synchronous ports or both.<h3><a name = "tag_008_001"> </a>Interface Characteristics</h3><h4><a name = "tag_008_001_001"> </a>Opening a Terminal Device File</h4>When a terminal device file is opened,it normallycauses the thread to wait untila connectionis established.In practice, application programsseldom open these files;they areopenedby special programs and become anapplication's standard input, output and error files.<p>As described in<i><a href="../xsh/open.html">open()</a></i>,openinga terminal device file with the O_NONBLOCKflag clear causes the threadto block until the terminal device is ready and available.If CLOCAL mode is not set,this means blocking until a connection is established.IfCLOCAL mode is set in theterminal, or the O_NONBLOCK flag is specified in the<i><a href="../xsh/open.html">open()</a></i>,the<i><a href="../xsh/open.html">open()</a></i>function returnsa file descriptor without waiting for a connectionto be established.<h4><a name = "tag_008_001_002"> </a>Process Groups</h4>A terminal may have a foregroundprocess group associated with it.This foregroundprocess group plays a special rolein handling signal-generating input characters, asdiscussed in<xref href=specchar><a href="#tag_008_001_009">Special Characters</a></xref>.<p>A command interpreter processsupporting job control canallocate the terminal to different jobs,or process groups, by placing related processes ina single process group and associatingthis process group with the terminal.A terminal'sforeground process group may be setor examined by a process, assuming the permissionrequirements are met;see<i><a href="../xsh/tcgetpgrp.html">tcgetpgrp()</a></i>and<i><a href="../xsh/tcsetpgrp.html">tcsetpgrp()</a></i>.Theterminal interface aids in this allocationby restricting access to the terminal by processesthat are not in the currentprocess group;see<xref href=termacc><a href="#tag_008_001_004">Terminal Access Control</a></xref>.<p>When there is no longer any process whose process ID or processgroup ID matches the process group ID of the foreground processgroup, the terminal will have no foreground process group.It is unspecified whether the terminal has a foreground processgroup when there is a process whose process ID matches theforeground process ID, but whose process group ID does not.No actions defined in this specification set, other than allocation of acontrolling terminal or a successful call to<i><a href="../xsh/tcsetpgrp.html">tcsetpgrp()</a></i>,will cause a process group to become the foreground process group ofthe terminal.<h4><a name = "tag_008_001_003"> </a>The Controlling Terminal</h4>A terminal may belong to aprocess as its controlling terminal.Each process of a sessionthat has a controlling terminal hasthe same controlling terminal.A terminal may be thecontrolling terminal for at most onesession.The controlling terminal for a session isallocated by the session leader inan implementation-dependent manner.If a session leaderhas no controlling terminal, and opensa terminal device file that is not already associatedwith a session without using theO_NOCTTY option (see<i><a href="../xsh/open.html">open()</a></i>),it isimplementation-dependent whether the terminal becomes thecontrolling terminal of thesession leader.If a process whichis not a session leader opens a terminal file, or theO_NOCTTY option is used on<i><a href="../xsh/open.html">open()</a></i>,then that terminal does not become the controllingterminal of the calling process.Whena controlling terminal becomes associated with asession, its foreground process group isset to the process group of the sessionleader.<p>The controlling terminal is inherited by a child process during a<i><a href="../xsh/fork.html">fork()</a></i>function call.A process relinquishes its controlling terminalwhen it creates a new session with the<i><a href="../xsh/setsid.html">setsid()</a></i>function;other processes remaining in the old session that hadthis terminal as their controlling terminal continue to have it.Upon the close of the last file descriptor in the system (whether ornot it is in the current session) associated with the controlling terminal,it is unspecified whether all processesthat had that terminal as their controlling terminal cease to have anycontrolling terminal.Whether and how a session leader can reacquire a controlling terminalafter the controlling terminal has been relinquished in this fashionis unspecified.A processdoes not relinquish its controlling terminal simply by closingall of its file descriptors associated with the controlling terminalif other processes continue to have it open.<p>When a controlling process terminates, thecontrolling terminal is dissociated from thecurrent session, allowing it to beacquired by a new session leader.Subsequent access tothe terminal by other processes inthe earlier session may be denied, with attempts toaccess the terminal treated as if amodem disconnect had been sensed.<h4><a name = "tag_008_001_004"> </a>Terminal Access Control</h4><xref type="3" name="termacc"></xref>If a process is in theforeground process group of its controlling terminal, read operationsare allowed, as described in<xref href=inproc><a href="#tag_008_001_005">Input Processing and Reading Data</a></xref>.Any attempts by a process in abackground process group to read fromits controlling terminal cause its processgroup to be sent a SIGTTINsignal unless one of the following special cases applies:if thereading process is ignoring or blockingthe SIGTTIN signal, or if the process group of thereading process is orphaned, the<i><a href="../xsh/read.html">read()</a></i>returns -1, with<i>errno</i>set to [EIO] and no signal issent.The default action of the SIGTTIN signalis to stop the process to which it is sent.See<i><a href="../xsh/signal.h.html"><signal.h></a></i>.<p>If a process is in theforeground process group of its controlling terminal, writeoperations are allowed as describedin<xref href=writedata><a href="#tag_008_001_008">Writing Data and Output Processing</a></xref>.Attempts by a process ina background process group to write to its controllingterminal will cause the process groupto be sent a SIGTTOU signal unless one of thefollowing special cases applies:if TOSTOPis not set, or if TOSTOP is set and the processis ignoring or blocking the SIGTTOUsignal, the process is allowed to write to theterminal and the SIGTTOU signal isnot sent.If TOSTOP is set, and the process group ofthe writing process is orphaned, andthe writing process is not ignoring or blocking the SIGTTOU signal, the<i><a href="../xsh/write.html">write()</a></i>returns -1, with<i>errno</i>set to [EIO] and no signal is sent.<p>Certain calls that set terminal parametersare treated in the same fashion as<i><a href="../xsh/write.html">write()</a></i>,except that TOSTOP is ignored; that is,the effect is identical to that of terminal writes whenTOSTOP is set (see<xref href=localmodes><a href="#tag_008_002_005">Local Modes</a></xref>,<i><a href="../xsh/tcdrain.html">tcdrain()</a></i>,<i><a href="../xsh/tcflow.html">tcflow()</a></i>,<i><a href="../xsh/tcflush.html">tcflush()</a></i>,<i><a href="../xsh/tcsendbreak.html">tcsendbreak()</a></i>,<i><a href="../xsh/tcsetattr.html">tcsetattr()</a></i>and<i><a href="../xsh/tcsetpgrp.html">tcsetpgrp()</a></i>).<h4><a name = "tag_008_001_005"> </a>Input Processing and Reading Data</h4><xref type="3" name="inproc"></xref>A terminal device associated with aterminal device file may operate in full-duplex mode,so that data may arrive evenwhile output is occurring.Each terminal device file hasanassociated with it,into which incoming data is stored by the systembefore being read by a process.The system may impose a limit, {MAX_INPUT}, on thenumber of bytes that may bestored in the input queue.The behaviour of the system whenthis limit is exceeded is implementation-dependent.<p>Two general kinds of input processingare available, determined by whether the terminaldevice file is in canonical modeor non-canonical mode.These modes are described in<xref href=canon><a href="#tag_008_001_006">Canonical Mode Input Processing</a></xref>and<xref href=noncanon><a href="#tag_008_001_007">Non-canonical Mode Input Processing</a></xref>.Additionally, input charactersare processed according to the<b>c_iflag</b>(see<xref href=inmodes><a href="#tag_008_002_002">Input Modes</a></xref>)and<b>c_lflag</b>(see<xref href=localmodes><a href="#tag_008_002_005">Local Modes</a></xref>)fields.Suchprocessing can includewhich ingeneral means transmitting input charactersimmediately back to the terminal whenthey are received from the terminal.This isuseful for terminals that can operatein full-duplex mode.<p>The manner in which data isprovided to a process reading from a terminal device file isdependent on whether the terminal fileis in canonical or non-canonical mode,and on whether or not the O_NONBLOCK flag is set by<i><a href="../xsh/open.html">open()</a></i>or<i><a href="../xsh/fcntl.html">fcntl()</a></i>.<p>If theO_NONBLOCK flag is clear, then theread request is blocked until data is availableor a signal has been received.If theO_NONBLOCK flag is set, then the read requestis completed, without blocking, in oneof three ways:<p><ol><p><li>If there is enough data available to satisfy theentire request, the<i><a href="../xsh/read.html">read()</a></i>completes successfully and returns the number of bytes read.<p><li>If there is not enough data available to satisfythe entire request, the<i><a href="../xsh/read.html">read()</a></i>completes successfully, having read as much data as possible, andreturns the number of bytes it was able to read.<p><li>If there is no data available, the<i><a href="../xsh/read.html">read()</a></i>returns -1, with<i>errno</i>set to [EAGAIN].<p></ol><p>When data is available depends onwhether the input processing mode is canonical ornon-canonical.The following sections,<xref href=canon><a href="#tag_008_001_006">Canonical Mode Input Processing</a></xref>and<xref href=noncanon><a href="#tag_008_001_007">Non-canonical Mode Input Processing</a></xref>describeeach of these inputprocessing modes.<h4><a name = "tag_008_001_006"> </a>Canonical Mode Input Processing</h4><xref type="3" name="canon"></xref>In canonical mode input processing,terminal input is processed in units of lines.A lineis delimited by a newline character (NL),an end-of-file character (EOF), or an end-of-line(EOL) character.See<xref href=specchar><a href="#tag_008_001_009">Special Characters</a></xref>for more information on EOFand EOL.This means that aread request will not return until an entire line hasbeen typed or a signal hasbeen received.Also, no matter how many bytes are requestedin the<i><a href="../xsh/read.html">read()</a></i>call, at mostone line will be returned.It is not, however, necessary to read a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -