社区
Linux/Unix社区
帖子详情
在linux下如何通过编程获得网络流量或cpu占用率?
yflu
2003-08-19 09:44:02
3x
...全文
223
8
打赏
收藏
在linux下如何通过编程获得网络流量或cpu占用率?
3x
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
8 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
liuty2006
2003-08-22
打赏
举报
回复
run result as follow:
CPU usage: 97.0%
CPU usage: 97.0%
CPU usage: 99.0%
CPU usage: 97.0%
CPU usage: 95.0%
CPU usage: 81.2%
CPU usage: 94.1%
CPU usage: 92.1%
question:
should the "idle" be include in?
liuty2006
2003-08-22
打赏
举报
回复
up
liuty2006
2003-08-20
打赏
举报
回复
uop
XSY_VC
2003-08-20
打赏
举报
回复
发
yflu
2003-08-20
打赏
举报
回复
uop什么意思?
blh
2003-08-20
打赏
举报
回复
cpu占用率
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
struct cpu_usage
{
unsigned long user;
unsigned long sys;
unsigned long nice;
unsigned long idle;
};
static struct cpu_usage old_cd, new_cd;
static tmp[64];
void get_cpu_data(struct cpu_usage *pcd)
{
FILE *file = NULL;
file = fopen("/proc/stat", "r");
if (file == NULL)
{
perror("fopen");
exit(-1);
}
memset(pcd, 0, sizeof(struct cpu_usage));
fscanf(file, "%s %lu %lu %lu %lu", tmp, &(pcd->user), &(pcd->sys), &(pcd->nice), &(pcd->idle
));
fclose(file);
}
void cpu_usage()
{
double user, sys, nice, idle, total;
get_cpu_data(&new_cd);
user = (double)(new_cd.user - old_cd.user);
sys = (double)(new_cd.sys - old_cd.sys);
nice = (double)(new_cd.nice - old_cd.nice);
idle = (double)(new_cd.idle - old_cd.idle);
total = user + sys + nice + idle;
memcpy(&old_cd, &new_cd, sizeof(struct cpu_usage));
fprintf(stdout, "CPU usage: %2.1f\%\n", (idle / total) * 100);
}
int main()
{
get_cpu_data(&old_cd);
while(1)
{
sleep(1);
cpu_usage();
}
return 0;
}
其实可以使用glibtop库实现
头文件在/usr/include/glibtop.h
yuanlei1978113
2003-08-20
打赏
举报
回复
一般这些信息都在/proc/这个目录下保存着:
网络信息:/proc/net/netstat
cpu信息:/proc/cpuinfo AND /proc/stat
你要获得这些信息时就读取这些文件即可。
axlw
2003-08-19
打赏
举报
回复
netstat
top
性能测试
Linux
系统资源监控工具【nmon】的使用和监控数据分析
通过性能监控工具nmon的... 课程提供的下载资料: 包含通过nmon收集的
Linux
服务器资源信息ecs-lc_220715_1019.nmon文件,大家在没有
Linux
服务器的情况下,可以通过对这个文件进行nmon数据解析和数据分析的学习。
编程
获取
linux
cpu
占用率
,内存
占用率
以及硬盘使用率
http://hnsswjjxy-163-com.iteye.com/blog/910322
Linux
下提供top、ps命令查看当前
cpu
、mem使用情况,简要介绍如下: 一、使用ps查看进程的资源占用 ps -aux 查看进程信息时,第三列就是
CPU
占用。 [root@localhost utx86]# ps -aux | grep my_process Warni
编程
获取
linux
的
CPU
使用率内存
占用率
编程
获取
linux
的
CPU
使用率内存
占用率
编程
获取
Linux
的
cpu
占用率
和 mem使用情况
Linux
下提供top、ps命令查看当前
cpu
、mem使用情况,简要介绍如下:一、使用ps查看进程的资源占用ps -aux查看进程信息时,第三列就是
CPU
占用。[root@localhost utx86]# ps -aux | grep my_processWarning: bad syntax, perhaps a bogus '-'? See /usr/share/do...
如何获取
cpu
占用率
和 mem使用情况
一、shell下提供top、ps命令查看当前
cpu
、mem使用情况,简要介绍如下: 1. 使用ps查看进程的资源占用 ps -aux 查看进程信息时,第三列就是
CPU
占用。 [root@localhost utx86]# ps -aux | grep my_process Warning: bad syntax, perhaps a bogus '-'? See /usr/share
Linux/Unix社区
23,116
社区成员
74,506
社区内容
发帖
与我相关
我的任务
Linux/Unix社区
Linux/Unix社区 应用程序开发区
复制链接
扫一扫
分享
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章