如何获取运行在用户和系统模式下的时间?

cacf10 2005-08-08 07:19:06
是在/proc文件系统里边么?我实在是不会了,哪位知道给指导一下吧!谢谢!!
...全文
93 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cacf10 2005-08-09
  • 打赏
  • 举报
回复
呵呵,原来是我没看清楚文档阿,呵呵,谢谢阿!!
mfkwmfk 2005-08-08
  • 打赏
  • 举报
回复
代码写的很烂哈,楼主不要介意。。。
mfkwmfk 2005-08-08
  • 打赏
  • 举报
回复
ft,怎么和我们上次的课程内容刚好一样啊?

/********************************
rpm.h

*******************************/
#include <stdio.h>
#include <pthread.h>

#define CPUNUM 1

pthread_attr_t attr;
pthread_t tid;


struct rpm{
long curr_stime;
long prev_stime;
long curr_utime;
long prev_utime;
}cpuinfo[CPUNUM];

void cpuagent();

void console();



/**************************************
linux system management
mfkwmfk 2005.7.13
***************************************/
#include "rpm.h"
void cpuagent(){
FILE *fn;
char tempchar1[256];
char tempchar2[256];
char tempchar3[256];
char tempchar4[256];
char tempchar5[256];
long stime;
long utime;
long ulowtime;
long temp1;
char buf[256];
do{
int i=0;
bzero(buf,256);
fn=fopen("/proc/stat","r");
fgets(buf,256,fn);
for(;i<CPUNUM;i++){
bzero(buf,256);
bzero(tempchar1,256);
bzero(tempchar2,256);
bzero(tempchar3,256);
bzero(tempchar4,256);
bzero(tempchar5,256);
fgets(buf,256,fn);
sscanf(buf,"%s %s %s %s %s",tempchar1,tempchar2,tempchar3,tempchar4,tempchar5);
utime=atoi(tempchar3);
ulowtime=atoi(tempchar4);
stime=atoi(tempchar5);
utime+=ulowtime;
cpuinfo[i].prev_stime=cpuinfo[i].curr_stime;
cpuinfo[i].prev_utime=cpuinfo[i].curr_utime;
cpuinfo[i].curr_stime=stime;
cpuinfo[i].curr_utime=utime;
}
fclose(fn);
sleep(2);
} while(1);
}


void console(){

while (1){
int i=0;
int j;
printf("cpu number : %d\n",CPUNUM);
for (;i<CPUNUM;i++){
int j=i+1;
printf("The %dth cpu current user time: %d\n",j,cpuinfo[i].curr_utime);
printf("The %dth cpu previous user time: %d\n",j,cpuinfo[i].prev_utime);
printf("The %dth cpu current system time: %d\n",j,cpuinfo[i].curr_stime);
printf("The %dth cpu previous system time: %d\n",j,cpuinfo[i].prev_stime);
printf("\n");
}
sleep(2);
}
}


void main(){
char cmd='m';
pthread_create(&tid, NULL,cpuagent, NULL);

do{
printf("Choose the cmd: \n");
printf(" [1]show me the cpu info\n");


void console(){

while (1){
int i=0;
int j;
printf("cpu number : %d\n",CPUNUM);
for (;i<CPUNUM;i++){
int j=i+1;
printf("The %dth cpu current user time: %d\n",j,cpuinfo[i].curr_utime);
printf("The %dth cpu previous user time: %d\n",j,cpuinfo[i].prev_utime);
printf("The %dth cpu current system time: %d\n",j,cpuinfo[i].curr_stime);
printf("The %dth cpu previous system time: %d\n",j,cpuinfo[i].prev_stime);
printf("\n");
}
sleep(2);
}
}


void main(){
char cmd='m';
pthread_create(&tid, NULL,cpuagent, NULL);

do{
printf("Choose the cmd: \n");
printf(" [1]show me the cpu info\n");
printf(" [q]exit\n");
cmd=getchar();
switch (cmd){
case '1':
console(); break;
}
}while((cmd !='Q')&&(cmd !='q'));
exit();
}

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧