linux c 获取系统时钟,C语言获取时钟时间、用户CPU时间和系统CPU时间

关于时钟时间、用户CPU时间和系统CPU时间的含义在这篇文章里有说明:linux中查看进程的时钟时间、用户CPU时间和系统CPU时间。

C语言里可以通过times函数获取这三种时间,times函数说明如下:

#include

clock_t times(struct tms *buf);

参数tms的结构如下:

struct tms {

clock_t tms_utime; /* user time */

clock_t tms_stime; /* system time */

clock_t tms_cutime; /* user time of children */

clock_t tms_cstime; /* system time of children */

};

其中时间都是以滴答数(clock tick)为单位,详细可以用man 2 times查看帮助手册。下面的示例用来计算执行系统命令date消耗的三种时间值。

#include

#include

#include

#include

int main(void)

{

//获取滴答数,在ubuntu 12.04下为100

int clktck = 0;

if ((clktck = sysconf(_SC_CLK_TCK)) < 0) {

printf("%s\n", "sysconf error");

exit(0);

}

struct tms tmsstart, tmsend;

clock_t start, end;

//获取开始时间

if ((start = times(&tmsstart)) == -1) {

printf("%s\n", "times error");

exit(0);

}

//执行系统函数date

system("date");

//获取结束时间

if ((end = times(&tmsend)) == -1) {

printf("%s\n", "times error");

exit(0);

}

printf("real: %7.2f\n", (end - start)/(double) clktck);

printf("user: %7.2f\n",

(tmsend.tms_utime - tmsstart.tms_utime)/(double) clktck);

printf("sys: %7.2f\n",

(tmsend.tms_stime - tmsstart.tms_stime)/(double) clktck);

printf("child user: %7.2f\n",

(tmsend.tms_cutime - tmsstart.tms_cutime)/(double) clktck);

printf("child sys: %7.2f\n",

(tmsend.tms_cstime - tmsstart.tms_cstime)/(double) clktck);

return 1;

}

编译执行上面的程序,输出如下:

$ ./a.out

Sun Dec 9 12:50:39 CST 2012

real: 0.01

user: 0.00

sys: 0.00

child user: 0.00

child sys: 0.00

其中child user就是执行date命令消耗的用户CPU时间,child sys就是执行date命令消耗的系统CPU时间。这里会发现这两个值都为0,因为滴答数为100,只能精确到小数点后面两位,date的执行时间非常快,所以就为0了。如何精确到小数点后面3位呢?

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Linux系统提供的 /proc/stat 文件来获取CPU各个核心的使用率。具体方法如下: 1. 打开 /proc/stat 文件,读取其的数据。 2. 在 /proc/stat 文件CPU使用情况被记录在以 "cpu" 开头的一行,每个核心对应一行。 3. 每行的数据由多个字段组成,其一个字段为 "cpu" 或 "cpu0"、"cpu1" 等,表示对应的核心。 4. 第二个字段到第五个字段分别表示该核心从系统启动到当前时刻所经过的时间(以时钟滴答为单位)及该时间内的 CPU 使用情况。 5. 通过对比两次读取的 /proc/stat 文件的数据,可以计算出 CPU使用率。 以下是一个简单的示例程序,实时输出 CPU 各个核心的使用率: ```c #include <stdio.h> #include <string.h> #include <unistd.h> int main() { unsigned long long last_total[64] = {0}; unsigned long long last_idle[64] = {0}; unsigned long long total[64] = {0}; unsigned long long idle[64] = {0}; float usage[64] = {0}; int cores = 0; FILE *fp = fopen("/proc/stat", "r"); char buf[256]; while (fgets(buf, sizeof(buf), fp)) { if (strncmp(buf, "cpu", 3) == 0) { int index = 0; sscanf(buf, "cpu%d %llu %llu %llu %llu", &index, &total[index], &total[index+1], &total[index+2], &total[index+3]); if (index == 0) { idle[index] = total[index+3]; } else { idle[index] = total[index+3] - last_total[index+3]; } usage[index] = 100.0 * (1.0 - (idle[index] - last_idle[index]) / (total[index] - last_total[index])); last_total[index] = total[index]; last_idle[index] = idle[index]; cores = index + 1; } } fclose(fp); while (1) { fp = fopen("/proc/stat", "r"); while (fgets(buf, sizeof(buf), fp)) { if (strncmp(buf, "cpu", 3) == 0) { int index = 0; sscanf(buf, "cpu%d %llu %llu %llu %llu", &index, &total[index], &total[index+1], &total[index+2], &total[index+3]); if (index == 0) { idle[index] = total[index+3]; } else { idle[index] = total[index+3] - last_total[index+3]; } usage[index] = 100.0 * (1.0 - (idle[index] - last_idle[index]) / (total[index] - last_total[index])); last_total[index] = total[index]; last_idle[index] = idle[index]; } } fclose(fp); printf("CPU usage:\n"); for (int i = 0; i < cores; i++) { printf("Core %d: %.2f%%\n", i, usage[i]); } printf("\n"); sleep(1); } return 0; } ``` 注意,以上程序只是一个简单示例,实际应用可能需要进行更加复杂的数据处理和计算,以得到更准确的 CPU 使用率信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值