zoukankan      html  css  js  c++  java
  • 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:%s processname:%s ",path,processname);
    }
    [sam@hzhsan test]$

  • 相关阅读:
    python函数
    python文件IO操作
    LAMP项目上线
    linux下的小工具
    linux下自有服务
    Lesson_Swift2
    枚举
    使用文件流下载附件
    Global中的Timer计时器
    IE11下的NPOI导出提示__doPostBack未定义解决方案
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3184848.html
Copyright © 2011-2022 走看看