BSD Device Files – Purpose of BSD pty/tty Device Files in /dev

macosterminal

I am on macOS Catalina.

As far as I can tell (reading man pty, man tty, parts of this wikipedia article, and this somewhat dated reference on ttys), these device files are no longer used.

Instead, when one opens up Terminal.app, /dev/ptmx (pseudo terminal master multiplexer) dynamically creates a /dev/ttys### slave for the user to type command inputs. This is what one sees when calling who.

However, when I ls /dev, I see many pairs of these BSD style pseudoterminal files, that is, /dev/ttyXY and /dev/ptyXY where XY are a pair of characters. I believe there are 255 total pairs of these files.

  • Is there any specific reason besides backwards compatibility that these files are on new versions of macOS? Any modern use cases?
  • If possible, how could I open one of these BSD PTY files, instead of /dev/ptmx, for general terminal use or with screen? When I tried to screen \dev\ttyw0, nothing happened, and xterm created another \dev\ttys###.
  • Why was the new ptmx system adopted? Just so I can create more than 255 terminal sessions simultaneously?

Edit: related but different post.

Best Answer

Backwards compatibility is the reason that these files are on the newest version of macOS. It is there to ensure that programs that expect this way of handling pseudo terminals still work.

You can open the /dev/ptyX# files the same way you would open /dev/ttys### files. You seem to have reversed the slashes in your command - please ensure that you are using forward slashes like this:

screen /dev/ttyw0

Note that this in itself doesn't cause anything to happen. You have only connected to one end of the terminal pair. You would need to connect to other end as well to transfer some data.

As an example you could open one Terminal window and run the command above, and then open another Terminal window and run the command below:

screen /dev/ptyw0

Now if you type something in one window, it appears in the other (and vice versa).

The main factor in adopting the ptmx system was that the old system is very complex for programmers to use correct. It looks deceptively simple, but when you get into it, you'll find that it is actually not.

One of the main problems is in determining which of the /dev/ttyXX files to actually use. I.e. you want to use one that is not in use by others. If you're not very careful, two programs that want to open a new pseudo terminal at the same time could both mistakenly think that the same terminal is not in use by others, and then both start using it - causing all sorts of problems. The ptmx system solves this problem.