TTYs / PTYs
Posted: Mon Sep 16, 2019 1:39 pm
One area that I've kind been stalled on with my OS is how to properly implement TTYs. I think I get the gist of them, but not the details, and even my books on OS dev kinda just gloss over the topic without getting into code very much on the subject. I can imagine lots of ways that might work "good enough", but I'd like to actually feel like I "get it" before putting together some hacking thing.
Here's my understanding:
I have a keyboard driver, which for each keyboard even fills up a queue of events, reading from /dev/keyboard will get those events.
A TTY is in principle basically a Screen/Keyboard combination. Assuming it is available in the VFS as /dev/tty, the writing to it would put chars on the screen, reading from it would get characters from the keyboard, typically blocking if no keys have been typed. Simple enough.
First question:
How is /dev/tty's input typically connected to /dev/keyboard's? Is it a case of just having the TTY open /dev/keyboard when it is created and deferring the reads to it as needed? I feel that this is too simplistic and won't be robust when I want to implement pipes and redirection.
I have a similar question for PTYs and how their I/O is "wired" up to the underling TTY.
I am imagining a "tree" where each node is a process and each edge is a queue, but this seems like an awful lot of indirection between the user typing and the process actually seeing the event. Are there any straight forward resources I can look at the get a better understanding of this?
Second Question:
For implementing Job control (ex Ctrl+Z to background a job), who "sees" the Ctrl-Z? Assuming a process tree where "shell" is running a process "do_stuff", what is the chain of events?
Does "do_stuff"'s next read trigger, shell to do a read, who sees the Ctrl+Z and then handles it? Or is the Ctrl-Z "pushed" to the controlling process more directly so that this can happen even if it doesn't happen to be doing a read at the time?
I've read http://www.linusakesson.net/programming/tty/index.php, which is great for the high level concepts and history, but I feel my knowledge gap is somewhere int he details not mentioned there.
Any help and/or simple examples would be very appreciated.
Thanks!
Here's my understanding:
I have a keyboard driver, which for each keyboard even fills up a queue of events, reading from /dev/keyboard will get those events.
A TTY is in principle basically a Screen/Keyboard combination. Assuming it is available in the VFS as /dev/tty, the writing to it would put chars on the screen, reading from it would get characters from the keyboard, typically blocking if no keys have been typed. Simple enough.
First question:
How is /dev/tty's input typically connected to /dev/keyboard's? Is it a case of just having the TTY open /dev/keyboard when it is created and deferring the reads to it as needed? I feel that this is too simplistic and won't be robust when I want to implement pipes and redirection.
I have a similar question for PTYs and how their I/O is "wired" up to the underling TTY.
I am imagining a "tree" where each node is a process and each edge is a queue, but this seems like an awful lot of indirection between the user typing and the process actually seeing the event. Are there any straight forward resources I can look at the get a better understanding of this?
Second Question:
For implementing Job control (ex Ctrl+Z to background a job), who "sees" the Ctrl-Z? Assuming a process tree where "shell" is running a process "do_stuff", what is the chain of events?
Does "do_stuff"'s next read trigger, shell to do a read, who sees the Ctrl+Z and then handles it? Or is the Ctrl-Z "pushed" to the controlling process more directly so that this can happen even if it doesn't happen to be doing a read at the time?
I've read http://www.linusakesson.net/programming/tty/index.php, which is great for the high level concepts and history, but I feel my knowledge gap is somewhere int he details not mentioned there.
Any help and/or simple examples would be very appreciated.
Thanks!