将进程信息从 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 binary 从哪里获取进程名称。
The pr_fname
领域的psinfo_t
结构体长度为 16 个字节,每源代码:
#define PRFNSZ 16 /* Maximum size of execed filename */
因此它实际上会被截断为最多 15 个字符。
您可以从以下位置获取实际二进制文件的名称/proc/PID/map
。对于 32 位进程,执行的二进制文件将映射到地址0x00010000
.
您可以浏览源代码ps
at http://src.illumos.org/source/xref/illumos-gate/usr/src/cmd/ps/如果您想查看它在哪里找到数据。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)