From: Jonathan de Boyne Pollard on
>
>
> When a parent process spawns a child process, how does the child know
> the ptty device of its parent and how does it know that it must write
> to it?
>
> For example, this the excerpted output `ps axf`:
>
> 1687 ? Ss 1:53 tmux
> 1688 pts/0 Ss+ 0:00 \_ -bash
> 2063 pts/4 Ss+ 0:00 \_ -bash
> 3552 pts/5 Ss+ 0:00 \_ -bash
> 31306 pts/11 Ss 0:00 \_ -bash
> 31835 pts/11 S+ 0:00 \_ ./myprogram
>
>
> Here as you can see, "myprogram" knows that its pts is pts/11 .
>
It's called a "controlling terminal". And the simple answer is that the
process structures inside the kernel have fields that note what each
process' controlling terminal is, and controlling terminals are copied
from parent to child by fork(). This isn't even a Linux-specific
thing. Get out your copy of the Single Unix Specification and read the
chapter on the General Terminal Interface.