Adventures with linux (virtual) terminals

April 23, 2011 at 12:05 am Leave a comment

I didn’t know how linux terminals worked. Now I know slightly more.

The linux kernel supports a preset number of terminal type things (called, variously, terminals, consoles or virtual terminals – but not pseudo-terminals). At the time of writing, 63 terminals were supported.

From the users perspective, it is as though they terminals always exist and can always be written to (in /dev/tty*), but
in practice, I imagine that linux allocates these terminals as you go.

A processes view of a terminal

You can write and read from a terminal just like you would a normal file (they are character streams so you can’t seek).

This works fine from the command line:

echo "HELLO TERMINAL" > /dev/tty16
cat < /dev/tty16 

The physical interface to a terminal

A terminal is more than a file – it is something that you physically interact with. You can see things that get written to a terminal and you can type into it.

This is all magically handled by the kernel: when the kernel thinks that you are looking at a particular terminal it reads any key presses, converts them to characters, then writes them to relevant file. (Interestingly, keypresses only do something if there is something on the other end reading from the terminal)

Similarly, characters written are collected into a buffer displayed on the screen.

There are also magic commands which switch terminals. Strace informs me that all this magic works via the ioctl function call. You can cause these cause the appropriate calls by either using the “chvt” command, or there are well-known keybindings for the task.

strace 2>&1 chvt 16 

ioctl(3, VT_ACTIVATE, 0x10) # HURRAH

You can see the actual keybindings by looking at the loadkeys keymap file. (in /etc/console-setup on Ubuntu 10.04.2). [Crazy fact – there are keybindings for switching to terminals 1-36 in my configuration file!).


Entry filed under: Uncategorized.

Gstreamer cheat sheet A rough outline of how X works

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed

April 2011
« Mar   May »