Operating System - HP-UX
1748163 Members
3727 Online
108758 Solutions
New Discussion юеВ

Re: Need a C function to retrieve currently executing file for a process

 
SOLVED
Go to solution
Bryce Cunningham
Occasional Contributor

Need a C function to retrieve currently executing file for a process

I have not found any C function or routine to get the currently executing file for a given process I.D. getrusage seems to be the closest I have, but I am unsure as I haven't found the structure of the rusage struct in the docs.

Can anyone recommend a function to retrieve this when given the process I.D.?

Thanks.


"Our problems are manmade; therefore they can be solved by man." - JFK
3 REPLIES 3
Andy Bennett
Valued Contributor

Re: Need a C function to retrieve currently executing file for a process

The simple is that there is no "reliable" way of getting the executable file name from the process ID. It is worth having a look at the thread "Retrieving program name" in the HPUX-DEVTOOLS mailing list (use thread name in the search at http://devresource.hp.com). This discusses some of the options and contains an elaborate program for finding a valid filename if possible.
Philip Chan_1
Respected Contributor

Re: Need a C function to retrieve currently executing file for a process

Well, there got to be a way to retrieve the name. With lsof, if a process id was given in the parameter then all actively opened files will be listed, and that binary file which the process started from is listed too.

If you have access to the lsof command source, browse through it and you may find what you want there.

~Philip
Kenneth Platz
Esteemed Contributor
Solution

Re: Need a C function to retrieve currently executing file for a process

Try this one:

#include

struct pst_status ps;
int pid;

/* determine the pid somewhere before here...*/
if ( pst_getstatus( &ps, sizeof(ps), 0, pid ) < 0 ) {
perror( "Cannot get status" );
return -1;
}

printf( "Process command line = %s\n", ps.pst_command );
printf( "Process base name = %s\n", pst.pst_ucomm );


Further information can be found in the man page for pstat(2).

I hope this helps.
I think, therefore I am... I think!