【发布时间】:2016-03-09 09:50:30
【问题描述】:
在将 psinfo 数据文件 (/proc/%d/psinfo) 中的进程信息从 solaris 中的 procfs.h 读取到 struct psinfo_t 时,未在 psinfo_t 结构的字段 pr_fname 中获取完整的进程名称。
完整的 psinfo_t 结构定义在以下站点:
http://docs.oracle.com/cd/E19253-01/816-5174/6mbb98ui2/index.html
只有当进程名称小于等于 15 个字符时,我才会获得完整的进程名称,否则如果进程名称超过 15 个字符,那么我只会截断进程名称的前 15 个字符。
我使用的代码如下:
#include <iostream>
#include <cstdlib>
#include <procfs.h>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
// get the pid from command line
int pid = atoi(argv[1]);
// create the pstatus struct from procfs
psinfo_t info;
char file[100];
sprintf(file, "/proc/%d/psinfo", pid);
ifstream in(file);
if (in)
{
in.read((char*)&info, sizeof(psinfo_t));
in.close();
cout << "My Name: " << info.pr_fname << endl;
}
else
{
cout << "Process Not Exists!" << endl;
}
return 0;
}
我是否必须从 procfs 文件系统中读取一些其他文件(除了 psinfo)才能获得完整的进程名称。 另外,如果我从命令行使用 belwo ps 命令,那么我可以获得完整的进程名称:
ps -p 4970 -o comm
但我不想通过在我的代码中执行 ps 命令来获取进程名称。 我很好奇 ps 二进制文件从哪里获取进程名称。
【问题讨论】: