Tho*_*mas 6 c c++ freebsd c++11
我正在尝试编写一个返回父进程名称的函数,
如果是bash
,那么它应该返回bash
.
const std::string &getParentProcessName() {
static std::string name;
auto ppid = getppid();
#ifdef __FreeBSD__
// ?????
#else
...
#endif
name = "unknown";
return name;
}
Run Code Online (Sandbox Code Playgroud)
使用kinfo_getproc函数
pid_t pid = ...;
struct kinfo_proc *proc = kinfo_getproc(pid);
if (proc) {
printf("Name %s\n", proc->ki_comm);
//ki_comm should be the program name
//see the sys/user.h header file for other relevant fields.
free(proc);
}
Run Code Online (Sandbox Code Playgroud)
另请参阅libprocstat函数以获取进程信息。