The "controlling terminal" aka. ctty, is distincted from "the terminal a process is interacting with".
Standard way of getting the path of ctty is ctermid(3). Upon calling this,
In freebsd since release 10, an actual path is looked up[1],
while older freebsd and glibc implementations[2] unconditionally returns "/dev/tty"].
ps(1) from the linux procps 3.2.8 package, read the numerical entry in /proc/*/stat[3], and then deduct the pathname partially by guessing[4, 5] due to lack of system support[6].
However if we are not strictly interested in the ctty but any terminal associated with stdio, tty(1) prints the terminal path connected to stdin, which is identical to ttyname(fileno(stdin))
in c, and an alternative is readlink /proc/self/fd/0
.
Less important thought regarding the unconditional "/dev/tty" behavior: Specs merely say the string returned by ctermid "when used as a path name, refer to the current controlling terminal", instead of some straightforward "is the path name of the current controlling terminal". It might be interpreted as that "/dev/tty" is not the controlling terminal, but only refer to the controlling terminal if the same process open(3) it. Thus not violating the
"a terminal may be ctty for at most one session" rule[7].
Another consequence is that when I am without any controlling terminal, ctermid does not fail -- such failing is allowed by specs[8] --, so only can I become aware of my ctty'lessness until failing a subsequent open(3), which is okay since specs also say calling open(3) on it is not guarranteed to succeed.
Not much.
Mostly a process without a controlling terminal is not subject to receiving (mostly job control related) signals from terminal events.
These would include SIGINT
, interrupting the foreground process (group) if the interrupt character is received (usually ^C); SIGTTIN
/SIGTTOU
stopping the process if it tries I/O to the terminal from the background, and; SIGHUP
telling the controlling process (the shell) to terminate, in when the terminal sees a disconnection. (Originally a modem hangup.)
The standard has the details if you like.
Best Answer
There is a process group leader - sort of like the head process - that owns the terminal, /dev/tty. A process group can be one or many processes.
The stty command changes and displays terminal settings. If you are actually going to use UNIX seriously consider finding a copy of Stevens 'Advanced Programming in the UNIX Environment'. Terminals have a lot of heavy baggage from the 1970's. You will spot that right away. Most of those odd settings can be ignored except for special things like UNIX system consoles.