[oclug] how to get a process's tty
Francis J. A. Pinteric
linuxdoctor at linuxdoctor.biz
Thu Apr 8 21:58:48 EDT 2004
On Thu, 8 Apr 2004 15:46:03 -0400
Roger Messier <en916 at ncf.ca> wrote:
> I've found a solution, but it doesn't feel very elegant to me. The
> /proc/<pid>/stat files give the tty number for each process (e.g.
> 34817). I then go through the /dev/pts directory and stat() each file
> there. I then compare one of the struct stat members (st_rdev)
> returned by stat() to the tty number found in /proc/<pid>/stat. If
> they match, then the file stated (e.g. /dev/pts/1) is the terminal
> that that process is attached to.
> I was wondering if there was a simpler way of doing what I've just
> described. I've been trying to understand the source code for top,
> since it gets the tty for each process, but haven't been able to wrap
> my head around it.
Look at the /proc/<pid>/fd directory. Using an `ls -l' command on that
directory will give you all the file descriptors used by that process.
I'll leave it as an exercise for the reader to extract the device name.
It's easy to write a shell script at this point.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 189 bytes
Desc: not available
Url : http://tux.oclug.on.ca/pipermail/oclug/attachments/20040408/ff586b9f/attachment.bin
More information about the OCLUG