Why do shells call fork()?
When you call an exec
family method, it doesn't create a new process. Instead, exec
replaces the current process memory and instruction set etc., with the process you want to run.
As an example: you want to run grep
using exec
bash
is a process (which has separate memory, address space). Now when you call exec(grep)
, exec
will replace the current process's memory, address space, instruction set etc. with grep
's data.
That means the bash
process will no longer exist.
As a result, you can't get back to terminal after completing the grep
command.
That's why exec
family methods never return. You can't execute any code after exec
; it is unreachable.
As per the pts
, check it yourself: in a shell, run
echo $$
to know your process-id (PID), I have for example
echo $$
29296
Then run for example sleep 60
and then, in another terminal
(0)samsung-romano:~% ps -edao pid,ppid,tty,command | grep 29296 | grep -v grep
29296 2343 pts/11 zsh
29499 29296 pts/11 sleep 60
So no, in general you have the same tty associated to the process. (Note that this is your sleep
because it has your shell as the parent).