Getting the process ID of the terminal in Unix/Linux

Hi,

How can we get the process id of the terminal we are using?

When we logged in to unix, we have an associated terminal. we can use "tty" command to get the terminal we are using like:
/dev/pts/0

I want to know the process id of this terminal. Please reply as I searched a lot but I didn't find any answer.
Thanks in advance!

Regards,

echo $$
[~]# ps
  PID TTY          TIME CMD
 7075 pts/1    00:00:00 bash
1 Like

$$ stand for current shells process id, but I want to know the process id of the terminal we are currently connected to.

Do you mean $PPID ?

A terminal is a file. Like /dev/tty. Files do not have a process id. Rather than having us continue to guess what is going for you, please tell us what you are trying to do. Not what you think you should do.

The process that "owns" the terminal is usually called the controlling process, or more correctly, the process group leader. You already were given a way to do that - to show the pid of the process group leader

1 Like

Just faced an question at an interview like:
How to kill the terminal you connected to?
I thought it was about killing your own session like logged in shell but the the interviewer asked me again and again the same question.
So I got confused and I searched a lot to know about it but got more confused and put the same question here to get some idea about the question. Thanks!

You close /dev/tty which has 3 attached file descriptors 0 -stdin, -stdout, 2 -stderr.
In C

 close(0); close (1); close(2);

bash:

0<&-
1<&-
2<&-

Of course after you do this you cannot control the process except by signal - kill [process pid]

Assuming the question is actually "How to kill the terminal emulator you are connected to ?" Control Shift Q would be my answer.