c语言占用cpu的程序,Windows下用C语言获取进程cpu使用率,内存使用,IO情况

转自: http://zhangyafeikimi.iteye.com/blog/378658

process_stat.h

/** @file * @brief 进程统计信息函数的声明 * @author 张亚霏 * @date 2009/05/03 * @version 0.1 * */ #ifndef PROCESS_STAT_H #define PROCESS_STAT_H #ifdef __cplusplus extern "C" { #endif typedef long long int64_t; typedef unsigned long long uint64_t; /// 获取当前进程的cpu使用率,返回-1失败 int get_cpu_usage(); /// 获取当前进程内存和虚拟内存使用量,返回-1失败,0成功 int get_memory_usage(uint64_t* mem, uint64_t* vmem); /// 获取当前进程总共读和写的IO字节数,返回-1失败,0成功 int get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes); #ifdef __cplusplus } #endif #endif/*PROCESS_STAT_H*/

process_stat_win.c

/** @file * @brief 进程统计信息函数的实现 * @author 张亚霏 * @date 2009/05/03 * @version 0.1 * * 部分代码来自MSDN的例子 * 部分代码来自google chromium项目 * * 需要连接到psapi.lib */ #include #include #include #include "process_stat.h" /// 时间转换 static uint64_t file_time_2_utc(const FILETIME* ftime) { LARGE_INTEGER li; assert(ftime); li.LowPart = ftime->dwLowDateTime; li.HighPart = ftime->dwHighDateTime; return li.QuadPart; } /// 获得CPU的核数 static int get_processor_number() { SYSTEM_INFO info; GetSystemInfo(&info); return (int)info.dwNumberOfProcessors; } int get_cpu_usage() { //cpu数量 static int processor_count_ = -1; //上一次的时间 static int64_t last_time_ = 0; static int64_t last_system_time_ = 0; FILETIME now; FILETIME creation_time; FILETIME exit_time; FILETIME kernel_time; FILETIME user_time; int64_t system_time; int64_t time; int64_t system_time_delta; int64_t time_delta; int cpu = -1; if(processor_count_ == -1) { processor_count_ = get_processor_number(); } GetSystemTimeAsFileTime(&now); if (!GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, &kernel_time, &user_time)) { // We don't assert here because in some cases (such as in the Task Manager) // we may call this function on a process that has just exited but we have // not yet received the notification. return -1; } system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time)) / processor_count_; time = file_time_2_utc(&now); if ((last_system_time_ == 0) || (last_time_ == 0)) { // First call, just set the last values. last_system_time_ = system_time; last_time_ = time; return -1; } system_time_delta = system_time - last_system_time_; time_delta = time - last_time_; assert(time_delta != 0); if (time_delta == 0) return -1; // We add time_delta / 2 so the result is rounded. cpu = (int)((system_time_delta * 100 + time_delta / 2) / time_delta); last_system_time_ = system_time; last_time_ = time; return cpu; } int get_memory_usage(uint64_t* mem, uint64_t* vmem) { PROCESS_MEMORY_COUNTERS pmc; if(GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) { if(mem) *mem = pmc.WorkingSetSize; if(vmem) *vmem = pmc.PagefileUsage; return 0; } return -1; } int get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes) { IO_COUNTERS io_counter; if(GetProcessIoCounters(GetCurrentProcess(), &io_counter)) { if(read_bytes) *read_bytes = io_counter.ReadTransferCount; if(write_bytes) *write_bytes = io_counter.WriteTransferCount; return 0; } return -1; }

可以这样使用:

/** @file * @brief 进程统计信息函数的测试 * @author 张亚霏 * @date 2009/05/03 * @version 0.1 * */ #include "process_stat.h" #include #include int main() { while(1) { int cpu; uint64_t mem, vmem, r, w; cpu = get_cpu_usage(); get_memory_usage(&mem, &vmem); get_io_bytes(&r, &w); printf("CPU使用率: %u/n",cpu); printf("内存使用: %u 字节/n", mem); printf("虚拟内存使用: %u 字节/n", vmem); printf("总共读: %u 字节/n", r); printf("总共写: %u 字节/n", w); Sleep(1000); } return 0; }

另外,可参考:

Windows API一日一练(91)GetProcessMemoryInfo函数

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/419994.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

oracle 本地使用命令导入数据到远程主机

第一步:装载oracle客户端 第二部:配置tnsnames.ora.  db_172.21.1.7 (DESCRIPTION (ADDRESS (PROTOCOL TCP)(HOST 172.21.1.7)(PORT 1526))(CONNECT_DATA (SERVER DEDICATED)(SERVICE_NAME db))) 第三部:打开命令行,执行如下命令: imp netelnusr/netelnusrdb_172.21.1.7…

c语言计算据标准时间多少天,C语言系列--时间处理

首先明确几个概念UTC 协调世界时,又称世界标准时间或世界协调时间。GMT 格林尼治平均时间或格林尼治标准時間,由于地球每天的自转是有些不规则的,而且正在缓慢减速,因此格林威治时间已经不再被作为标准时间使用日历时间&#xff0…

Perl学习笔记(六)--文件(一)

一、文件描述符: 访问文件时用来代表文件的数字。 它是系统资源,系统限制打开的文件描述符数量。 Perl中只在某些系统调用时才使用它 文件句柄: 功能同文件描述符,但是与文件描述符不是一个东西。 Perl使用文件句柄代表文件。 文件…

android mkdirs 不起作用,Android mkdirs()创建一个零字节文件而不是文件夹

在我的Android应用程序中,我试图在SD卡上创建以下文件夹:/mnt/sdcard/OSGiComponents/admin/felix-cache/这是代码:File cacheDir new File( Environment.getExternalStorageDirectory().getAbsolutePath() "/OSGiComponents/admin/felix-cache/&qu…

向ComboBox列表框中添加Enum的全部数据

相当于利用反射的 GetValues与GetNames foreach (HETieXinType theType in Enum.GetValues(typeof(HETieXinType))) foreach (string mode in Enum.GetNames(typeof(BooleanInteractionMode))) { ledResponseModesComboBox.Items.Add(mode); switchResponseModesComboBox.Items…

android sqlite 备份数据库文件,android – 将SQLite数据库备份和还原到sdcard

这是我的代码:// Local databaseInputStream input new FileInputStream(from);// create directory for backupFile dir new File(DB_BACKUP_PATH);dir.mkdir();// Path to the external backupOutputStream output new FileOutputStream(to);// transfer bytes…

Spring中配置数据源的4种形式 ---转

不管采用何种持久化技术,都需要定义数据源。Spring中提供了4种不同形式的数据源配置方式: spring自带的数据源(DriverManagerDataSource),DBCP数据源,C3P0数据源,JNDI数据源。 1.spring自带的数据源 DriverManagerDataSource XML…

android中拖动文字实现功能,Android:图片中叠加文字,支持拖动改变位置

之所以做了这么一个Demo,是因为最近项目中有一个奇葩的需求:用户拍摄照片后,分享到微信的同时添加备注,想获取用户在微信的弹出框输入的内容,保存在自己的服务器上。而事实上,这个内容程序是无法获取的&…

Mac 下nginx 环境的配置

这个是在度娘那里学来的。 因为是使用brew所以先安装: 安装命令如下:curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz -C/usr/local --strip 1当brew安装成功后,就可以随意安装自己想要的软件了,例如w…

android抽屉屏幕右滑,android - Android - 使滑动抽屉从左向右滑动 - 堆栈内存溢出...

我使用下面的XML布局在我的应用程序中实现了“Sliding Drawer”:(我从androidpeople.com得到了这个例子)android:layout_width"fill_parent" android:layout_height"fill_parent"xmlns:android"http://schemas.android.com/apk/res/android"andr…

bzoj 3196/tyvj p1730 二逼平衡树

原题链接&#xff1a;http://www.tyvj.cn/p/1730 树套树。。。 如下&#xff1a; 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<algorithm> 5 #define lc root<<1 6 #define rc root<<1|1 7 #define INF…

观察者模式与Boost.Signals

1&#xff09; 观察者模式定义 略&#xff0c;各种设计模式的书上都有定义。 2&#xff09; 观察者模式一般实现 观察者模式一般实现&#xff0c;都是“被观察者”保存一个“观察者”的列表&#xff0c;循环这个列表来通知“观察者”。代码&#xff0c;其中使用了boost的智能…

Android获取最新发送短信的基本信息,没有之一

注册&#xff1a; getContentResolver().registerContentObserver( Uri.parse("content://sms"), true, new SmsObserver(this, new Handler())); 监听&#xff1a; //用于检测发出的短信 public class SmsObserver extends Conten…

联想android刷机教程,超详细的联想刷机教程~带你嘻刷刷

一、刷机是什么说到“刷机”&#xff0c;很多人可能会和“升级”混淆起来&#xff0c;其实升级和刷机并不是同一概念。通俗地讲&#xff0c;升级就是对手机内的软件或系统进行升级&#xff0c;比如很多厂商手机都支持的OTA空中在线升级。而刷机&#xff0c;则相当于就是重装系统…

我的github地址

我的github仓库地址 https://github.com/xutiantian/Test转载于:https://www.cnblogs.com/xuxiaomeng/p/4455850.html

多看 android6,多看阅读本地版

为您推荐&#xff1a;多看阅读《多看阅读本地版》是一款由多看科技倾情研发打造的海量优质完本小说免费在线阅读app软件&#xff0c;这款软件的功能非常的全面&#xff0c;操作性简单&#xff0c;上手起来非常的容易&#xff0c;在这款软件里&#xff0c;各位用户们将能够于此体…

UIProgressView-初识IOS

好几天没更新了&#xff0c;学的时候太紧&#xff0c;没时间复习了都。今天刚好有时间&#xff0c;多更几个。 今天复习的是UIProgressView,我们常见使用在修改某些属性的时候经常用到&#xff0c;比如透明度&#xff0c;今天我们介绍一个简单的使用例子 定义什么的&#xff0c…

android正则判断两个符号之间,Android字母、数字、字符任意两种组合正则验证

释放双眼&#xff0c;带上耳机&#xff0c;听听看~&#xff01;最近朋友有个用户名验证&#xff0c;要求字母、数字、字符任意两种组合即可&#xff0c;让我帮写个正则验证&#xff0c;现在正则验证如下&#xff1a;/*** 判断是否匹配正则** param regex 正则表达式* param inp…

android手机deviceowner,删除 androidDeviceOwnerWiFiConfiguration

删除 androidDeviceOwnerWiFiConfigurationDelete androidDeviceOwnerWiFiConfiguration2021/3/24本文内容命名空间&#xff1a;microsoft.graphNamespace: microsoft.graph重要提示&#xff1a; /beta 版本下的 Microsoft Graph API 可能会更改;不支持生产使用。Important: Mi…