Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to obtain pid

Status
Not open for further replies.

rotis23

Programmer
Aug 29, 2002
121
0
0
GB
hi there,

how can i get the pid of a different process using its name?

are there any functions that do this?

TIA,

rotis23
 
That will probably depend on the operating system.

/JOlesen
 
linux.

if i'm in control of the other program, i'm thinking that the best way is to create a pid file using getpid(). the other prog can then read the file to get the pid.

if i'm not then maybe accessing proc or using ps - a bit clumsy though!

any other ideas?
 
You could read the proc directory using opendir(), readdir() and closedir() functions looking for any subdirectory directory that is purely numeric in name then examine the contents of the stat file in each such dub-directory. This will have the binary name within a pair of brackets.

When this name matches the target application you know that the directory name containing this file is the pid of the application.
 
newmangj's is a good way, not very easy to do.
i propose the old way; scann the output of ps.

#define MYPS "/bin/ps -efopid,comm" /* on a solaris-os */

FILE *ps = popen(MYPS,"r");
while(fgets(buff,MAXBUFF,ps)){
if 'name' not in 'buff' continue;
pid = atoi(buff);
...
...
}
pclose(ps);

it my be old-fashioned, but it works ans is portable.
you only have to change the ps statement.
for the if use a string function. ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
Hi

Not very similar but a related query. How do I copy a file
from one location to another using pure ANSI C calls ? Is there a portable, atomic function in ansi c libraries to do this ?

Thanks

Anand Pillai ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top