首页 > 编程 > C > 正文

linux c 获得当前进程的进程名和执行路径(示例)

2020-01-26 15:59:10
字体:
来源:转载
供稿:网友

复制代码 代码如下:

[sam@hzhsan test]$ more test_processname.cpp
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

size_t get_executable_path( char* processdir,char* processname, size_t len)
{
        char* path_end;
        if(readlink("/proc/self/exe", processdir,len) <=0)
                return -1;
        path_end = strrchr(processdir,  '/');
        if(path_end == NULL)
                return -1;
        ++path_end;
        strcpy(processname, path_end);
        *path_end = '/0';
        return (size_t)(path_end - processdir);
}
int main()
{
        char path[PATH_MAX];
        char processname[1024];
        get_executable_path(path, processname, sizeof(path));
        printf("directory:%s/nprocessname:%s/n",path,processname);
}
[sam@hzhsan test]$


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选