How to get current process name in linux?

后端 未结 7 1812
醉话见心
醉话见心 2020-12-01 05:49

How can I get the process name in C? The same name, which is in /proc/$pid/status. I do not want to parse that file. Is there any programmatic way of doing this

相关标签:
7条回答
  • 2020-12-01 06:17

    This is a version that works on macOS, FreeBSD and Linux.

    #if defined(__APPLE__) || defined(__FreeBSD__)
    const char * appname = getprogname();
    #elif defined(_GNU_SOURCE)
    const char * appname = program_invocation_name;
    #else
    const char * appname = "?";
    #endif
    
    0 讨论(0)
提交回复
热议问题