Résumé de la façon d'obtenir votre propre nom de processus au moment de l'exécution en C, C ++
get_process_name.cpp
#include <iostream>
#include <cstdlib>
#include <errno.h>
extern char *__progname;
int main(int argc, char const* argv[])
{
// argv[0]
std::cout << "argv[0] : " << argv[0] << std::endl;
// linux environment
std::cout << "getenv(\"_\") : " << getenv("_") << std::endl;
// libc
std::cout << "__progname : " << __progname << std::endl;
// errno.h
std::cout << "program_invocation_name : " << program_invocation_name << std::endl;
std::cout << "program_invocation_short_name : " << program_invocation_short_name << std::endl;
return 0;
}
Exemple d'exécution
/tmp$ g++ get_process_name.cpp -o hoge && ./hoge
argv[0] : ./hoge
getenv("_") : ./hoge
__progname : hoge
program_invocation_name : ./hoge
program_invocation_short_name : hoge
/tmp$ g++ get_process_name.cpp -o hoge && $PWD/hoge
argv[0] : /tmp/hoge
getenv("_") : /tmp/hoge
__progname : hoge
program_invocation_name : /tmp/hoge
program_invocation_short_name : hoge
c - How to get current process name in linux? - Stack Overflow
Recommended Posts