Based on what I have read about pseudoterminals in Linux, there are two types of pseudoterminals: BSD-style pseudoterminals (which is deprecated) and UNIX 98 pseudoterminals.
I have created two images that shows my understanding of these two types of pseudoterminals.
The following image shows how the BSD-style pseudoterminals works (please correct me if the image is wrong):
This type of pseudoterminals is not hard to understand, each terminal is connected to a unique master driver.
But in the UNIX 98 pseudoterminals, things are a little more confusing. The following image shows how I think this type of pseudoterminals works:
So basically all terminals use the same master driver (/dev/ptmx
), but I am not sure how the master driver knows how to do the following:
If data is being sent from one of the terminal processes, how does the master driver knows to which TTY slave driver the data should be passed to?
If data is being sent from one of the TTY slave drivers, how does the master driver knows to which terminal process the data should be passed to?
Does the master driver knows how to do this in the way that I have shown in the image (i.e. the master driver have a mapping table that maps each terminal PID to its corresponding TTY slave driver)?
/dev/ptmx
is a driver in the image, I was just implying that the/dev/ptmx
device file points to the master driver. – user7681202 Nov 21 '17 at 11:30bash
) will have in its fd table the fd 0 (stdin
) and fd 1 (stdout
) and fd 2 (stderr
) all point to/dev/pts/0
, Now if you have anotherbash
process, it will have its fd 0 and fd 1 and fd 2 all point to/dev/pts/1
, do we agree so far? Now let's talk about the master side, say we have twognome-terminal
processes running, what will the fd 0 and fd 1 and fd 2 in the fd table for eachgnome-terminal
process point to, will they point to/dev/ptmx
? – user7681202 Nov 25 '17 at 11:21gnome-terminal
usually does not have the file descriptors 0, 1, and 2 open; and in the case it has them open, they may likely be on the slave slave side of the pseudoterminal from where it was launched. The two terminal emulators in which the twobashes
are running will each have a file descriptor representing the master side of the two pseudoterminals; yes, both file descriptors were obtained by opening/dev/ptmx
; nevertheless they are different pseudoterminals with no relationship between them. (continued) – AlexP Nov 25 '17 at 13:22/tmp/some-file.txt
, it does not mean that they have the same file open. It may be the case that the first process opened the file, then removed it without closing it; then the second process opened the same filename. They each have a different file opened, although they both opened the same name. Not to mention namespaces... Exactly because all master sides of all ptys are obtained by opening the same filename there is the functionptsname()
to get the name of the slave side. – AlexP Nov 25 '17 at 13:24ls -l /proc/<PID_goes_here>/fd
. Each process had an entry like:16 -> /dev/ptmx
. So you're telling me that these multiple processes do not all talk to/dev/ptmx
, but rather each process talk to a unique master object/driver, and the/dev/ptmx
name that they all have in their fd tables is just the name of the file that each process originally opened and which they no longer use? – user7681202 Nov 25 '17 at 17:26