I had a similar kind of work. If you want to get the process-id of a currently running process, you search thru the /proc directories. Inside this /proc you can see some directories named using numbers, those are all actually process-id's, and symbolic link
exe inside each numbered directory will show the process-name.
This link will provide more information:
http://www.linuxexposed.com/Articles...em---proc.html
I had written a small program, to get the process id of a process which was created using system(). I thought this will be usefull. The code is here...
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
unsigned int getProcessID( char *process )
{
int target_link;
DIR *dir_p;
DIR *dir_proc;
struct dirent *dir_entry_p;
struct dirent *dir_proc_p;
char dir_name[35];
char target_name[235];
char exe_link[235];
char process_id[20];
int i;
dir_p = opendir( "/proc/" ); /* Open /proc/ directory */
while( NULL != (dir_entry_p = readdir(dir_p))) /* Reading /proc/ entries */
{
if ( strspn( dir_entry_p->d_name, "0123456789" ) == strlen( dir_entry_p->d_name ) ) /* Checking for numbered directories */
{
strcpy( process_id, dir_entry_p->d_name ); /* Saving the numbered directory i.e process-id */
strcpy( dir_name, "/proc/" );
strcat( dir_name, dir_entry_p->d_name );
strcat( dir_name, "/" ); /* Obtaining the full-path eg: /proc/24657/ */
if ( ( dir_proc = opendir( dir_name ) ) ) /* Open that numbered directory */
{
while ( ( dir_proc_p = readdir( dir_proc ) ) ) /* Reading its entries */
{
if ( strcmp( dir_proc_p->d_name, "exe" ) == 0 ) /* Check for that exe link file */
{
strcat( exe_link, dir_name );
strcat( exe_link, dir_proc_p->d_name ); /* Getting the full-path of that exe link */
/* Getting the target of the exe ie to which binary it points to */
target_link = readlink( exe_link, target_name, sizeof( target_name ) );
target_name[ target_link ] = '\0';
if ( strstr( target_name, process ) != NULL ) /* Searching for process name in the target name */
{
printf( "\n\n%s FOUND --- PROCESS_ID %s \n\n\n", process, process_id );
return atoi(process_id); /* GOT IT: return the process-id */
}
strcpy( exe_link ,"" );
}
}
}
else
printf( "Dir %s opening failed \n", dir_name );
}
}
closedir(dir_p);
return 0;
}
int main()
{
system( "emacs &" );
printf( " %u ", getProcessID( "emacs" ) );
return 0;
}
I was able to use this program successfully. Let me know if there are some other ways.