Results 1 to 5 of 5

Thread: virtual terminals and displays

  1. #1

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    virtual terminals and displays

    I've been looking all day for functions that will give me the
    1. vt that the current process is running on
    2. and the display it's running on.

    I found a slow way of getting the active terminal with fgconsole.

    Question 3:
    I want to avoid the use of fgconsole because I'm calling another program from my program and then parsing the output.

    That's 3 different things I'm looking for.
    I need the vt and display of the process
    and
    fgconsole equivalent to get the number of the active vt.

    I know this is a tough question because I've been looking so long.

  2. #2
    Hyperactive Member CyberSurfer's Avatar
    Join Date
    Aug 2000
    Location
    Old London Town
    Posts
    425

    Re: virtual terminals and displays

    Well, I'm not sure how easy it is to get the number of the active display but the active terminal I can definately help with. You can use ps to see what terminal a process is running under, like so (grepping for X as the example process we want to look up:

    # ps -ef | grep X
    UID PID PPID C STIME TTY TIME CMD
    root 7400 3182 0 2006 tty7 00:00:02 /usr/X11R6/bin/X {truncated}

    So as you can see, the TTY column gives you the name of the tty that the process is running under. If you want to extract *just* the TTY value for the process in question, you can use this command:

    Code:
    ps -ef | grep processname | awk '{print $6}'
    Which in the example above would result in:

    # ps -ef | grep X | awk '{print $6}'
    tty7

    What that command basically does is calls ps -ef, greps the results for processes with "X" in the name, and then uses awk to extract the 6th column from the output, which is the TTY column you saw in the first example.

    As fgconsole gives you the number of the tty without the text "tty" in front of it, you can use this command to acheive a similar effect:

    Code:
    ps -ef | grep X | awk '{print $6}' | sed 's/tty//'
    What this example does in addition to the above example is filters the result through sed to substitute all occurences of the text "tty" with "", acheiving the following result:

    #ps -ef | grep X | awk '{print $6}' | sed 's/tty//'
    7

    The active display is handled by the graphics subsystem / window manager itself so it would be most likely a utility specific to the desired window manager that would give you what you want. I must confess I've never come across one but you may have more luck searching for utilities specific to WMs.

  3. #3
    Hyperactive Member CyberSurfer's Avatar
    Join Date
    Aug 2000
    Location
    Old London Town
    Posts
    425

    Re: virtual terminals and displays

    Having read your question again, it looks like you might be looking for a language specific solution....if so, what language are you using? The commands I gave you use standard utilities found on all *nix systems so it's safe to assume any system your program looks at will have them.

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: virtual terminals and displays

    PHP Code:
    #include <unistd.h>

    int main()
    {
            
    printf("Terminal name: %s\n"ttyname(0));

            return 
    0;

    The 0 argument to ttyname refers to stdin of the current process. If input is not a terminal (ie input redirected into this process from another app) then ttyname returns null.

    For me this gives:
    Terminal name: /dev/pts/0
    I don't live here any more.

  5. #5
    Hyperactive Member CyberSurfer's Avatar
    Join Date
    Aug 2000
    Location
    Old London Town
    Posts
    425

    Re: virtual terminals and displays

    Yeah, what he said

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width