Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get real path of application from pid?

Tags:

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

I am using Mac OS X.

like image 585
RLT Avatar asked Sep 22 '11 08:09

RLT


People also ask

How do I get the full path of running process?

You can, for instance, open a command line, change to any random directory, and run the executable by specifying a full path to it; GetCurrentDirectory() will return the directory you executed from rather than the executable's directory.

What is the command used to find the path of a process?

The answer is the pwd command, which stands for print working directory.


1 Answers

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 <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <libproc.h>  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; } 
like image 100
Alen Stojanov Avatar answered Oct 16 '22 11:10

Alen Stojanov