How It Works
Programs like terminal emulators need a stable way to connect to programs or backends, this is provided by the kernel through a pipe-like abstraction known as pseudo-terminals.
These can provide a little more functionality than plain pipes but work similarly – data goes in one end and comes out the other.
The problem is how to allocate these resources – some systems do it statically and others dynamically. The SecureLang RD Kernel currently solves this problem by statically allocating pseudo-terminals and letting programs decide which one to use.
The Simple Approach
The system creates some devices pty0, pty1, pty2 … for one end of the communication and matching tty0, tty1, tty2 … devices for the other end.
A program such as a terminal emulator then needs to just attempt to open the pty0/tty0 pair and if this fails it tries pty1/tty1, pty2/tty2 and so on until one is available.
This system is a little simplified but is basically the same as the old BSD way (from what search results tell me of early versions).
Why Dynamic Allocation Is Probably Better
The simple way works, but may not be fully reliable when multiple programs are cycling through trying to open things at the same time. It also raises some issues like having to give some attention to file permissions to ensure the other end is accessible.
One solution used by most modern systems is to have the kernel dynamically allocate a new pty/tty pair when needed. On the one hand this solves the problem, on the other hand it isn’t really as simple or intuitive as having a few numbered devices that a program can just select from.
So this might be a longer term strategy for the SecureLang RD implementation but might not actually be necessary if these issues can be resolved easier another way.
This Stuff Is Abstracted By Libraries
Luckily nowadays programs like terminal emulators don’t really need to know those details, as they can just use library functions like posix_openpt and ptsname to select a pty/tty pair. However the implementation may still be important for system administrators and framework developers, and can be important for programmers if/when it breaks!
Subnote: Why Not Pipes/Sockets
Operating systems provide other features that can be used for channeling I/O streams between applications, but a dedicated pseudo-terminal abstraction is important because it can also be used to apply terminal-specific options to these channels (for example to help programs reliably detect terminal type).
Leave a Reply