#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define ONE_MB (1024 * 1024)
float g_cpu_used;
int cpu_num;
struct occupy
{
char name[20];
unsigned int user;
unsigned int nice;
unsigned int system;
unsigned int idle;
};
void cal_occupy(struct occupy *, struct occupy *);
void get_occupy(struct occupy *);
int getCpuInfo();
int getMemInfo(void);
int main(void)
{
getCpuInfo();
getMemInfo();
}
int getCpuInfo()
{
struct occupy ocpu[10];
struct occupy ncpu[10];
int i;
cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
get_occupy(ocpu);
sleep(1);
get_occupy(ncpu);
for (i=0; i<cpu_num; i++)
{
#include <stdlib.h>
#include <unistd.h>
#define ONE_MB (1024 * 1024)
float g_cpu_used;
int cpu_num;
struct occupy
{
char name[20];
unsigned int user;
unsigned int nice;
unsigned int system;
unsigned int idle;
};
void cal_occupy(struct occupy *, struct occupy *);
void get_occupy(struct occupy *);
int getCpuInfo();
int getMemInfo(void);
int main(void)
{
getCpuInfo();
getMemInfo();
}
int getCpuInfo()
{
struct occupy ocpu[10];
struct occupy ncpu[10];
int i;
cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
get_occupy(ocpu);
sleep(1);
get_occupy(ncpu);
for (i=0; i<cpu_num; i++)
{

本文详细介绍了如何在Unix/Linux环境中通过编程获取CPU和内存的占用率。内容涵盖解析系统文件,使用struct和float进行数据处理,以及包含必要的头文件如<sys/resource.h>和<unistd.h>。通过这些方法,可以实时监控系统的资源使用情况。
最低0.47元/天 解锁文章
264

被折叠的 条评论
为什么被折叠?



