[ Previous | Next | Contents | Glossary | Home | Search ]
AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs

Line Discipline Module (ldterm)

The ldterm line discipline is the common line discipline for terminals. This line discipline is POSIX compliant and also ensures compatibility with the BSD interface. The latter line discipline is supported only for compatibility with older applications. For portability reasons, it is strongly recommended that you use the POSIX line discipline in new applications.

This section describes the features provided by the ldterm line discipline. For more information about controlling ldterm, see "termios.h File" in AIX Version 4.3 Files Reference

Read the following to learn more about the ldterm line discipline:

Terminal Parameters

The parameters that control certain terminal I/O characteristics are specified in the termios structure as defined in the termios.h file. The termios structure includes (but is not limited to) the following members:

tcflag_t c_iflag Input modes
tcflag_t c_oflag Output modes
tcflag_t c_cflag Control modes
tcflag_t c_lflag Local modes
cc_t c_cc[NCCS] Control characters.

The tcflag_t and cc_t unsigned integer types are defined in the termios.h file. The NCCS symbol is also defined in the termios.h file.

Process Group Session Management (Job Control)

A controlling terminal distinguishes one process group in the session, with which it is associated, to be the foreground process group. All other process groups in the session are designated as background process groups. The foreground process group plays a special role in handling signals.

Command interpreter processes that support job control, such as the Korn shell (the ksh command) and the C shell (the csh command), can allocate the terminal to different jobs, or process groups, by placing related processes in a single process group and associating this process group with the terminal. A terminal's foreground process group can be set or examined by a process, assuming the permission requirements are met. The terminal driver assists in job allocation by restricting access to the terminal by processes that are not in the foreground process group.

Terminal Access Control

If a process that is not in the foreground process group of its controlling terminal attempts to read from the controlling terminal, the process group of that process is sent a SIGTTIN signal. However, if the reading process is ignoring or blocking the SIGTTIN signal, or if the process group of the reading process is orphaned, the read request returns a value of -1, sets the errno global variable to EIO, and does not send a signal.

If a process that is not in the foreground process group of its controlling terminal attempts to write to the controlling terminal, the process group of that process is sent a SIGTTOU signal. However, the management of the SIGTTOU signal depends on the TOSTOP flag which is defined in the c_lflag field of the termios structure. If the TOSTOP flag is not set, or if the TOSTOP flag is set and the process is ignoring or blocking the SIGTTOU signal, the process is allowed to write to the terminal, and the SIGTTOU signal is not sent. If the TOSTOP flag is set, the process group of the writing process is orphaned, and the writing process is not ignoring or blocking the SIGTTOU signal, then the write request returns a value of -1, sets the errno global variable to EIO, and does not send a signal.

Certain functions that set terminal parameters (tcsetattr, tcsendbreak, tcflow, and tcflush) are treated in the same manner as write requests, except that the TOSTOP flag is ignored; that is, the effect is identical to that of terminal write requests when the TOSTOP flag is set.

Reading Data and Input Processing

For terminals that operate in full-duplex mode, data can arrive even while output is occurring. Each terminal device file is associated with an input queue, where incoming data is stored by the system before being read by a process. The system imposes a limit (defined by the MAX_INPUT constant in the limits.h header file) on the number of bytes that can be stored in the input queue. When the input limit is reached, all the saved characters are thrown away without notice.

Two general kinds of input processing are available, depending on whether the terminal device file is in canonical or noncanonical mode. Additionally, input characters are processed according to the c_iflag and c_lflag fields. Such processing can include echoing, or the transmitting of input characters immediately back to the terminal that sent them. Echoing is useful for terminals that can operate in full-duplex mode.

A read request can be handled in two ways, depending on whether the O_NONBLOCK flag is set by an open or fcntl subroutine. If the O_NONBLOCK flag is not set, the read request is blocked until data is available or until a signal is received. If the O_NONBLOCK flag is set, the read request is completed, without blocking, in one of three ways:

The availability of data depends on whether the input processing mode is canonical or noncanonical. (The canonical or noncanonical modes can be set with the stty command.)

Canonical Mode Input Processing

In canonical mode input processing (ICANON flag set in c_lflag field of termios structure), terminal input is processed in units of lines. A line is delimited by a new-line (ASCII LF) character, an end-of-file (EOF) character, or an end-of-line (EOL) character. This means that a program attempting to read is blocked until an entire line has been typed or a signal has been received. Also, regardless of how many characters are specified in the read request, no more than one line is returned. It is not, however, necessary to read an entire line at once. Any number of characters can be specified in a read request without losing information.

During input, erase and kill processing is done. The ERASE character (Backspace, by default) erases the last character typed. The WERASE character (the Ctrl-W key sequence, by default) erases the last word typed in the current line, but not any preceding spaces or tabs. (A word is defined as a sequence of nonblank characters; tabs are regarded as blanks.) Neither the ERASE nor the WERASE character erases beyond the beginning of the line. The KILL character (the Ctrl-U sequence, by default) deletes the entire input line and, optionally, outputs a new-line character. All of these characters operate on a keystroke basis, independent of any backspacing or tabbing that might have been done.

The REPRINT character (the Ctrl-R sequence, by default) prints a new line followed by the characters from the previous line that have not been read. Reprinting also occurs automatically if characters that would normally be erased from the screen are fouled by program output. The characters are reprinted as if they were being echoed. Consequently, if the ECHO flag is not set in the c_lflag field of the termios structure, the characters are not printed. The ERASE and KILL characters can be entered literally by preceding them with the escape character \ (backslash), in which case, the escape character is not read. The ERASE, WERASE, and KILL characters can be changed.

Noncanonical Mode Input Processing

In noncanonical mode input processing (-ICANON flag set in c_lflag field of termios structure), input bytes are not assembled into lines, and erase and kill processing does not occur. The values of the MIN and TIME members of the c_cc array are used to determine how to process the bytes received. (The MIN and TIME values can be set with the stty command.)

MIN represents the minimum number of bytes that should be received when the read request is successful. TIME is a timer of 0.1-second granularity that is used to time-out burst and short-term data transmissions. If the MIN value is greater than the defined value of the MAX_INPUT constant, the response to the request is implementation-defined. The four possible values for MIN and TIME and their interactions are described in the subsequent paragraphs.

Case A: MIN > 0, TIME > 0

In this case, TIME serves as an interbyte timer, which is activated after the first byte is received and reset each time a byte is received. If MIN bytes are received before the interbyte timer expires, the read request is satisfied. If the timer expires before MIN bytes are received, the characters received to that point are returned to the user. If TIME expires, at least one byte is returned. (The timer would not have been enabled unless a byte was received.) The read operation blocks until the MIN and TIME mechanisms are activated by the receipt of the first byte or until a signal is received.

Case B: MIN > 0, TIME = 0

In this case, only MIN is significant; the timer is not significant (the value of TIME is 0). A pending read request is not satisfied (blocks) until MIN bytes are received or until a signal is received. A program that uses this case to read record-based terminal I/O can block indefinitely in the read operation.

Case C: MIN = 0, TIME > 0

In this case, because the value of MIN is 0, TIME no longer represents an interbyte timer. It now serves as a read timer that is activated as soon as the read request is processed. A read request is satisfied as soon as a byte is received or when the read timer expires. Note that if the timer expires, no bytes are returned. If the timer does not expire, the read request can be satisfied only if a byte is received. In this case, the read operation does not block indefinitely, waiting for a byte. If, after the read request is initiated, no byte is received within the period specified by TIME multiplied by 0.1 seconds, the read request returns a value of 0, having read no data.

Case D: MIN = 0, TIME = 0

In this case, the minimum of either the number of bytes requested or the number of bytes currently available is returned without waiting for more bytes to be input. If no characters are available, the read request returns a value of 0, having read no data.

Cases A and B exist to handle burst-mode activity, such as file transfer programs, where a program needs to process at least the number of characters specified by the MIN variable at one time. In Case A, the interbyte timer is activated as a safety measure. In Case B, the timer is turned off.

Cases C and D exist to handle single-character, limited transfers. These cases are readily adaptable to screen-based applications that need to know if a character is present in the input queue before refreshing the screen. In Case C, the timer is activated. In Case D, the timer is turned off. Case D leads to bad performance; but it is better to use it than doing a read request with setting the O_NONBLOCK flag.

Writing Data and Output Processing

When one or more characters are written, they are transmitted to the terminal as soon as previously written characters are displayed. (Input characters are echoed by putting them into the output queue as they arrive.) If a process produces characters more rapidly than they can be displayed, the process is suspended when its output queue exceeds a certain limit. When the queue has drained down to a certain threshold, the program is resumed.

Modem Management

If the CLOCAL flag is set in the c_cflag field of the termios structure, a connection does not depend on the state of the modem status lines. If the CLOCAL flag is clear, the modem status lines are monitored. Under normal circumstances, an open function waits for the modem connection to complete. However, if the O_NONBLOCK or CLOCAL flag is set, the open function returns immediately without waiting for the connection.

If the CLOCAL flag is not set in the c_cflag field of the termios structure and a modem disconnect is detected by the terminal interface for a controlling terminal, the SIGHUP signal is sent to the controlling process associated with the terminal. Unless other arrangements have been made, this signal causes the process to terminate. If the SIGHUP signal is ignored or caught, any subsequent read request returns an end-of-file indication until the terminal is closed. Any subsequent write request to the terminal returns a value of -1 and sets the errno global variable to EIO until the device is closed.

Closing a Terminal Device File

The last process to close a terminal device file causes any output to be sent to the device and any input to be discarded. Then, if the HUPCL flag is set in the c_cflag field of the termios structure and the communications port supports a disconnect function, the terminal device performs a disconnect.

Related Information

TTY Subsystem Overview.

The termios.h file.


[ Previous | Next | Contents | Glossary | Home | Search ]