如何在 C/C 中以编程方式获取进程信息 来自 Solaris 系统?

发布于 2024-07-11 16:34:07 字数 287 浏览 5 评论 0原文

是否有关于如何在 Solaris 上收集系统和进程信息的 C/C++ 库和文档?

虽然我可以解析命令行工具,但我宁愿使用一个使任务更容易完成的库。

谢谢

编辑: 建议使用 /proc 虚拟目录来收集信息,但是它并不比解析命令行工具好多少,从某种意义上说,我需要实现某种对我需要的每条数据进行自定义解析。

我正在寻找类似于 Windows 或 MacOS 的 c 库的东西,通过基于 c 的系统 API 提供此信息,但是我在 Google 上没有运气。

Is there a C/C++ library, and documentation about how to collect system and process information on Solaris?

Although I could parse command-line tools, I'd rather use a library that makes the task easier to do.

Thanks

Edit: It has been suggested to use the /proc virtual directory to collect information, however its not much better than parsing command-line tools, in the sense that I'll need to implement some sort of custom parsing for every piece of data I need.

I'm looking for something along the lines of c libraries for Windows or MacOS that provides this information through a c-based systems API, however I'm having no luck with Google.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

離殇 2024-07-18 16:34:07

您可以使用 kstat API 获取此类信息。

man -s 3KSTAT  kstat

您可以查看它在 OpenSolaris 中的使用方式 vmstatiostat 来源。

有关 processus 的信息,我会查看 ps

You can get this kind of information with kstat API.

man -s 3KSTAT  kstat

You can see how it is used in OpenSolaris vmstat and iostat source.

For information about processus, I'd look at ps.

笙痞 2024-07-18 16:34:07

Solaris 有 /proc 虚拟目录,它允许您使用以下命令收集有关进程的各种信息文件系统 I/O 函数。

Solaris has the /proc virtual directory, which allows you to gather all sorts of information about processes using filesystem I/O functions.

晨曦÷微暖 2024-07-18 16:34:07

我会按照 CrashWorks 的建议使用 /proc 虚拟目录。 我在 aux 和 linux 上都做过这个。 需要记住的一件事是,当我在 Linux 上使用 /proc 目录时,文件的格式因内核而异。

我不知道 Solaris 方面的情况如何,但这可能意味着您的解决方案无法从一个 Solaris 平台移植到另一个平台。

I would use the /proc virutal dir as CrashWorks has suggested. I've done this on both aux and linux. One thing to keep in mind is when I did use the /proc dir on linux the format of the files varied from one kernel to another.

I don't know what the situation is like on the Solaris side but this could mean that your solution will not be portable from one solaris platform to another.

飘逸的'云 2024-07-18 16:34:07

getrusage() 怎么样?

what about getrusage()?

说谎友 2024-07-18 16:34:07

我绝对不是这个主题的专家,但上学期我在一项作业中做了一些非常类似的事情,当时我们被要求拍摄流程快照。 不幸的是,这种方法需要深入内核,这可能不是您想要做的。

我发现这篇文章很有帮助。

无论如何,这里有一些片段。

 write_lock_irq(&tasklist_lock);
  for_each_process(task) {

    if (system_or_user == 0)
      print_mem_user(task);
    if (system_or_user == 1)
      print_mem_system(task);
  }
  write_unlock_irq(&tasklist_lock);

您需要锁定某些数据结构的想法,否则有时内核会挂起。 “for_each_process”是在某处定义的宏,但我不记得它是如何工作的 D:

static void print_mem_system(struct task_struct *task)
{
  struct mm_struct *mm;

  if (task -> mm == NULL){ // this is how you distinguish system processes from user processes
    myarraypid[totalnumberofprocesses] = task -> pid; // store process id's into myarraypid[], which you can later copy back to user space for printing/display. Additional information would be found in a "task_struct" which is Linux's implementation of a process. 



  }


}

我的一些同学采取了不同的方法并深入研究了“ps”实用程序的源代码。 我相信我正在 Linux 2.6.18-92.1.13.e15 上工作。 免责声明:这对我有用,但您的里程可能会有所不同。 我很可能会偏离正轨,但我不想把你引向错误的方向。

I'm definately not an expert on the subject, but I did something very similar for an assignment last semester when we were required to take snapshots of processes. Unfortunately this method requires digging into the kernel which is probably not what you want to do.

I found this article helpful.

Anyways here are some snippets.

 write_lock_irq(&tasklist_lock);
  for_each_process(task) {

    if (system_or_user == 0)
      print_mem_user(task);
    if (system_or_user == 1)
      print_mem_system(task);
  }
  write_unlock_irq(&tasklist_lock);

The idea you need to lock down some data structures or sometimes the kernel will hang. "for_each_process" is a macro defined somewhere but I don't remember how it works D:

static void print_mem_system(struct task_struct *task)
{
  struct mm_struct *mm;

  if (task -> mm == NULL){ // this is how you distinguish system processes from user processes
    myarraypid[totalnumberofprocesses] = task -> pid; // store process id's into myarraypid[], which you can later copy back to user space for printing/display. Additional information would be found in a "task_struct" which is Linux's implementation of a process. 



  }


}

Some of my classmates took different approaches and dived into the source of the "ps" utility. I believe I was working on Linux 2.6.18-92.1.13.e15. Disclaimer: This worked for me but your mileage may vary. I could very well be off the wall and I don't want to lead you down the wrong direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文