Jay Taylor's notes

back to listing index

The TTY demystified

[web search]
Original source (www.linusakesson.net)
Tags: tty www.linusakesson.net
Clipped on: 2017-02-05

The TTY demystified

Image (Asset 2/12) alt= speed 38400 baud; rows 73; columns 238; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

The -a flag tells stty to display all settings. By default, it will look at the TTY device attached to your shell, but you can specify another device with -F.

Some of these settings refer to UART parameters, some affect the line discipline and some are for job control. All mixed up in a bucket for monsieur. Let's have a look at the first line:

speedUARTThe baud rate. Ignored for pseudo terminals.
rows, columnsTTY driverSomebody's idea of the size, in characters, of the terminal attached to this TTY device. Basically, it's just a pair of variables within kernel space, that you may freely set and get. Setting them will cause the TTY driver to dispatch a SIGWINCH to the foreground job.
lineLine disciplineThe line discipline attached to the TTY device. 0 is N_TTY. All valid numbers are listed in /proc/tty/ldiscs. Unlisted numbers appear to be aliases for N_TTY, but don't rely on it.

Try the following: Start an xterm. Make a note of its TTY device (as reported by tty) and its size (as reported by stty -a). Start vim (or some other full-screen terminal application) in the xterm. The editor queries the TTY device for the current terminal size in order to fill the entire window. Now, from a different shell window, type:

stty -F X rows Y

where X is the TTY device, and Y is half the terminal height. This will update the TTY data structure in kernel memory, and send a SIGWINCH to the editor, which will promptly redraw itself using only the upper half of the available window area.

The second line of stty -a output lists all the special characters. Start a new xterm and try this:

stty intr o

Now "o", rather than ^C, will send a SIGINT to the foreground job. Try starting something, such as cat, and verify that you can't kill it using ^C. Then, try typing "hello" into it.

Occasionally, you may come across a UNIX system where the backspace key doesn't work. This happens when the terminal emulator transmits a backspace code (either ASCII 8 or ASCII 127) which doesn't match the erase setting in the TTY device. To remedy the problem, one usually types stty erase ^H (for ASCII 8) or stty erase ^? (for ASCII 127). But please remember that many terminal applications use readline, which puts the line discipline in raw mode. Those applications aren't affected.

Finally, stty -a lists a bunch of switches. As expected, they are listed in no particular order. Some of them are UART-related, some affect the line discipline behaviour, some are for flow control and some are for job control. A dash (-) indicates that the switch is off; otherwise it is on. All of the switches are explained in the stty(1) man page, so I'll just briefly mention a few:

icanon toggles the canonical (line-based) mode. Try this in a new xterm:

stty -icanon; cat

Note how all the line editing characters, such as backspace and ^U, have stopped working. Also note that cat is receiving (and consequently outputting) one character at a time, rather than one line at a time.

echo enables character echoing, and is on by default. Re-enable canonical mode (stty icanon), and then try:

stty -echo; cat

As you type, your terminal emulator transmits information to the kernel. Usually, the kernel echoes the same information back to the terminal emulator, allowing you to see what you type. Without character echoing, you can't see what you type, but we're in cooked mode so the line editing facilities are still working. Once you press enter, the line discipline will transmit the edit buffer to cat, which will reveal what your wrote.

tostop controls whether background jobs are allowed to write to the terminal. First try this:

stty tostop; (sleep 5; echo hello, world) &

The & causes the command to run as a background job. After five seconds, the job will attempt to write to the TTY. The TTY driver will suspend it using SIGTTOU, and your shell will probably report this fact, either immediately, or when it's about to issue a new prompt to you. Now kill the background job, and try the following instead:

stty -tostop; (sleep 5; echo hello, world) &

You will get your prompt back, but after five seconds, the background job transmits hello, world to the terminal, in the middle of whatever you were typing.

Finally, stty sane will restore your TTY device configuration to something reasonable.

Conclusion

I hope this article has provided you with enough information to get to terms with TTY drivers and line disciplines, and how they are related to terminals, line editing and job control. Further details can be found in the various man pages I've mentioned, as well as in the glibc manual (info libc, "Job Control").

Finally, while I don't have enough time to answer all the questions I get, I do welcome feedback on this and other pages on the site. Thanks for reading!

Posted Friday 25-Jul-2008 19:46

Discuss this page

Disclaimer: I am not responsible for what people (other than myself) write in the forums. Please report any abuse, such as insults, slander, spam and illegal material, and I will take appropriate actions. Don't feed the trolls.

Jag tar inget ansvar för det som skrivs i forumet, förutom mina egna inlägg. Vänligen rapportera alla inlägg som bryter mot reglerna, så ska jag se vad jag kan göra. Som regelbrott räknas till exempel förolämpningar, förtal, spam och olagligt material. Mata inte trålarna.

Anonymous
Sun 24-Aug-2008 23:36
Very good and informative article for a complex topic
- the tty system really gets demystified here.

Only a small correction:
Your statement "icanon switches between raw and cooked mode" is not completely true.

'stty -icanon' still interprets control characters such as Ctrl-C whereas 'stty raw' disables even this and is the real raw mode.

Greetings,
-Andreas.
lft
Linus Åkesson
Fri 29-Aug-2008 18:42
Very good and informative article for a complex topic
- the tty system really gets demystified here.

Only a small correction:
Your statement "icanon switches between raw and cooked mode" is not completely true.

'stty -icanon' still interprets control characters such as Ctrl-C whereas 'stty raw' disables even this and is the real raw mode.

Thanks!

Yes, you're quite right. I've fixed it.
Anonymous
Wed 26-Nov-2008 08:13
I have been trying to chew through documents in every unix book possible to explain how the TTY system really works. Your article has been more informative than all of these books. Without a programmers knowledge of how a unix kernel works, it's quite difficult for a System Administrator to understand concepts that are so crucial to their jobs. Thank you for your writing such a great article. Your help is truly appreciated.
Anonymous
Wed 10-Dec-2008 13:27
I really admire you!! I'm not use to post, but this article was by far the best I ever read about tty, i read many articles and even books but nothing so clear like this.
Keep doing things like this please...

Excelente!
Muchas Gracias
Fede Tula
Anonymous
Sat 20-Dec-2008 06:20
Good article. A few years ago, after some experimentation, I wrote up some TTY stuff for myself, because I couldn't find any good documentation. For example, I never understood why process group leaders (and consequently also session leaders) are prevented from calling setsid(2). (See the manual for what setsid(2) does.) I think I can explain it now.

The process P is a pg leader if P.PID = P.PGID. In the example of the article, "Job" means process group, and ls (103) is a process group leader:

ls.PID = 103
ls.PGID = 103
ls.SID = 101
ls.CTTY = /dev/pts/0

Suppose we allow ls to call setsid(2). This would have the following consequences:

ls.PID = 103 # unchanged
ls.PGID = 103 # set to ls.PID, but in fact this is no change!
ls.SID = 103 # set to ls.PID
ls.CTTY = <none>

Now ls is session leader (ls.SID = ls.PID), and ls is process group leader (ls.PGID = ls.PID).

At this point, however, sort (104) would belong to a process group (103) whose leader's (ls's) SID (103) doesn't match sort's SID (101)!

sort.PID = 104
sort.PGID = 103
sort.SID = 101

the pg leader for pg 103 is ls (103):
ls.PID = ls.PGID = 103 = sort.PGID
however
ls.SID = 103 != 101 = sort.SID

We have two processes in the same process group belonging to different sessions!

ls is prevented from calling setsid() because as current process leader its PGID doesn't change when it is set to its PID, while its SID changes. Thus it would leave the session while staying in the process group.

Sort, OTOH, can call setsid(), becuase it also leaves the process group:
sort.PID=104
sort.PGID=104 # leaves process group too
sort.SID=104
sort.CTTY=<none>

fork(2)-ing and calling setsid(2) in the child helps, because the child gets a new PID, which will be different from any PGID of the parent (as that PGID was the PID of some process), and so when the child calls setsid(2), the child.PGID := child.PID operation will actually change the child's inherited PGID and so the child will be able to leave the process group.

Right after fork():
parent.PGID = some_old_PID
child.PID = new_PID
child.PGID = parent.PGID = some_old_PID

The child calls setsid():
child.PGID = child.PID = new_PID != some_old_PID = parent.PGID

A session leader *could* call setsid(), despite also being a process group leader, since neither its PGID nor its SID would change. However, its CTTY would be set to <none>, and this would result in a situation where the original controlling process (= a session leader with a CTTY), for example, your shell, has no more access to the terminal!

Furthermore, there is the rule that when a controlling process dies, each session member (each process P with P.SID = SL.SID) loses access to the terminal (and possibly get a SIGHUP on the next read/write). This clearly shows the intent that no session member shall have access to the terminal when the session leader has none. This principle would be violated if the current session leader could detach from the CTTY by calling setsid(). (Or all session members would have to lose access to the CTTY, just as if the session leader died.)
Anonymous
Thu 25-Dec-2008 20:19
good job & best introductory for TTY
Anonymous
Mon 20-Apr-2009 10:28
amazing man , this is amazing !!! best tty article ever
Anonymous
Tue 2-Jun-2009 21:27
very good article!
Anonymous
Mon 15-Jun-2009 18:10
Holy Smokes dude, those were the days!

Riff
www.absolute-anonymity.us.tc
Anonymous
Mon 15-Jun-2009 21:59
I rarely comment on Blogs as I usually feel my input would not be necessary, but I must say this has been one of the best written and descriptive documents I have had the pleasure of learning from
Anonymous
Tue 16-Jun-2009 00:41
ZZZZZZZZZZZZZZZZZZZZZZZZZZ to long, jesus christ write up a summery for us lazy people
Anonymous
Tue 16-Jun-2009 02:22
I've been looking for an article like this for a very long time... thanks very much for putting this together!
Anonymous
Tue 16-Jun-2009 04:38
Great summary, and very useful. Thanks!
Anonymous
Tue 16-Jun-2009 08:57
Just freaking great!

script to make commands like less (more) adapt to changed screen size.
It even tries to exit from the command leaving the cursor on the "correct" place.
The kludge should work well with anything, that ought to be updated because of
a change in terminal window size.

CAVEATS
It's written for Unix under Mac OsX, doesn't really know if tput are implemented under Linux.

Well here we go. I'm sorry for the loss of tabs, it should have been indented.
I have used this for a year and a half and it really works.

#! /bin/bash
export LESS=" -I -r -f -J -S -g -M -x 4"
# -I ignore case when searching
# -r "raw" do not preparate ctrl-chars,
# -f force open special files (may be binary) BEWARE OF ANSISEQUENCES.
# -J show status column
# -S chop long lines.
# -g highlight on last hit in the search.
# -M Most Verbose status column...
# -x 4 tabspacing = 4
# -------------------------------------- the kludge starts here.................
ORIGLINES=$LINES

ESC=`printf "\e"`
ScreenRedraw_off=`echo -n "$ESC""[8m"`
ScreenRedraw_on=`echo -n "$ESC""[0m"`

function OkayScreen()
{
export PS1="" # Turns off the prompt to avoid cluttering..
echo -n ${ScreenRedraw_off}
CURLINES=`bash -i < ~/bin/kludge.bash `
# ^^^^^^^^^^^ NB! the path where kludge.bash should be placed.
if [ $CURLINES -gt $ORIGLINES ] ; then
TO_SKIP="$(expr "$CURLINES" '-' "$ORIGLINES")"
if [ $TO_SKIP -lt 3 ] ; then
TO_SKIP="$(expr "$TO_SKIP" '-' '2')"
else
TO_SKIP="$(expr "$TO_SKIP" '-' '1')"
fi
tput cuu 1 #cursor up one line
echo -n ${ScreenRedraw_on}
echo -n "\$" #restores prompt
echo -n ${ScreenRedraw_off}
tput cud $TO_SKIP
echo -n ${ScreenRedraw_on}
echo # activate cli correct position.
else
tput cuu 2
echo ${ScreenRedraw_on}
fi
}
trap OkayScreen SIGWINCH
# if [ -t 0 ] ; then # /* this enables syntax highlighting */
# $VIMRUNTIME/macros/less.sh $@ /* After tinkering with vim */
# else
/usr/bin/less $@
# fi
trap '' SIGWINCH
# cp ./tmp/.vimrc~
Anonymous
Tue 16-Jun-2009 09:05
Just freaking great!

Forgot the innerpart, which makes it all work ....
This is a second script called kludge.bash which I have in my ~/bin folder.
Needs to execute this to get the changed winsize in a new process since at
least bash 2.05a didn't update the LINE variable in active process in the terminal window.



#! /bin/bash
# ### SYS Finds the number of lines in a window after window rechange - less... !#
# kludge.scr - to be placed in the ~/bin folder is the inner workings of the bash script named less
PS1=""
shopt -s checkwinsize
echo $LINES
Anonymous
Wed 17-Jun-2009 01:58
Thanks for the great article!

I'd recommend adding some info about the *wide* spread myth of parent's death triggering SIGHUPs for all its children.

Recall that the related behavior only applies to session leaders, and is triggered in any of the two following cases:

#1:
IF session leader exiting
..IF it has ctty
....send SIGHUP to foreground PG
..ELSE
....send SIGHUP to foreground PG at last time it had ctty

#2:
IF session leader detaching (TIOCNOTTY)
..send SIGHUP to foreground PG

--JuanJo
Anonymous
Wed 17-Jun-2009 12:42
Actually, to be more precise:
#1:
IF session leader exiting
..IF it has ctty
....send SIGHUP to foreground PG
..ELSE
....send SIGHUP,SIGCONT to foreground PG at last time it had ctty

#2:
IF session leader detaching (TIOCNOTTY)
..send SIGHUP,SIGCONT to foreground PG


See: http://google.com/codesearch/p?hl=en&sa=N&cd=2&ct=rc#p4tPAkVsQ_c/linux-2.2.26/drivers/char/tty_io.c&l=537"

--JuanJo AKA jjo
lft
Linus Åkesson
Sun 21-Jun-2009 14:58
A reader pointed out that VT (in VT-100) stands for "video terminal", not "virtual terminal". This has been fixed.
Anonymous
Thu 30-Jul-2009 15:07
Thanks for the article, really helped !
Anonymous
Thu 30-Jul-2009 21:09
Thankyou, I found this to be an excellent TTY primer.
Anonymous
Sat 1-Aug-2009 00:14
Bookmarked.
Good tips =)
Anonymous
Mon 10-Aug-2009 09:21
Very nice. I'm currently (trying to) write a Unix-clone mostly from scratch, and this is a good resource on how TTYs are supposed to work.
Anonymous
Sun 16-Aug-2009 17:32
Hi--how are things in Sweden?

Every once in a while I get up the ambition to complain about the width of text on a web page, and you're the lucky winner today--sorry ;-)

This could be a good article--from the looks of it, it probably is--but why is it (and so many other web pages today) so wide?

Checking one line at random, it is 130 characters wide:

echo "Meanwhile, however, the computers — still quite large and primitive, but able to multitask — were becoming powerful enough to" | wc
1 20 130

Oh, and I'm ignoring the stuff in the left hand panel / column--I simply horizontally scroll so that panel is not visible.

I have three choices if I want to read your article:

* horizontally scroll on each line

* set the type size very small (or zoom out), so an entire line appears on the screen, then use a magnifying glass

* copy and paste the text to a file and read it in an editor--possibly deleting hard line breaks to let the text flow better.

Ideally, and I've seen it done this way, so I believe it can be done:

* the text should be arranged to wrap to the width of the (reader's) window

* if there are long lines of code (pre-formatted text), or wide pictures, or something like that, the other text should still wrap to the width of the reader's window, although he'll have to horizontally scroll to see the full picture or code or whatever. (This is the part I'm specifically referring to as having seen done, but I can't remember any details (like an example, or how to do it--I'll try to pay attention and find some).

Anyway, sorry for the rant--thanks for making the effort to create and disseminate pages with information like this!

Randy Kramer
Anonymous
Fri 4-Sep-2009 11:24
Wonderful article.the explanation is meticulous and elegant. Thank you.
Anonymous
Sat 12-Sep-2009 16:09
GREAT ARTICLE!!! Read articles and books on Terminal I/O and none as clear as this summary.
Well Done!
Anonymous
Wed 23-Sep-2009 16:26
Thanks for a good article
Anonymous
Sat 17-Oct-2009 10:12
Thanks for this very good article
Anonymous
Tue 3-Nov-2009 18:53
Thanks for such a wonderful article. I am a beginner in tty and your article was of great help!!
Anonymous
Thu 5-Nov-2009 20:54
nice article..thanx for sharing ur knowledge :)
Anonymous
Tue 22-Dec-2009 21:58
Thanks a lot for this precise article ! This has been very useful to me... (reading an external device on RS232 from bash...)
Anonymous
Tue 9-Feb-2010 08:34
Thank you for sharing such a great artical. -Hai
Anonymous
Thu 15-Apr-2010 11:08
Thanks a lot for this great article
Anonymous
Tue 4-May-2010 10:36
Wonderful article! I never learned much about unix process stuff (apart from little practical things like piping, detaching, killing, etc), but now I feel like I'm starting to see what's behind the magical terminals! Thank you :)
Anonymous
Wed 12-May-2010 03:00
"daemonizing" a process - detaching from the tty - would probably also fit here. -rurban
Anonymous
Thu 13-May-2010 03:17
What does TTY mean?
lft
Linus Åkesson
Thu 13-May-2010 17:33
What does TTY mean?

TeleTYpe.
Anonymous
Wed 14-Jul-2010 05:21
What Randy Kramer said.
Anonymous
Sat 24-Jul-2010 16:28
excellent article, well done!
Anonymous
Sun 25-Jul-2010 02:31
The process of daemonizing is covered elsewhere, easy to Google. It involved forking, killing the parent, then calling setsid() in the child, and optionally chdir()ing to / and closing stdin/stdout/stderr.

I am currently working on writing a toy OS, and this was very useful in its treatment of the basic structure of the TTY subsystem. Thanks.
ralph
Ralph Corderoy
Sun 25-Jul-2010 16:22
Nice article, various points...

The erase and kill characters used to be # and @, and as you were printing on paper there was no rubbing out, so you might see

$ ls @wc -l /etvc##c/passwd
42 /etc/passwd
$

where the `@' was killing the whole line entered so far and the `##' was erasing the preceding `vc'.

It's only modern shell that provide line editing, hence shell history substitutions like `!!' and `!$' existing. If /bin/sh is a plain old non-line-editing shell on your system then you can see the difference in tty settings by using `stty -a' from another terminal to capture the differences. Don't run stty(1) from, e.g., the bash shell since the shell will alter the tty settings before running stty. Here, bash has the literal next character, lnext, being undefined and turns off -icrnl, -icanon, and -echo.

The above example of # and @ was achieved by

$ sh
$ stty erase \# kill @ -crterase -echok
$ ls @wc -l /etvc##c/passwd
42 /etc/passwd
$ stty sane
$ exit
$

where /bin/sh is dash(1) on this Ubuntu system.

"Write permissions to the device file are required, so when a user logs in on a particular TTY, that user must become the owner of the device file." I think it's read permission that's required to alter a tty's settings. It did used to be write, in the very early days, but since write(1) and mesg(1) meant users could write to one another's terminals it also meant they could alter their settings. Much fun could be had with changing erase to `e' for a second and back again at random intervals whilst the user was trying to type. So it was switched to require read permission which only the owner of tty normally has. This can be seen in stdin of stty needing to be re-directed to specify the terminal, and not stdout, e.g. `stty -a </dev/pts/1'.

Flow control, e.g. ^S and ^Q, existed long before the signals for job control. IIRC, it was Berkeley that added all the ^Z stuff and related signals, it wasn't Bell Labs.

The Linux kernel doesn't bother to implement all of the normal control characters. Flush is one that's missing, IIRC, which is set with stty's `eol2'. It's a shame.

Cheers,
Ralph.

P.S. There's a typo, `1970:s'.
lft
Linus Åkesson
Sun 25-Jul-2010 18:14

ralph wrote:

Nice article, various points...

Thanks! That was very interesting. It hadn't occured to me that erase/kill would be usable without interactive line editing, but it makes sense.

ralph wrote:

P.S. There's a typo, `1970:s'.

Changed to 1970s.
Anonymous
Mon 2-Aug-2010 22:22
How do you restart bash in tty0? For some reason my bash has died in all tty's (Ctrl-Alt-Fn). I does not show the login prompt anymore.

How can I restart it? I cannot reboot the machine. I can ssh into it from another machine.
lft
Linus Åkesson
Thu 5-Aug-2010 10:10
How do you restart bash in tty0? For some reason my bash has died in all tty's (Ctrl-Alt-Fn). I does not show the login prompt anymore.

How can I restart it? I cannot reboot the machine. I can ssh into it from another machine.

I don't know what's wrong in your particular case, but it's init (pid 1) that's supposed to (re-)start the login program in each terminal. You can modify init's configuration at runtime by editing /etc/inittab and then doing "kill -HUP 1". But it's probably not an error in the configuration file, so use ps(1) to investigate what processes are running in the terminals.
Anonymous
Tue 10-Aug-2010 01:41
Great article.
For another great source on TTY devices you can go to:
http://publib.boulder.ibm.com/infocenter/aix/v6r1/index.jsp?topic=/com.ibm.aix.genprogc/doc/genprogc/ttysys.htm

Enjoy,
Ori
Anonymous
Fri 13-Aug-2010 12:59
Very good article...
We want the part II were the concepts gets even closer to the Linux implementation...

just an example:
an xterm session under ssh when resized makes the TTY device to adjust the terminal size and generate SIGWINCH signal for the running app to know about the change...
but if the xterm is under a serial line when resize occurs NOTHING happens (not kernel side size update, not SIGWINCH signal)... I know under serial line xterm does not communicate the size change, well where is the the place to patch in order to solve this, from the xterm side it would be very easy to send a escape sequence telling the TTY driver the new size but this driver should be patched for catching it and react as in the ssh case....

Thanks for your out of ordinary (little or inexistant content + lot of google adds) article. I hope we can get a deeper version someday.

Pat
Anonymous
Sat 21-Aug-2010 05:00
Hi,

A very interesting article. I have just published a related article on the terminals in French : http://www.etud.insa-toulouse.fr/~mcheramy/wordpress/?p=198
(And if you don't understand french, there are few interesting links in english at the end)

Thanks.
Max.
Anonymous
Fri 27-Aug-2010 14:22
How to Modify the tty driver so that control-W erases the previous word typed by the user.
Anonymous
Mon 25-Oct-2010 15:14
Nithin: thanks for the page....i got a lot of information from ur web page...
Anonymous
Wed 1-Dec-2010 15:45
Great article, very imformative please explain how to prevent UART overrun
Anonymous
Thu 9-Dec-2010 19:19
Hi there ! Would it be possible to create a raw tty which redirects input to some fifo and another tty to read from that fifo ? I know that is extrange what I'm asking...I'm building a z80 emulator and if this is possible would help me to test the input/output routines of the emulator without writting code to read/display...
Anonymous
Sun 26-Dec-2010 22:32
very helpful indeed Thanks a ton.
Anonymous
Thu 6-Jan-2011 11:46
Good article.Thanks you very much

/Renjith G
Anonymous
Wed 12-Jan-2011 14:12
this article is very good .
now i have a question.
linux use /dev/console in booting before the init called . so i write a progrm that run after kernel booted and pass my progrm with option init in boot parameter and bypass init program so the first program that run is my program.
this is my question : how i correct my program to resive signels from /dev/consoole?
Anonymous
Tue 25-Jan-2011 10:19
Awesome article! Its been very helpful in understanding TTY layer as whole.

Thanks
Ambresh
Anonymous
Wed 9-Feb-2011 18:56
Hi, this seems to be an excellent technical article, however is there any chance you can provide a broad description of TTY for a non technical audience. e.g. explain in simple language the origin, function and implications of TTY.
You may not consider this your remit, which is fair enough.
Anonymous
Wed 16-Feb-2011 08:57
Great Job, learned much
Anonymous
Wed 23-Feb-2011 21:35
Very good document, thanks!
Anonymous
Fri 25-Feb-2011 23:10
great article. I have known the term 'TTY' for a long time and never knew what the history behind it. Seeing how even current day UNIX system still make use of TTY is quite interesting to me. Now I feel more 'connected' when I press a key on my keyboard and seeing a character appear on the monitor screen. A good read.
Anonymous
Tue 1-Mar-2011 22:42
Wow! The name of the article says it all, but for real this time! Thanks!

Regards,
Newman
Anonymous
Fri 18-Mar-2011 21:18
Amazing article. Thanks so much for putting this together. I've been using Linux for 10 years, and this taught me quite a bit.
Anonymous
Mon 11-Apr-2011 20:08
ZZZZZZZZZZZZZZZZZZZZZZZZZZ to long, jesus christ write up a summery for us lazy people
if(easilyBored || notInterested){
goToAnotherSiteThen();
} else {
try {
readAndLearn();
}
catch(DontUnderstandException dde){
wiki(dde.getSubject());
}
finally {
enjoyArticle();
}
}
Anonymous
Wed 29-Jun-2011 14:19
Fantastic expalanation. Keep up the good work
Anonymous
Fri 1-Jul-2011 04:12
Greatly informative article. Of course, Bookmarked.
Anonymous
Mon 11-Jul-2011 16:18
Very informative article; Thanks for knowledge sharing ...
Anonymous
Wed 7-Sep-2011 15:48
Fucking awesome. Thanks a lot for the info!
Anonymous
Wed 7-Sep-2011 15:49
Fucking awesome. Thanks a lot for the info!
Anonymous
Fri 9-Sep-2011 16:25
Excelent article, I started to read it at work, now I'm printing it to read it more carefuly on my way home. Thanks for sharing all this!
Anonymous
Mon 19-Sep-2011 22:32
Thank you for sharing your knoledge so generously!
Anonymous
Tue 27-Sep-2011 08:01
thanks for the works!
Anonymous
Fri 30-Sep-2011 16:39
Hi,

I am currently working on Solaris sparc 10, where i am seeing below problem with default setting.

Shell is allowing me to type in only 256 characters. e.g.

bash-3.00$ cat
SunStudio12u1-SunOS-SPARC-tar-MLSunStudio12u1-SunOS-SPARC-tar-MLSunStudio12u1-SunOS-SPARC-tar-MLSunStudio12u1-SunOS-SPARC-tar-MLSunStudio12u1-SunOS-SPARC-tar-MLSunStudio12u1-SunOS-SPARC-tar-MLSunStudio12u1-SunOS-SPARC-tar-MLSunStudio12u1-SunOS-SPARC-tar-MLS

If i attempt to input more data nothing happens. After going through your notes it seems like i am crossing line buff limit. If yes can you please suggest how i can increase this limit


Minal Patil
Anonymous
Mon 24-Oct-2011 21:11
can anybody give exact command for sending messages via linux?
Anonymous
Wed 2-Nov-2011 03:21
Hi,
can you please suggest how i can increase this limit

http://tinyurl.com/6yql6r8
Anonymous
Mon 14-Nov-2011 21:33
From your disclaimer: “Please report any abuse, such as insults, slander, spam and illegal material”

Just a note: “slander” only applies to verbal communication. For written, use the term “libel” instead. :-D
Anonymous
Tue 22-Nov-2011 16:04
Thanks for writing the article. It was a bit heavy going at times, but it's helped me understand some of what is going on with the whole terminal thing.
Anonymous
Mon 12-Dec-2011 04:22
This was enormously informative and helpful. I've bookmarked it for future use. Thank you!
Anonymous
Wed 21-Dec-2011 04:20
I am trying to solve a mystery with certain services started at reboot or via a root crontab.
These sercvices seem to fail, unless I login to a shell that has a proper TTY setup
(eg: PuTTY of any type of ssh session)
If I start up these services with a proper tty, they work and continue to run.
My theory is that on reboot or via root crontab there is no tty.

How can I create a psuedo tty master/slave pair underwhich to run these services so they work, with me doing a manual ssh login......?

Larry Wichter
Anonymous
Thu 12-Jan-2012 15:37
Very informative article, thanks very much for sharing it with the web.
Anonymous
Wed 18-Jan-2012 01:00
Real great article thanks very much.
Anonymous
Mon 20-Feb-2012 05:35
Superb post. Thank you.
Anonymous
Mon 5-Mar-2012 18:15
Great article! I'm currently running a real Teletype ASR-33 on a serial port with Ubuntu. I have to use stty to set the proper parameters for an uppercase-only terminal, change tabs to spaces, etc. Is there a way to determine what port I'm logging into (ttyS0? S1? S2, S3 or S4?) Right now, I have to run stty on all ports and get an access denied error for any port I'm not connected to. I'd like to find out what port I'm on and only run the stty on that port. Thanks for your time!
Anonymous
Mon 2-Apr-2012 14:21
Is there a way to determine what port I'm logging into (ttyS0? S1? S2, S3 or S4?)
Sounds like a job for the tty(1) command.
Anonymous
Mon 2-Apr-2012 19:14
I think the commentary about line editing being part of the operating system to simplify programs is potentially misleading - the simplification of applications is a side effect.

If you're connected something like a VT100, the terminal handles line editing, and programs send control codes to the terminal to switch between cooked mode and raw mode. When virtual terminals were implemented, this functionality became part of the operating system to maintain compatibility with existing software.
Anonymous
Thu 12-Apr-2012 01:15
This is a really great job! Really helped with background jobs writing to stdout and going to sleep (putting 'stty -tostop;' before background command). It takes much time to generate meaningful examples - much appreciated!
Anonymous
Sat 14-Apr-2012 13:54
Thanks !!
Anonymous
Sat 14-Apr-2012 14:21
Great Article and good writing. I came looking for one answer (which I found) and found that I could not stop reading (normally a sign of a good article).

Having been around as the last of the Telex's died out, having designed UART circuits, worked on Xenix and Unix Systems, and naturally Linux systems, your article plugged some reasonable holes I had on the subject...

Thanks again...
Anonymous
Tue 17-Apr-2012 18:35
This is a very good article and I've read and enjoyed it multiple times (no, really!).

One thing I think it does lack is that when it's talking about signals, it should mention about Linux's new(ish) signalfd.
Anonymous
Sat 5-May-2012 16:56
Great and insightful article!!!
From this article I gain general knowledge about architecture
of Linux devices(LowLevelDriver<->LineDiscipline<->HighLevelDriver).
It's really a core of Linux I/O.

Thank you very much.
___________________________
Sichkar Dmytro from Ukraine
dmbios@mail.ru
Anonymous
Wed 16-May-2012 01:52
Thank you so much. Great article!
Anonymous
Thu 24-May-2012 17:22
Good stuff, thanks!
Anonymous
Mon 28-May-2012 04:48
Thanks.
whizziwig
David Blackman
Mon 4-Jun-2012 19:16
Can you explain why sometimes my terminal gets diseased, and the line I'm typing overwrites itself, or the cursor is in the wrong place, or similar symptoms? is there a way to fix it?
Anonymous
Mon 4-Jun-2012 21:11
Love the article, but I did have a comment -- namely that I wouldn't describe the DEC VT100 as solid state because of its CRT. Again, though, the article is great! Thanks!
Anonymous
Tue 5-Jun-2012 09:20
Absolutely terrific!
Anonymous
Tue 5-Jun-2012 21:07
That brings back old memories. The TTY shown is from the 1940's were 5 bit rather than the later 8 bit (really 7 bit plus parity) machines seen in the late 60's and 70's. Next retro core memory come back.

RC Roeder
Anonymous
Wed 6-Jun-2012 05:02
Fantastic! awesome article you should think about to write a book or something, It clarified me some things, even having 5+ years of Unix experience.

Al Mejida.
Anonymous
Tue 12-Jun-2012 20:59
Thanks a lot for writing this!
Anonymous
Fri 6-Jul-2012 17:46
"yes" program , produced in 2009 !

Thank you David McKenzie for your contribution to open source community !

I am just wondering what in your background that enabled the FSF to accept such worthless contribution ?

Member of what masonic lodge or what church or son of a war hero or billonarties you have to be so they accept that piece of crap ?

For reference:

yes command - otputs a line on tty until killed !

coded and added to Linux in 2009.
Anonymous
Fri 6-Jul-2012 17:48
"yes" program , produced in 2009 !

Thank you David McKenzie for your contribution to open source community !

I am just wondering what in your background that enabled the FSF to accept such worthless contribution ?

Member of what masonic lodge or what church or son of a war hero or billonarties you have to be so they accept that piece of crap ?

For reference:

yes command - otputs a line on tty until killed !

coded and added to Linux in 2009.

interesing point although might be harsh on a boy who modified a hello world program and managed to add it to Linux.
Anonymous
Fri 6-Jul-2012 17:50
"yes" program , produced in 2009 !


I disagree an entire source code of the yes in TARball may be a good example of how to add a new command to Linux, sort of like a new command template.



www.LinuxCAD.com
lft
Linus Åkesson
Fri 6-Jul-2012 18:07
"yes" program , produced in 2009 !
...
Member of what masonic lodge or what church or son of a war hero or billonarties you have to be so they accept that piece of crap ?

Your angry ignorance is amusing. Do you also consider echo or /dev/zero crap? Unix commands are simple by design, so that they can be combined easily. For instance, off the top of my head, here's a way to list the first 100 powers of two: (echo 1; yes 'p2*')|dc|head -n 100
Anonymous
Fri 6-Jul-2012 23:17
int main( in argc, char** arg)
{
if ( argc < 2 )
{
printf( "\nNo program, prints line to tty until killed !... please put now my name in Linux.\n" );
exit(1);
}
while(1)
{
printf( argv[1] );
};
}
Anonymous
Fri 13-Jul-2012 19:37
Thank you!!
Anonymous
Sat 14-Jul-2012 05:33
Shell is allowing me to type in only 256 characters. e.g.

bash-3.00$ cat
SunStudio12u1-SunOS-SPARC-

Minal Patil

Can Web Master please delete this post so that the page would not be made so wide in some browser to be unreadable?
Thanks.
lft
Linus Åkesson
Sat 14-Jul-2012 13:39
Can Web Master please delete this post so that the page would not be made so wide in some browser to be unreadable?
Thanks.

I've added some "max-width" attributes that should fix the problem. What browser do you use?
Anonymous
Sun 15-Jul-2012 00:18

lft wrote:

Can Web Master please delete this post

I've added some "max-width" attributes that should fix the problem. What browser do you use?

Thanks for being so responsive.

I did realize, later, that it was just elinks that laid out the page to be annoyingly wide.

$ elinks http://www.linusakesson.net/programming/tty/index.php

(Use "]" or mouse to click near the right border of the terminal to scroll right.)

I also found, later, that removing the posting containing a unusually long single-word helps, but a little scrolling left and right is still needed in elinks.

Since this is restricted to an uncommon browser, don't worry about it.

(I started using elinks on one computer due to the recent development on Linux desktop that resulted in poor support for older video cards where Firefox would freeze the entire desktop when rendering some common web content. elinks worked great for text content.)

By the way, thanks for the page. It was a great read.
lft
Linus Åkesson
Sun 15-Jul-2012 22:29

lft wrote:

Can Web Master please delete this post

I've added some "max-width" attributes that should fix the problem. What browser do you use?

Thanks for being so responsive.

I did realize, later, that it was just elinks that laid out the page to be annoyingly wide.

I use elinks quite a lot myself, actually, for a fast, nonsense-reduced browsing experience. The page is not wider than the screen on my setup (elinks version 0.12pre5 with CSS enabled).
Anonymous
Wed 19-Sep-2012 23:01
It's not to keep application simple that the line discipline is in the kernel. The unix philosophy is quite the contrary, to keep the KERNEL simple, and deport to user space the handing of complexities that they don't want inside the kernel. (See for example interrupted system calls and signal handling, but that's another topic).

The reason why the line discipline is inside the kernel, is to avoid context switches at the reception of each character (which in the early times of small core memories, would imply swap-outs and swap-ins!). So the line discipline keeps in a kernel buffer a line of input, and since it's simple enough to test for a specific byte and decrement a counter to implement the backspace "editing" (and a few other simple editing functions), it's done there.

The alternative, is to use the raw mode, where the characters are forwarded to the application as soon as they're received, which is needed for more sophisticated editors, like the (at the time) famously known Eight Megabytes And Constantly Swapping editor (emacs). And indeed, since emacs had to use this raw mode, which implies a context switch at the reception of each character typed, it was constantly swapping when the computers hadn't enough memory to keep emacs and all the other programs in core.
Anonymous
Thu 20-Sep-2012 04:54
Thanks a lot! Was looking for this kind of information all over the place, never found anything good. If you got more please keep it coming.
Anonymous
Fri 28-Sep-2012 00:03
The most concise/intuitive article I've read so far on the subject. Diagrams were especially helpful. If you manage to write a book on Linux is such manner, no doubt it's going to be a bestseller...

Thanks,
Val.
Anonymous
Fri 5-Oct-2012 19:38
ZZZZZZZZZZZZZZZZZZZZZZZZZZ to long, jesus christ write up a summery for us lazy people

too many Zs. write a shorter insult with proper spelling for us non-moron people
Anonymous
Sun 11-Nov-2012 21:09
Thanks for the page. You help me a lot.
Anonymous
Thu 29-Nov-2012 11:27
Great job!

I have just a suggestion.
This article can be used as a first step by people (like me...) who don't have a deep knowledge on the topic,
so why not to add a "References" section to help going into more depth?

Cheers!
Anonymous
Sun 30-Dec-2012 10:26
Thank you very much for this great article!
Anonymous
Fri 4-Jan-2013 16:38
Excellent!! Thank you very much for this article. Watching and trying to understand technological issues via their historical context and being showed how they evolved make it really a pleasure to follow your explanations and very easy to get them.
Anonymous
Wed 6-Feb-2013 02:27
Very useful article, thanks.

A couple of clarifications that would be great:

1. It seems that there is a 1:1 correspondence between a session and the associated tty (bearing in mind that the associated tty may be "none") - is that true? In other words, can one session include processes with different ttys, and can processes in two different sessions be associated with the same tty? (and if so... what does that mean?!)

2. What are the exact rules for automatic raising of SIGHUP? It seems that this is raised by the TTY driver, right? Does the POSIX specification specify when this should happen and who should receive it, and does Linux follow that? I have a confusing situation involving ssh -t raising SIGHUP on exit, while logging out of an interactive ssh login apparently does not.

Thanks
D
Anonymous
Mon 11-Mar-2013 18:54
Wow! Thanks Linus for the article.
This is how I learned about things in the past, from people who knew their craft well. Compare this with how we do it today - don't think, google first, rummage through incoherent posts, forums, mailing lists and if you are patient enough might be able to put pieces together to get just the clues/pointers to the information you want! So much for the 'age of information'.
Keep up the good work!
Thanks.

- VJ
Anonymous
Tue 2-Apr-2013 13:01
Real very informative! Kudos to your effort and thanks for hard work!
ulzha
ulzha
Sun 7-Apr-2013 16:59
Fantastic, tack så mycket!

I'm not writing an OS, but writing an improved screen or GUI terminal I do consider.

In particular I was interested in whether there was an API to tell foreground job's output and background job's output apart (e.g. associating a PID with each chunk output) to highlight them understandably or something. Now I figure that I just might be able to implement that by wisely trapping SIGTTOUs perhaps...
Anonymous
Sat 18-May-2013 13:57
So much for the 'age of information'.

Well, it is the age of information. Noone said it was the age of wisdom.
Anonymous
Sat 18-May-2013 14:08
"yes" program , produced in 2009 !

Thank you David McKenzie for your contribution to open source community !

I am just wondering what in your background that enabled the FSF to accept such worthless contribution ?

Member of what masonic lodge or what church or son of a war hero or billonarties you have to be so they accept that piece of crap ?

For reference:

yes command - otputs a line on tty until killed !

coded and added to Linux in 2009.

Actually, "yes", which is part of GNU coreutils, has been around since, like forever, being an implementation of the same-named Unix command.

David MacKenzie is the author of many of coreutils' commands, including chgrp, chmod, chown, date, dirname, expand, fold, ginstall, groups, head, mkdir, mkfifo, mknod, nice, printenv, printf, rmdir, stty, su, tty, uname, unexpand, and obviously yes; and is co-author of many others. But he is probably best known for autotools, which is one of the most central pieces of free software, as any distribution maintainer could tell you.

Now, what have YOU done for GNU or Linux or free software?
Anonymous
Sat 24-Aug-2013 23:48
So I was searching for information about the TTY system, and where did I, by chance, end up? Thanks for the great article. :-)

/radiantx
Anonymous
Fri 6-Sep-2013 14:10
really great post ! Thanks a lot for the general understanding of the how, instead of the usual "to do this, do that" ! And I'll stop here because having three exclamation mark in one sentence is a clear sign of mental disorder, but thanks again.
Anonymous
Sun 15-Sep-2013 22:58
Excellent post on TTY!! Thank you -- Aananth C N
Anonymous
Thu 10-Oct-2013 07:42
thank you. very interesting.
Anonymous
Wed 11-Dec-2013 15:19
I'll join the crowd here to say thank you for the time you spent on this article, I've appreciated it a lot.
Anonymous
Fri 13-Dec-2013 16:13
Thank you. I don't think this article will EVER be outdated :P
Anonymous
Fri 27-Dec-2013 16:55
Great article! I'm currently running a real Teletype ASR-33 on a serial port with Ubuntu. I have to use stty to set the proper parameters for an uppercase-only terminal, change tabs to spaces, etc. Is there a way to determine what port I'm logging into (ttyS0? S1? S2, S3 or S4?) Right now, I have to run stty on all ports and get an access denied error for any port I'm not connected to. I'd like to find out what port I'm on and only run the stty on that port. Thanks for your time!

Have you tried ls /dev/tty.* to get a listing of connected devices? On my Mac OS X this cues me into which tty device to use. I'm running a DEC Writer III. :)
Anonymous
Tue 11-Feb-2014 20:38
Thanks for this enlightening article! It is a bit wordy, but I completely expected that.

I am currently working on writing a toy OS

You work for Microsoft?
Anonymous
Wed 12-Feb-2014 03:14
First teletype was made in Germany around the 30s or 40s. One of the many types of teletypes was the Hellschreiber. There were also other types but all worked the same: you punch letters and they are remote printed to one or more receiving stations. After the war in the operations of confiscating the german patents, the US developed the teletype further. That's missing from your article.
Anonymous
Fri 14-Feb-2014 09:32
First teletype was made in Germany around the 30s or 40s. One of the many types of teletypes was the Hellschreiber. There were also other types but all worked the same: you punch letters and they are remote printed to one or more receiving stations. After the war in the operations of confiscating the german patents, the US developed the teletype further. That's missing from your article.

The Hellschreiber device is not a teletype, it's more of a facsimile machine. You pressed a letter, and a _bitmap_ of the letter was transmitted (twice to account for mechanical asynchrony). On the receiving side, marks and spaces were literally penned onto the paper, creating two copies of the text, one atop the other. Due to said asynchrony, it was often skewed, but because two copies were printed, nonetheless legible.

This is a very, very different mode of operation from the teletypes described above.
Anonymous
Fri 14-Feb-2014 09:45
First teletype was made in Germany around the 30s or 40s. One of the many types of teletypes was the Hellschreiber. There were also other types but all worked the same: you punch letters and they are remote printed to one or more receiving stations. After the war in the operations of confiscating the german patents, the US developed the teletype further. That's missing from your article.

I don't know of the history of teletypes in Germany, but the Teletype Model 15 was first produced in 1930 and was in wide use before and after World War II. Both in military and civilian contexts.

The Model 15 wasn't the first model produced by the Teletype corporation, but it was probably the most widely used pre-war model. It might have been the most widely used model, period.

Like I said, I don't really know anything about German teletypes, but looking at the Wikipedia page for Hellschreiber, that device is quite different than the Teletype corporation's devices. It looks like the Hellschrieber sends pixels and might actually be more similar to FAX machines than Teletype devices.
Anonymous
Mon 17-Feb-2014 08:42
man! you made me see the light!
Anonymous
Thu 27-Feb-2014 08:55
Hi--how are things in Sweden?

Every once in a while I get up the ambition to complain about the width of text on a web page, and you're the lucky winner today--sorry ;-)

This could be a good article--from the looks of it, it probably is--but why is it (and so many other web pages today) so wide?

Checking one line at random, it is 130 characters wide:

echo "Meanwhile, however, the computers — still quite large and primitive, but able to multitask — were becoming powerful enough to" | wc
1 20 130

Oh, and I'm ignoring the stuff in the left hand panel / column--I simply horizontally scroll so that panel is not visible.

.....

You mean you aren't browsing this page with a teletype?
Anonymous
Thu 10-Apr-2014 14:05
> Writing to a TTY which is stopped due to flow control, ...... will block your process

I can't understand? The process will not blocked when TTY is stopped by flow control(ctrl+S), the foreground process will continue running. The only difference is I can't see the display until I type ctrl+Q again.

Thanks for you article
nyu
Anonymous
Thu 10-Jul-2014 13:03
> Writing to a TTY which is stopped due to flow control, ...... will block your process
I can't understand? The process will not blocked when TTY is stopped by flow control(ctrl+S), the foreground process will continue running. The only difference is I can't see the display until I type ctrl+Q again.

You're right Nyu... and this running process may be blocked when the TTY kernel buffer is full of non-displayed characters, if it outputs too much on stdout/stderr.
I guess the author has taken a shortcut when writing this, as the flow control stop is often used to block a too verbose process and to be able to read few lines before let it go on again.

Yves
Anonymous
Mon 14-Jul-2014 13:16
great post!!
Anonymous
Thu 24-Jul-2014 11:39
Hi!
I'm struggling with RS485 communication:
Is it possible to configure a tty to automatically raise the RTS line before sending and lower the RTS line after sending?
Thank you for your input,
Helmut
Anonymous
Mon 28-Jul-2014 13:13
the VT100 was not a colour terminal as the text suggests, though.
Anonymous
Tue 29-Jul-2014 00:07
Unix purist <- I WAS HERE +1 great article
Anonymous
Tue 5-Aug-2014 11:29
Great article - I will recommend it to my network.

Kind regards,
Christian from Germany
Anonymous
Tue 2-Sep-2014 18:01
the VT100 was not a colour terminal as the text suggests, though.

seems so, just checked the manuals online. http://www.vt100.net/docs/

do you perhaps know which terminal introduced color attributes for escape sequences?
Anonymous
Fri 5-Sep-2014 01:13
The Plan 9 operating system completely does away with TTY, signals, and ioctl. Everything in Plan 9 is either a file or represented as a file.
Anonymous
Sat 13-Sep-2014 14:33
The basic idea is that every pipeline is a job, because every process in a pipeline should be manipulated (stopped, resumed, killed) simultaneously.

Not just pipelines, every command is a job in shell's parlance. Job is a userspace thing, only maintained by shell, not kernel.
http://www.gnu.org/software/bash/manual/bashref.html#Shell-Commands

Niz
Anonymous
Wed 24-Sep-2014 14:34
By far the most amazing article about TTY I have ever read, thanks!
Anonymous
Sun 12-Oct-2014 19:04
Great article. Thank you! After 15 years of using unix and linux i finally got to the description of ttys :)
Anonymous
Fri 14-Nov-2014 02:31
Fantastic article. Thanks for doing all of the legwork. :)

-pf
Anonymous
Sun 16-Nov-2014 03:37
Thank you so for the article!
And now, I know the meaning of "tty" and "pty"!

Maya2003
Anonymous
Wed 3-Dec-2014 16:42
Great article, thanks a lot!
Anonymous
Wed 14-Jan-2015 21:09
This is the best article I have ever read about tty.

Thanks a lot!
Anonymous
Mon 9-Feb-2015 03:38
Thank you !!! :)
Anonymous
Sun 15-Feb-2015 23:47
Yup, interesting intro to the TTY area, I think, and fun trying out the terminal tweaks..but any ideas of where I can find a good intro to using TTY in Linux, without the writer assuming I know all about networking/the Linux kernel in advance??? Please and thank-you.
Anonymous
Sun 15-Feb-2015 23:47
Yup, interesting intro to the TTY area, I think, and fun trying out the terminal tweaks..but any ideas of where I can find a good intro to using TTY in Linux, without the writer assuming I know all about networking/the Linux kernel in advance??? Please and thank-you.
zdennis
Zach Dennis
Mon 9-Mar-2015 00:07
Yup, interesting intro to the TTY area, I think, and fun trying out the terminal tweaks..but any ideas of where I can find a good intro to using TTY in Linux, without the writer assuming I know all about networking/the Linux kernel in advance??? Please and thank-you.

The O'Reilly book titled "Termcap & TermInfo" should get you started: http://www.amazon.com/termcap-terminfo-OReilly-Nutshell-Linda/dp/0937175226
Anonymous
Sun 12-Apr-2015 00:14
best tty explanation ever written, hands down.

thank you so very much.
Anonymous
Wed 15-Apr-2015 01:21
I'm not especially new to bash programming or serial ports in general, but have never delved into stty and echo.

So I have a simple script (and I'm not sure I have the stty settings right), that should send "tx c" to the serial port. However, the best I can tell using minicom, if I perform the following from the command line:

echo tx c > /dev/ttyO1

the serial device receives:

tx.{

echo aaa > /dev/ttyO1 echos aay
echo bbbbb > /dev/ttyO1 echos bbbbz
echo abcdefghijklmnop > /dev/ttyO1 echos abeefgkijkmmnoz

WHAT am I missing?
Anonymous
Tue 21-Apr-2015 18:45
This is best ever article on TTY. What an awesome job. Please keep writing such articles.
Chakri
Anonymous
Fri 8-May-2015 14:00
The best article about tty ever! no kidding.
Anonymous
Sat 25-Jul-2015 20:08
very useful! thanks~
Anonymous
Thu 30-Jul-2015 06:41
I am new to Linux but this article is so clear even i can taste 60% of them. I will read it again a couple days later because this is the best article i ever read about linux.
Anonymous
Sat 15-Aug-2015 21:45
amazingly clear on a very fundamental piece of technology that I never really knew about. Just the build up of the subject is a great way to approach any kind of technical writing. I'll definitely keep this post in mind when I try to tackle writing about a complicated technical subject.
Anonymous
Sun 16-Aug-2015 05:57
"yes" program , produced in 2009 !

Thank you David McKenzie for your contribution to open source community !

I am just wondering what in your background that enabled the FSF to accept such worthless contribution ?

Member of what masonic lodge or what church or son of a war hero or billonarties you have to be so they accept that piece of crap ?

For reference:

yes command - otputs a line on tty until killed !

coded and added to Linux in 2009.

HISTORY
The yes command appeared in Version 7 AT&T UNIX.
The code for this in GNU is ridiculously long, but in other systems, the code is shorter than the license or the above comment
#include <stdio.h>

int
main(int argc, char *argv[])
{
if (argc > 1)
for (;;)
puts(argv[1]);
else
for (;;)
puts("y");
}
i336_
David Lindsay
Tue 18-Aug-2015 18:55
This is really cool. I've just discovered this; I might have more to add after I've properly read through it. While I remember, though, I want to mention one of the better resources I've found for terminal escape sequences: an old online manpage for dtterm(5), at

http://www2.phys.canterbury.ac.nz/dept/docs/manuals/unix/DEC_4.0e_Docs/HTML/MAN/MAN5/0036____.HTM

Also, the following Github Gist is updated extremely frequently (6 days ago, as I type this); it tracks terminal support for 24-bit color, a fairly new capability that's slowly gaining traction. You should be able to just expect it to be available 3-5 years from now; terminals in hyper-current distros like Arch likely have the support already.

https://gist.github.com/XVilka/8346728
Anonymous
Tue 22-Sep-2015 06:41
Thanks for a great explanation of infrequently discussed, arcane, but essential part of the Unix/Linux "reptile brain". Required knowledge for anyone who hopes to use Unix/Linux beyond a novice level.
Anonymous
Mon 30-Nov-2015 10:35
Amazing. Very very needed. Thank you very much.
Anonymous
Fri 4-Dec-2015 09:14
Very nice summary for those born too late :-)

It might be worth mentioning that UARTs/serial ports have control lines to block transmission when the received data can't be processed as fast as it's received.
This way the UART itself would stop transmitting and the whole process was handled in hardware.
The sending process would have to wait (be blocked) for the UART to transmit it's data anyway, as the hardware would mostly be slower than the software anyway.

However when modems came in to play, the terminals UART and the hosts UART were no longer directly wired to each other and modems would only transmit data and no control lines between them. This situation required another method of flow control that would have to be transmitted in-band with the data. So software flow control via device control characters was invented.
Anonymous
Thu 7-Jan-2016 13:45
5
Anonymous
Tue 12-Jan-2016 17:07
Really good document,thanks for your great efforts
Anonymous
Fri 5-Feb-2016 15:34
I am learning linux... and it is best article to understand what is going under the hood:) thanks for your time
Anonymous
Sat 20-Feb-2016 02:55
Good job demystifying TTY for me. Keep it up.

Cheers
Anonymous
Thu 10-Mar-2016 02:06
This and all OK.. But.. I have one doubt for long time... In case of terminal emulator we can navigate/scroll using mouse.. How do we scroll/navigate inside TTY (I am a Linux beginner.. So sorry if it's a stupid question)
lft
Linus Åkesson
Wed 30-Mar-2016 00:35
This and all OK.. But.. I have one doubt for long time... In case of terminal emulator we can navigate/scroll using mouse.. How do we scroll/navigate inside TTY (I am a Linux beginner.. So sorry if it's a stupid question)

Hi! That is not handled by the TTY layer, it is done by the terminal emulator itself. Whenever something scrolls off-screen, it is appended to a scrollback buffer, which you can then view using GUI controls or keyboard shortcuts.
Anonymous
Thu 31-Mar-2016 00:02
Yes! More than I was looking for, but I'm not complaining. Brilliant article, and now I know enough to stay the fuck away until I must.
Anonymous
Sat 9-Apr-2016 03:04
really cool thanx to share your knowledge
Anonymous
Sun 10-Apr-2016 05:34
Hello Linus! I have a question about what happens when you kill a process in raw mode. If you launch, say, vim in a terminal and then run `killall -9 vim` from a separate terminal, you would expect the original terminal to be left in raw mode after vim exits (as if you had run `stty raw` or something). However, surprisingly, it is left in canonical mode. I have tried this with multiple shells, terminal emulators, and OS's, and only urxvt behaves differently from this. My question is, what mechanism resets the terminal in this case?

-- kc
lcu
Luis Colorado
Thu 14-Apr-2016 16:19
Hello Linus! I have a question about what happens when you kill a process in raw mode. If you launch, say, vim in a terminal and then run `killall -9 vim` from a separate terminal, you would expect the original terminal to be left in raw mode after vim exits (as if you had run `stty raw` or something). However, surprisingly, it is left in canonical mode. I have tried this with multiple shells, terminal emulators, and OS's, and only urxvt behaves differently from this. My question is, what mechanism resets the terminal in this case?

-- kc
Historically, the UNIX tty driver used to reset terminal settings to known values (300bps, some parity, cooked mode, etc.) in close() callback (which was called by the kernel only on last close to the device only) but this was found nonsense, because normal initial session programs like old /etc/getty (yes, it was stored there) were to initialize it fully. This had some drawbacks, because this last close could happen when not needed and the reasons to conserve settings between close where significant.

This requirement has been dropped from linux kernel and now linux allows you to fix terminal settings from a previous shell command without losing those settings because of a last close issue.
Anonymous
Fri 22-Apr-2016 06:21
Thank you sir. You have been helpful.
Anonymous
Mon 16-May-2016 23:57
Thank you. I don't think this article will EVER be outdated :P
The images of it already are on high resolution monitors.
Anonymous
Thu 23-Jun-2016 22:00
Best article regarding this mess!
Anonymous
Fri 15-Jul-2016 23:14
This is just awesome! I have old phone co UNIX books that don't go into this depth! Thx for this info
Anonymous
Sat 16-Jul-2016 21:19
Nice one. Thanks.
Anonymous
Sat 23-Jul-2016 16:39
I'm using python on Debian os.
I've interfaced bluetooth to uart.
The data coming from bluetooth is received and can be monitored on ttyS0.
Can someone tell me how to copy that data.
I want to paste it in a text file
Anonymous
Tue 30-Aug-2016 00:34
"yes" program , produced in 2009 !

.... cut ....

For reference:

yes command - otputs a line on tty until killed !

coded and added to Linux in 2009.

Interesting! I remember I made a joke with yes program to my friend in 1996 by using Slackware Linux I.e. Linux kernel so new.
Anonymous
Tue 30-Aug-2016 01:03
I'm not especially new to bash programming or serial ports in general, but have never delved into stty and echo.

So I have a simple script (and I'm not sure I have the stty settings right), that should send "tx c" to the serial port. However, the best I can tell using minicom, if I perform the following from the command line:

echo tx c > /dev/ttyO1

the serial device receives:

tx.{

echo aaa > /dev/ttyO1 echos aay
echo bbbbb > /dev/ttyO1 echos bbbbz
echo abcdefghijklmnop > /dev/ttyO1 echos abeefgkijkmmnoz

WHAT am I missing?

Not sure about your system but in my old linux systems with serial port, device was ttyS01 but not tty01.
Anonymous
Tue 30-Aug-2016 01:11
This and all OK.. But.. I have one doubt for long time... In case of terminal emulator we can navigate/scroll using mouse.. How do we scroll/navigate inside TTY (I am a Linux beginner.. So sorry if it's a stupid question)

Shift+page up/down

This will do the job both in fyi terminal emulators and the ones on TTYs (ctrl+alt+F1, etc.).
Anonymous
Tue 30-Aug-2016 01:23
I'm not especially new to bash programming or serial ports in general, but have never delved into stty and echo.

So I have a simple script (and I'm not sure I have the stty settings right), that should send "tx c" to the serial port. However, the best I can tell using minicom, if I perform the following from the command line:

echo tx c > /dev/ttyO1

the serial device receives:

tx.{

echo aaa > /dev/ttyO1 echos aay
echo bbbbb > /dev/ttyO1 echos bbbbz
echo abcdefghijklmnop > /dev/ttyO1 echos abeefgkijkmmnoz

WHAT am I missing?

Not sure about your system but in my old linux systems with serial port, device was ttyS01 but not tty01.

It seems ttyO1 works for you.
Maybe outrun a CR and LF at the end of the echoed words will help.
Indeed CR may not be needed.

Try this:
echo bbbbb\n\r > /dev/ttyO1

Hope it works *fingers crossed* :)
Anonymous
Tue 30-Aug-2016 01:29
I have been with Linux and UNIX systems since 1994. They were good old days and I am learning these amazing knowledge just now from this article.
Fantastic job Linus. Thank you so much!

Bedri Özgür Güler
Anonymous
Thu 6-Oct-2016 21:39
* the text should be arranged to wrap to the width of the (reader's) window

Whilst I agree with you entirely, this article wraps just as you request.

Of course, it may have been altered in the many years since you wrote your comment :-) but credit where credit is due!
Anonymous
Sun 20-Nov-2016 22:14
Wow, very impressive style of writing, thank you very much.
Great article.
Would be awesome something like that about handling keycodes(infocmp & friends).
Anonymous
Mon 12-Dec-2016 00:47
I have a small question.

Can you tell me when the /dev/ttyXXX or /dev/pts/X are added in the /dev directory ?

Initially, I thought that each program had its own /dev/ttyXXX device. When is the kernel adding a new /dev/ttyXXX device ?

Let say that I add the following lines in /etc/rc.local
# Start these program during the init.
/home/user/prog1 &
/home/user/prog2 &
/home/user/prog3 &

Will it create one or three new /dev/ttyXXX device ?
(or will it reuse another one that is already present?)

Thanks for this article.