【linuxC语言】获取进程信息

news/2024/10/20 0:01:12/
cle class="baidu_pl">
cle_content" class="article_content clearfix">
content_views" class="markdown_views prism-atom-one-dark">cap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">

class="toc">

文章目录

  • 前言
  • 一、getrusage函数
  • 二、示例代码
  • 总结


前言

在Linux环境下࿰c;了解和获取进程的信息对于系统监控、性能优化以及调试等任务至关重要。C语言作为Linux系统编程的主要语言之一࿰c;提供了丰富的系统调用和库函数࿰c;可以帮助我们轻松地获取进程相关的信息。本文将介绍如何使用C语言编写代码来获取进程的信息࿰c;并举例说明如何获取进程的ID、命令行参数、状态等信息࿰c;以及如何获取进程的资源使用情况。


一、getrusage函数

getrusage() 函数用于获取进程或其子进程的资源使用情况。资源使用情况包括 CPU 时间、内存、文件 I/O 等。它的原型定义在 <sys/resource.h> 头文件中。

下面是 getrusage() 函数的原型:

<code class="prism language-c">class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><sys/resource.h>class="token keyword">int class="token function">getrusageclass="token punctuation">(class="token keyword">int whoclass="token punctuation">, class="token keyword">struct class="token class-name">rusage class="token operator">*usageclass="token punctuation">)class="token punctuation">;
code>

参数 who 指定了要获取资源使用情况的进程类型。常用的取值有:

RUSAGE_SELF:获取调用进程的资源使用情况。
RUSAGE_CHILDREN:获取所有子进程的资源使用情况。

如果你使用 RUSAGE_CHILDREN 参数来获取子进程的资源使用情况࿰c;那么 getrusage() 函数将会返回所有子进程的总和࿰c;而不是单个进程的信息。如果你需要获取单个进程的信息࿰c;你可以使用其他方法࿰c;比如通过进程的ID来获取该进程的信息。

一种方法是使用 getrusage() 函数来获取特定进程的资源使用情况࿰c;但是它只能获取到调用它的进程的资源使用情况࿰c;而无法获取其他进程的信息。

其他特殊值还有 RUSAGE_THREAD 用于获取当前线程的资源使用情况(Linux特有)。
参数 usage 是一个指向 struct rusage 结构的指针࿰c;用于存储获取到的资源使用情况信息。

下面是 struct rusage 结构的定义:

<code class="prism language-c">class="token keyword">struct class="token class-name">rusage class="token punctuation">{class="token keyword">struct class="token class-name">timeval ru_utimeclass="token punctuation">; class="token comment">/* 用户 CPU 时间 */class="token keyword">struct class="token class-name">timeval ru_stimeclass="token punctuation">; class="token comment">/* 系统 CPU 时间 */class="token keyword">long   ru_maxrssclass="token punctuation">;         class="token comment">/* 最大常驻集大小(以 kBytes 为单位) */class="token keyword">long   ru_ixrssclass="token punctuation">;          class="token comment">/* 未使用 (历史遗留) */class="token keyword">long   ru_idrssclass="token punctuation">;          class="token comment">/* 未使用 (历史遗留) */class="token keyword">long   ru_isrssclass="token punctuation">;          class="token comment">/* 未使用 (历史遗留) */class="token keyword">long   ru_minfltclass="token punctuation">;         class="token comment">/* 缺页错误次数 */class="token keyword">long   ru_majfltclass="token punctuation">;         class="token comment">/* 分页错误次数 */class="token keyword">long   ru_nswapclass="token punctuation">;          class="token comment">/* 未使用 (历史遗留) */class="token keyword">long   ru_inblockclass="token punctuation">;        class="token comment">/* 从块设备中读取的块数量 */class="token keyword">long   ru_oublockclass="token punctuation">;        class="token comment">/* 向块设备写入的块数量 */class="token keyword">long   ru_msgsndclass="token punctuation">;         class="token comment">/* 发送的消息数量 */class="token keyword">long   ru_msgrcvclass="token punctuation">;         class="token comment">/* 接收的消息数量 */class="token keyword">long   ru_nsignalsclass="token punctuation">;       class="token comment">/* 收到的信号数量 */class="token keyword">long   ru_nvcswclass="token punctuation">;          class="token comment">/* 由于等待虚拟时钟转换而进行的上下文切换 */class="token keyword">long   ru_nivcswclass="token punctuation">;         class="token comment">/* 由于除了等待虚拟时钟转换之外的原因进行的上下文切换 */
class="token punctuation">}class="token punctuation">;
code>

c="https://img-blog.csdnimg.cn/direct/c3eb3168ca5a4f648ff69babb8f9a529.png#pic_center" alt="在这里插入图片描述" />

二、示例代码

下面是一个简单的示例代码࿰c;演示如何使用 getrusage() 函数获取进程的资源使用情况:

<code class="prism language-c">class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><stdio.h>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><sys/resource.h>class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{class="token keyword">struct class="token class-name">rusage usageclass="token punctuation">;class="token keyword">if class="token punctuation">(class="token function">getrusageclass="token punctuation">(RUSAGE_SELFclass="token punctuation">, class="token operator">&usageclass="token punctuation">) class="token operator">== class="token operator">-class="token number">1class="token punctuation">) class="token punctuation">{class="token function">perrorclass="token punctuation">(class="token string">"getrusage"class="token punctuation">)class="token punctuation">;class="token keyword">return class="token number">1class="token punctuation">;class="token punctuation">}class="token function">printfclass="token punctuation">(class="token string">"用户 CPU 时间:%ld 微秒\n"class="token punctuation">, usageclass="token punctuation">.ru_utimeclass="token punctuation">.tv_usecclass="token punctuation">)class="token punctuation">;class="token function">printfclass="token punctuation">(class="token string">"系统 CPU 时间:%ld 微秒\n"class="token punctuation">, usageclass="token punctuation">.ru_stimeclass="token punctuation">.tv_usecclass="token punctuation">)class="token punctuation">;class="token function">printfclass="token punctuation">(class="token string">"缺页错误次数:%ld\n"class="token punctuation">, usageclass="token punctuation">.ru_minfltclass="token punctuation">)class="token punctuation">;class="token function">printfclass="token punctuation">(class="token string">"分页错误次数:%ld\n"class="token punctuation">, usageclass="token punctuation">.ru_majfltclass="token punctuation">)class="token punctuation">;class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>

c="https://img-blog.csdnimg.cn/direct/2d746dfda80d499d89103edf2a137b2e.png#pic_center" alt="在这里插入图片描述" />

在这个示例中࿰c;我们使用 getrusage() 函数获取当前进程的资源使用情况࿰c;并打印了用户 CPU 时间、系统 CPU 时间、缺页错误次数和分页错误次数。


总结

通过本文的介绍࿰c;我们了解了如何使用C语言编程获取进程的信息。首先࿰c;我们可以使用系统调用getpid()来获取当前进程的ID࿰c;也可以使用getppid()获取当前进程的父进程ID。其次࿰c;我们可以通过读取/proc文件系统下的相应文件࿰c;比如/proc/[PID]/cmdline来获取进程的命令行参数࿰c;通过/proc/[PID]/status来获取进程的状态等信息。最后࿰c;我们还介绍了如何使用getrusage()函数来获取进程的资源使用情况࿰c;包括CPU时间、内存等。

总的来说࿰c;通过C语言编程获取进程的信息是一项相对简单而强大的任务࿰c;可以帮助我们更好地理解和监控系统的运行情况࿰c;为系统管理和优化提供有力的支持。


http://www.ppmy.cn/news/1449727.html

相关文章

Flink on k8s 入门实战

Flink on k8s 入门实战 背景 最近一直在学习flink on k8s,踩了不少坑,折腾了好几天,终于是达到了自己想要的效果。 首先说一下,我要实现的效果是什么?Application模式下,ip和端口保持不变,根据不同的路径访问不同的任务。 环境部署 这一步也是我遇到问题最多的一步…

[C++]22:C++11_part_one

C11 一.列表初始化&#xff1a;1.{}初始化&#xff1a;2.C11扩大了列表初始化的范围&#xff1a;3.std::initializer_list1.简单类型的列表初始化&#xff1a;2.复杂类型的列表初始化3.实现vector的列表初始化4.实现list的列表初始化&#xff1a;5.不支持列表初始化&#xff1a…

OceanBase开发者大会实录-陈文光:AI时代需要怎样的数据处理技术?

本文来自2024 OceanBase开发者大会&#xff0c;清华大学教授、蚂蚁技术研究院院长陈文光的演讲实录—《AI 时代的数据处理技术》。完整视频回看&#xff0c;请点击这里&#xff1e;> 大家好&#xff0c;我是清华大学、蚂蚁技术研究院陈文光&#xff0c;今天为大家带来《AI 时…

Vue.js最佳实践和性能优化技巧

Vue.js 是一个流行的前端 JavaScript 框架&#xff0c;用于构建用户界面和单页面应用&#xff08;SPA&#xff09;。为了确保你的 Vue 应用运行得更快、更有效率&#xff0c;以下是一些最佳实践和性能优化技巧&#xff1a; ### 1. 组件设计最佳实践 - **合理拆分组件**&#x…

网络安全之WebShell截获

不知道这是哪个大哥的手笔有没有认领的20240424十一点四十分左右 大哥计算机的具体信息贴上了&#xff0c;还好大哥没有put成功&#xff0c;返回405&#xff01; IP地址 31.49.67.43:36668 MAC地址 80:05:88:48:37:b5ToolB的MAC地址厂商信息查询网站&#xff1a;https://tool…

python 如何判断是函数还是方法 (function or method)

示例代码&#xff1a; def test_fn():passclass Test(object):staticmethoddef s_fn():passclassmethoddef c_fn(cls):passdef my_fn(self):pass如何判断是可调用的方法&#xff1a; hasattr(test_fn, __call__) # true hasattr(Test.s_fn, __call__) # true hasattr(Test.c…

Flutter笔记:Widgets Easier组件库(8)使用图片

Flutter笔记 Widgets Easier组件库&#xff08;8&#xff09;&#xff1a;使用图片 - 文章信息 - Author: 李俊才 (jcLee95) Visit me at CSDN: https://jclee95.blog.csdn.netMy WebSite&#xff1a;http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddress o…

Transformers:它们如何转换您的数据?

一、说明 在快速发展的人工智能和机器学习领域&#xff0c;一项创新因其对我们处理、理解和生成数据的方式产生深远影响而脱颖而出&#xff1a;Transformers。Transformer 彻底改变了自然语言处理 &#xff08;NLP&#xff09; 及其他领域&#xff0c;为当今一些最先进的 AI 应…