aaptel aaptel -5 years ago 165
Linux Question

How do *nix pseudo-terminals work ? What's the master/slave channel?

I want to write a simple, dumb, X terminal emulator in C on a Linux system.

At first, I just thought I would have to popen a shell and display its output.
I checked xterm and rxvt code, and it looks a bit more complicated.

First, I have to open a pseudo-terminal with openpty. So I look at the man page and see that openpty fills 2 file descriptors, the master and the slave.
Both xterm and rxvt code are messy because of the system-dependent-ness of those specials files.

I understand the termios stuff : it's just a bunch of information about the escape code of the terminal.
What I really don't get is : what am I suppose to do with the master/slave file descriptor ?

An example program which open a terminal, logs in, executes a "ls" on the shell would be awesome.

(English is not my native language, excuse my eventual mistake)

Here's the sample code I came up with :

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <pty.h>
#include <utmp.h>
#include <ctype.h>

safe_print (char* s)
while(*s) {
if(*s == '\n')
else if(iscntrl(*s))
printf("\\e(%d)", *s);

main (int argc, char** argv)
char buf[BUFSIZ] = {0};
int master;
int ret = forkpty(&master, NULL, NULL, NULL);

if(ret == -1)
puts("no fork"), exit(0);

if(!ret) {
execl("/bin/sh", "sh", NULL);

sleep(1); /* let the shell run */

if(argc >= 2) {
write(master, argv[1], strlen(argv[1]));
write(master, "\n", 1);
} else {
write(master, "date\n", sizeof "date\n");

while(1) {
switch(ret = read(master, buf, BUFSIZ)) {
case -1:
case 0:
puts("nothing.."), sleep(1);
buf[ret] = '\0';



return 0;

Answer Source

With respect to the master/slave part of your question, from the pty(4) man page (which is referenced from the openpty(3) man page on my system):

A pseudo terminal is a pair of character devices, a master device and a slave device. The slave device provides to a process an interface identical to that described in tty(4). However, whereas all other devices which provide the interface described in tty(4) have a hardware device of some sort behind them, the slave device has, instead, another process manipulating it through the master half of the pseudo terminal. That is, anything written on the master device is given to the slave device as input and anything written on the slave device is presented as input on the master device.

Man pages are your friends.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download