获取当前进程 可执行文件路径
#include <stdio.h>
#include <unistd.h>
#include <linux/limits.h>
int main() {
char dir[PATH_MAX] = {0};
int n = readlink("/proc/self/exe", dir, PATH_MAX);
printf("PATH_MAX: %d\n", PATH_MAX);
printf("readlink return: %d\n", n);
printf("dir: %s\n", dir);
return 0;
}
上面文件生成的可执行文件是 /home/yasi/tmp/t,执行结果如下:
PATH_MAX: 4096
readlink return: 16
dir: /home/yasi/tmp/t
获取指定id进程 可执行文件路径
查看 指定进程10010的 可执行文件路径
#include <stdio.h>
#include <unistd.h>
#include <linux/limits.h>
int main() {
char dir[PATH_MAX] = {0}, path[20]={0};
int pid=10010, n;
sprintf(path, "proc/%d/exe", pid);
n = readlink(path, dir, PATH_MAX);
printf("readlink return: %d\n", n);
printf("dir: %s\n", dir);
return 0;
}