您的位置:首页 > 其它

读proc/pid得到进程名称

2009-11-05 11:23 513 查看
#include <stdio.h>

#include <stdlib.h>
#include <sys/procfs.h>
#include <memory.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <unistd.h>
#include <stropts.h>

void get_program_name( char *p_name_space ){
char strBuf[100];

memset( strBuf, 0, sizeof( strBuf));
sprintf( strBuf, "/proc/%d/psinfo", getpid());

int fd = open( strBuf, O_RDONLY);

if( fd == -1 ){
printf("open err!");
}

psinfo_t retval;
read(fd, &retval, sizeof(psinfo_t));

char p_name[1024] = "";
sprintf( p_name, "%s", retval.pr_fname );

char *p_name_begin = strchr( p_name, '/');

close(fd);

strcpy( p_name_space, p_name_begin + 1 );
}

int main(){

char p_name[1024] = "";
get_program_name( p_name );
printf("proc name :%s/n", p_name );

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: