Thread: How To Check if process is associated with invoking terminal?

  1. #1
    Registered User
    Join Date
    Oct 2010

    Question How To Check if process is associated with invoking terminal?

    ok so i have been writing my own ps program for an assignment and it needs to have the option -A which is fine, apart from choosing what to display when -A is not selected.

    It is meant to display as ps does and only show the processes associated with the same terminal as the invoker. What I am tearing my hair out with is that i dont know how, in c, to find out what the invoking terminal is!!

    So if anyone can lend me a hand, much appreciated

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    I'm pretty sure the invoking terminal is the process' parent, in which case you can just use: getppid(2): process identification - Linux man page.

    You'll want to recursively check the parent's pid just in case it's a subprocess:
    113 (terminal process)
    -- 175 proc1
        -- 176 subproc1
    If you understand what you're doing, you're not learning anything.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    "Controlling terminal" refers to a device, and a device is not a process (it's a kernel interface) and therefore does not have a process id. You can find the device name of the controlling terminal with tty:

    Quote Originally Posted by man page for tty
    tty - print the file name of the terminal connected to standard input
    These correspond to the "TTY" field in ps output.

    Getppid() will usually return the pid of the executing bash shell (presuming the system uses bash). For a process started from the "real" non-GUI console (a /dev/tty), the parent id of the shell will then be the login process which was first spawned by the getty (different distros use different forms of getty) process which opened the tty interface. Normally, the parent process of both the login and the getty is 1 (init). Ie, the getty is not in the process's parent chain.

    For gui consoles (/dev/pts), the parent process of the bash will be the terminal emulator application (eg, "gnome-terminal").

    "ps" sans -A includes the terminal shell and all its attached descendents. So, you could either trace back to a terminal emulator process or a login process (and not include them, since ps does not), or (probably a better idea...) you could just get the pid of the shell and include it in the list.

    You could also have a look at the real ps source code, of course.
    Last edited by MK27; 11-09-2011 at 09:13 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My own terminal > Running a background process
    By tiestodj in forum C Programming
    Replies: 3
    Last Post: 03-06-2010, 03:30 AM
  2. Replies: 6
    Last Post: 01-12-2010, 10:21 PM
  3. Check Running Process Dev-C++
    By gamesplant in forum Windows Programming
    Replies: 23
    Last Post: 10-19-2009, 03:20 AM
  4. [c++] check if a process is already running
    By liquid_ice in forum Linux Programming
    Replies: 0
    Last Post: 05-12-2009, 03:30 AM
  5. how to check if a child process is done?
    By ichijoji in forum Linux Programming
    Replies: 1
    Last Post: 01-28-2005, 01:06 AM