本文介绍了psinfo_t solaris在其字段中不包含完整的进程名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从ssolaris中的procfs.h将psinfo数据文件(/proc/%d/psinfo)的过程信息从psinfo数据文件(/proc/%d/psinfo)读入struct psinfo_t时,没有在psinfo_t struct的字段pr_fname中获得完整的进程名称.

完整的psinfo_t结构定义在以下站点上显示:

http://docs.oracle.com/cd/E19253-01/816-5174/6mbb98ui2/index.html

仅如果进程名称小于等于15个字符,则获得完整的进程名称;否则,如果进程名称大于15个字符,则仅获得前15个字符,其余部分将被截断./p>

我正在使用的代码如下:

#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二进制文件从哪里获取进程名称开始感到好奇.

解决方案

Solaris 11.3 SRU 5引入了/proc/<pid>/execname,其中包含完整的命令名,因此您可以检查该文件是否存在,如果存在则使用该文件,否则将失败.回到受限的pr_fname.

请参见 Solaris 11.3 SRU 5.6:ps(1)和/proc/< pid>中的更新/{cmdline,environ,execname} 了解详情.

Not getting full process name in psinfo_t struct's field pr_fname while reading process info from psinfo data file(/proc/%d/psinfo) into struct psinfo_t from procfs.h in solaris.

Full psinfo_t struct definition is present on below site:

http://docs.oracle.com/cd/E19253-01/816-5174/6mbb98ui2/index.html

Only if the process name is less than equal to 15 characters then I am getting the full process name other wise if process name is more than 15 characters then I am getting only first 15 characters of process name rest characters are truncated.

The code I am using is as below:

#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;
}

Do I have to read some other file (other than psinfo) from procfs file system in order to get full process name.Also if I am using belwo ps command from command line then I am able to get full process name:

ps -p 4970 -o comm

but I don't want to get process name by executing ps command inside my code.I am curious from where ps binary is picking up the process name.

解决方案

Solaris 11.3 SRU 5 introduced /proc/<pid>/execname which contains the full command name, so you can check to see if that file exists and use it if so, else fall back to the limited pr_fname.

See Solaris 11.3 SRU 5.6: updates in ps(1) and /proc/<pid>/{cmdline,environ,execname} for details.

这篇关于psinfo_t solaris在其字段中不包含完整的进程名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 21:07