0

I begin learn about the Linux C,but i meet the problem so confused me.
I use the function times.but return the value equals 0.
ok i made the mistak,I changed the code: But the is not much relative with printf. clock_t is define with long in Linux.so i convert clock_t to long.
This is my code:

#include <sys/times.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
   long clock_times;
   struct tms begintime;
   sleep(5);
   if((clock_times=times(&begintime))==-1)
      perror("get times error");
   else
   {
      printf("%ld\n",(long)begintime.tms_utime);
      printf("%ld\n",(long)begintime.tms_stime);
      printf("%ld\n",(long)begintime.tms_cutime);
      printf("%ld\n",(long)begintime.tms_cstime);
   }
   return 0;
}

the output: 0 0 0 0
the also return 0;
and I using gdb to debug,Also the variable of begintimes also to be zero. there is no relative with printf function. Please

friddle
  • 1,257
  • 2
  • 9
  • 11
  • 6
    Your `printf` is using floating point format `%f`, but the time is a `long` integer. Try using `%ld` (long integer) instead of `%f` (float). – lurker Aug 29 '13 at 02:48
  • 1
    ... and turn some warnings on. Your compiler will tell you the problem immediately. – Carl Norum Aug 29 '13 at 03:37
  • possible duplicate of [Why do the first and the third printf work so differently?](http://stackoverflow.com/questions/17514395/why-do-the-first-and-the-third-printf-work-so-differently) – Carl Norum Aug 29 '13 at 03:37
  • I made the mistake in first printf.I changed.but there is no relative with the printf,i just wonder why using `times`.the struct variable is zero. – friddle Aug 29 '13 at 05:38

2 Answers2

3

This is not unusual; the process simply hasn't used enough CPU time to measure. The amount of time the process spends in sleep() doesn't count against the program's CPU time, as times() measures The CPU time charged for the execution of user instructions (among other related times) which is the amount of time the process has spent executing user/kernel code.

Change your program to the following which uses more CPU and can therefore be measured:

#include <sys/times.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
   long clock_times;
   struct tms begintime;
   unsigned i;

   for (i = 0; i < 1000000; i++)
      time(NULL);    // An arbitrary library call

   if((clock_times=times(&begintime))==-1)
      perror("get times error");
   else
   {
      printf("%ld %ld %ld %ld\n",
        (long)begintime.tms_utime,
        (long)begintime.tms_stime,
        (long)begintime.tms_cutime,
        (long)begintime.tms_cstime);
   }
   return 0;
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
2

Your code using close to none CPU time, so results are correct. Sleep suspends your program execution - everything that happens in this time is not your execution time, so it wouldn't be counted.

Add empty loop and you'll see the difference. (ofc., disable compiler optimisations - or empty loop will be removed).

Take a look at 'time' program output (time ./a.out) - it prints 'real' time (estimated by gettimeofday(), i suppose), user time (time wasted by your userspace code) and system time (time wasted within system calls - e.g. write to file, open network connection, etc.).

(sure, by 'wasted' i mean 'used', but whatever)

keltar
  • 17,711
  • 2
  • 37
  • 42