linux c获取内存和cpu使用率,linux c程序获取cpu使用率及内存使用情况

fgets (buff, sizeof(buff), fd);

fgets (buff, sizeof(buff), fd);

fgets (buff, sizeof(buff), fd);

sscanf (buff, "%s %u %s", m->name, &m->total, m->name2);

fgets (buff, sizeof(buff), fd); //从fd文件中读取长度为buff的字符串再存到起始地址为buff这个空间里

sscanf (buff, "%s %u", m->name2, &m->free, m->name2);

fclose(fd); //关闭文件fd

}

int cal_cpuoccupy (CPU_OCCUPY *o, CPU_OCCUPY *n)

{

unsigned long od, nd;

unsigned long id, sd;

int cpu_use = 0;

od = (unsigned long) (o->user + o->nice + o->system +o->idle);//第一次(用户+优先级+系统+空闲)的时间再赋给od

nd = (unsigned long) (n->user + n->nice + n->system +n->idle);//第二次(用户+优先级+系统+空闲)的时间再赋给od

id = (unsigned long) (n->user - o->user); //用户第一次和第二次的时间之差再赋给id sd = (unsigned long) (n->system - o->system);//系统第一次和第二次的时间之差再赋给sd if((nd-od) != 0)

cpu_use = (int)((sd+id)*10000)/(nd-od); //((用户+系统)乖100)除(第一次和第二次的时间差)再赋给g_cpu_used

else cpu_use = 0;

//printf("cpu: %u\n",cpu_use);

return cpu_use;

}

get_cpuoccupy (CPU_OCCUPY *cpust) //对无类型get函数含有一个形参结构体类弄的指针O {

FILE *fd;

int n;

char buff[256];

CPU_OCCUPY *cpu_occupy;

cpu_occupy=cpust;

fd = fopen ("/proc/stat", "r");

fgets (buff, sizeof(buff), fd);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值