Convert PID to process name

I want to iterate over the process IDs (e.g., returned by listing /proc)
and turn them into process names. Is there a function I can use to do this?
Thanks,
Randy Hyde

I want to iterate over the process IDs (e.g.,
returned by listing /proc)
and turn them into process names. Is there a
function I can use to do this?
Thanks,
Randy Hyde

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <devctl.h>
#include <stdlib.h>
#include <dirent.h>
#include <fnmatch.h>
#include <sys/procfs.h>

static int print_name( char *pid )
{
struct {
procfs_debuginfo info;
char buff[ _POSIX_PATH_MAX ];
} name;
char ppath[ _POSIX_PATH_MAX ];
int fd;

sprintf( ppath, “/proc/%s/as”, pid );

if( -1 == ( fd = open( ppath, O_RDONLY ))) {
perror( “open()” );
return 1;
}

memset( &name, 0, sizeof( name ));

if( EOK != devctl( fd, DCMD_PROC_MAPDEBUG_BASE, &name, sizeof( name ), 0 )) {
close( fd );
perror( “devctl()” );
return 1;
}

close( fd );
printf( “%12s\t%s\n”, pid, name.info.path );
return 0;
}

int main( int argc, char **argv )
{
DIR *dir;
struct dirent *den;

if( NULL == ( dir = opendir( “/proc” ))) {
perror( “opendir()” );
return 1;
}

while( NULL != ( den = readdir( dir ))) {
if( 0 == fnmatch( “[0-9]*”, den->d_name, FNM_PATHNAME )) {
print_name( den->d_name );
}
}

closedir( dir );
return 0;
}

Thanks.
Randy Hyde

“Serge Yuschenko” <nospam@forums.openqnx.com> wrote in message
news:29139395.1044393398761.JavaMail.fliu@tiger…

I want to iterate over the process IDs (e.g.,
returned by listing /proc)
and turn them into process names. Is there a
function I can use to do this?
Thanks,
Randy Hyde

#include <errno.h
#include <fcntl.h
#include <stdio.h
#include <devctl.h
#include <stdlib.h
#include <dirent.h
#include <fnmatch.h
#include <sys/procfs.h

static int print_name( char *pid )
{
struct {
procfs_debuginfo info;
char buff[ _POSIX_PATH_MAX ];
} name;
char ppath[ _POSIX_PATH_MAX ];
int fd;

sprintf( ppath, “/proc/%s/as”, pid );

if( -1 == ( fd = open( ppath, O_RDONLY ))) {
perror( “open()” );
return 1;
}

memset( &name, 0, sizeof( name ));

if( EOK != devctl( fd, DCMD_PROC_MAPDEBUG_BASE, &name, sizeof( name ),
0 )) {
close( fd );
perror( “devctl()” );
return 1;
}

close( fd );
printf( “%12s\t%s\n”, pid, name.info.path );
return 0;
}

int main( int argc, char **argv )
{
DIR *dir;
struct dirent *den;

if( NULL == ( dir = opendir( “/proc” ))) {
perror( “opendir()” );
return 1;
}

while( NULL != ( den = readdir( dir ))) {
if( 0 == fnmatch( “[0-9]*”, den->d_name, FNM_PATHNAME )) {
print_name( den->d_name );
}
}

closedir( dir );
return 0;
}