萬盛學電腦網

 萬盛學電腦網 >> 服務器教程 >> linux c 獲得當前進程的進程名和執行路徑

linux c 獲得當前進程的進程名和執行路徑

如何得到當前進程的進程名和執行路徑。寫了個程序分享一下   復制代碼 代碼如下:
[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 = '';
        return (size_t)(path_end - processdir);
}
int main()
{
        char path[PATH_MAX];
        char processname[1024];
        get_executable_path(path, processname, sizeof(path));
        printf("directory:%snprocessname:%sn",path,processname);
}
[sam@hzhsan test]$

copyright © 萬盛學電腦網 all rights reserved