Get real path of application from pid?

前端 未结 5 1662
轻奢々
轻奢々 2020-12-08 01:01

How can I get the process details like name of application & real path of application from process id?

I am using Mac OS X.

5条回答
  •  猫巷女王i
    2020-12-08 01:41

    It's quite easy to get the process name / location if you know the PID, just use proc_name or proc_pidpath. Have a look at the following example, which provides the process path:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main (int argc, char* argv[])
    {
        pid_t pid; int ret;
        char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
    
        if ( argc > 1 ) {
            pid = (pid_t) atoi(argv[1]);
            ret = proc_pidpath (pid, pathbuf, sizeof(pathbuf));
            if ( ret <= 0 ) {
                fprintf(stderr, "PID %d: proc_pidpath ();\n", pid);
                fprintf(stderr, "    %s\n", strerror(errno));
            } else {
                printf("proc %d: %s\n", pid, pathbuf);
            }
        }
    
        return 0;
    }
    

提交回复
热议问题