finding process id of a window

Marcus Schäfer ms at suse.de
Sun Apr 3 23:41:21 PDT 2005


Hi,

> I have few  X applications, started from the command line with identical
> command line, but each has a different DISPLAY environment variable.
> Is there a way to get the process id of a process from its window ?
> 
> Using xlsclients I can get the command line of the application, but since
> all of them use same command line, I can't find unique process id.
> 
> Thanks for your help.

maybe the _NET_WM_PID property can help here:

int main (int argc, char*argv[]) {
    Atom atom,actual_type;
    Display *dpy;
    int screen;
    char *atom_name;
    int actual_format;
    unsigned long nitems;
    unsigned long bytes_after;
    unsigned char *prop;
    int status;
    int pid;
    Window w=0;

    dpy = XOpenDisplay((char*)getenv("DISPLAY"));
    screen = DefaultScreen(dpy);
    if (argc!=2) {
        printf("Usage %s window-id\n",argv[0]);
        exit(-1);
    }
    sscanf(argv[1],"0x%x",(unsigned int*)(&w));
    atom = XInternAtom(dpy, "_NET_WM_PID", True);
    atom_name = XGetAtomName (dpy,atom);
    status = XGetWindowProperty(
        dpy, w, atom, 0, 1024,
        False, AnyPropertyType,
        &actual_type,
        &actual_format, &nitems,
        &bytes_after,
        &prop
    );

    if (status!=0) {
        printf("Cannot get _NET_WM_PID");
        exit(-2);
    }
    if (! prop) {
        printf ("No properties\n");
        exit (-3);
    }

    pid = prop[1] * 256;
    pid += prop[0];
    printf("pid of window 0x%x = %d\n",(unsigned int)w,pid);

    return 0;
}

please note this will only work if the _NET_WM_PID property can
be accessed and thus a windowmanager doing this is required.

Regards
Marcus
-- 
 Public Key available
 -------------------------------------------------------
 Marcus Schäfer (Res. & Dev.)   SUSE LINUX Products GmbH
 Tel: 0911-740 53 0             Maxfeldstrasse 5
 FAX: 0911-740 53 479           D-90409 Nürnberg
 http://www.suse.de             Germany
 -------------------------------------------------------



More information about the xorg mailing list