经过一段时间后,utime和stime返回的值仍为0?

时间:2017-09-20 15:08:50

标签: linux time proc stat

我正在从文件/ proc / [PID] / stat中读取信息,我想解析运行main的当前进程的字段utimestime,但这些值等于0?

这是一个重现问题的最小C测试:

#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>


int main()
{
  FILE *fp;
  char *pathname = (char *) malloc(30*sizeof(char));
  pid_t pid = getpid();
  int utime, stime, cutime, cstime, result;
  int total = 0;
  int i = 0;
  if (pathname == NULL) return -1;

  sprintf(pathname, "/proc/%d/stat", pid);
  fp = fopen(pathname, "r");
  if (fp == NULL) return -1;

  // Busy loop to cause time to be spent
  for (i = 0; i < 1000; i++)
    {
      total = total + i;
    }

  printf("total value has been calculated %d \n", total);  
  result = fscanf (fp, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %d %d %d %d\n",
  &utime,
  &stime,
  &cutime,
  &cstime);
  if (result < 4)
    {
      return -1;
    }
  else
    {
      printf("utime = %d \nstime = %d \ncutime = %d \ncstime = %d \n", utime, stime, cutime, cstime);
  }
  return 0;
}

输出:

total value has been calculated 499500 
utime = 0 
stime = 0 
cutime = 0 
cstime = 0 

我还试图捕捉我程序的/proc/[PID]/stat,我发现即使程序处于休眠状态,这些值仍保持为0(睡眠(200))

我在这里缺少什么吗? 感谢

0 个答案:

没有答案