Find the answer to your Linux question:
Results 1 to 4 of 4
Hi All, I have to find the executable file path by using the PID (process ID). User will input some PID and required output is corresponding executable file path. For ...
  1. #1
    Just Joined!
    Join Date
    Aug 2008
    Posts
    49

    find executable path

    Hi All,

    I have to find the executable file path by using the PID (process ID).

    User will input some PID and required output is corresponding executable file path.
    For this i have to write a C program using system call. Anyone help me.

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Post

    I can't understand your requirement..note : every process creates a directory under /proc...if you know the PID you can get the Envorimental values and others process related info from there,check those files ....
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  3. #3
    Just Joined! rjayavrp's Avatar
    Join Date
    Apr 2009
    Location
    Incredible India
    Posts
    42

    Thumbs up Try this

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>

    char *getpath(int pid)
    {
    FILE *fp;
    char command[256];
    char output[64];
    char reply[32];
    strcpy(reply, "");
    sprintf(output, "/tmp/.path.%d", pid);
    sprintf(command, "/usr/proc/bin/pmap %d | nawk '\n{\nif(substr($3,3,1)==\"x\") {\nprint $4\nexit(0);\n}\n}\n'>%s",
    pid, output);
    system(command);
    if((fp=fopen(output, "r"))!=NULL)
    {
    fgets(reply, 32, fp);
    fclose(fp);
    unlink(output);
    }
    return(reply);
    }

    main(int argc, char **argv)
    {
    printf("%s", getpath(atoi(argv[1])));
    }

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I hope you'll forgive me if I think that spawning off awk from a C program is not the best way to go about this.

    First of all, is this a homework assignment? It does sort of sound like one. Homework assignments are not allowed on this forum.

    Assuming that it is not, Lakshmipathi is correct. Every PID has a directory under /proc. Each of these directories has a lot of information about the particular process. One file in that directory is called cmdline, and I suspect it will help you. Check "man proc" for more information.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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