如何获取当前进程对应之静态映像文件的绝对路径
这是一个x86/Linux Kernel 2.4.20-8系统中利用proc获取绝对路径的例子
#include
#include
#include
#define PATH_MAX 1024
int
main(int argc, char **argv)
{
char buf[PATH_MAX];
int count;
count = readlink("/proc/self/exe",buf,PATH_MAX);
if( count < 0 || count > PATH_MAX)
{
printf(" failed \n");
exit(1);
}
buf[count] = '\0';
printf(" %s's path is %s \n", argv[0], buf);
exit(0);
}
编译与执行
]# gcc proself.c
]# /home/a.out
/home/a.out's path is /home/a.out