title: struct tms values returned by times() all zero or negative
body: <p>I'm trying to implement the times() function in C programming.</p>
  
  <p>I'm using the <code>struct tms</code> structure which consists of the fields: tms_utime, tms_cutime,
  tms_stime and tms_cstime.</p>
  
  <p>In order to implement the times() function in my program, I do:</p>
  
  <ol>
  <li><p>Before I fork and create a child, I call the times function (in the parent process).</p>
  
  <pre><code>times(&amp;start_tms);
  </code></pre></li>
  <li>I create a pipe and I pass the times of start structure to the pipe when I'm in the child process.</li>
  <li>The child executes a simple <code>ls -l</code> command</li>
  <li><p>When the child finishes he execution, the father calls for the second time the times() function.</p>
  
  <pre><code>times(&amp;end_tms);
  </code></pre>
  
  <p>Unfortunately, the times of end_tms are all zero! Weird, but I don't know why.</p></li>
  </ol>
  
  <p>Some things I don't understand in my program are:</p>
  
  <p>1) In the first <code>printfs</code> the times of the struct start are negative. Why is that?
  2) When I run the program, why do I get zeros for times? What am i doing wrong?</p>
  
  <p>My program is as follows:</p>
  
  <p>Thanks, in advance</p>
  
  <hr>
  
  <pre><code>#include &lt;sys/times.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;time.h&gt;
  
  int main() {
  
  printf("test\n");
  
  int     fd[2]; //two pointers
  int nbytes;
  char    string[] = "Hello, world!\n";
  char    readbuffer[80];
  struct tms start_tms;
  struct tms end_tms;
  clock_t start, end;
  double cpu_time_used;
  
  pipe(fd);
  
  //once we have established the pipeline we fork the child
  pid_t   childpid;
  pid_t pid_waitpid;
  
  //NEW MODIFICATION!!! call times before fork()!!!
  times(&amp;start_tms);
  
  //they return negative values, but why???
  printf("Test start_tms.tms_utime = %f\n\n",start_tms.tms_utime);
  printf("Test start_tms.tms_cutime = %f\n\n",start_tms.tms_cutime);
  printf("Test start_tms.tms_stime = %f\n\n",start_tms.tms_stime);
  
  
  if((childpid = fork()) == -1)
              {
                      perror("fork");
                      exit(1);
              }
  
  if(childpid == 0)
              {
  
                      /* Child process closes up input side of pipe */
                       close(fd[0]);
  
                     /* call times function */ 
                     /*times(&amp;start_tms);*/
  
  
                        //REMOVED!!!!
                      //write(fd[1], string, (strlen(string)+1));
                      write(fd[1], &amp;start_tms.tms_cutime, sizeof(clock_t));
                      write(fd[1], &amp;start_tms.tms_utime, sizeof(clock_t));
                      write(fd[1], &amp;start_tms.tms_stime, sizeof(clock_t));
  
                       //execute /bin/ls
                      execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);
  
                      exit(0);
  
  
              }
  else
              {
                      /* Parent process closes up output side of pipe */
                      close(fd[1]);
  
                      /* NEW MODIFICATION, wait for the child!!! */
                       if( (pid_waitpid  = waitpid(childpid,NULL,0) ) == -1)
                      {
                               perror("waitpid");
                               exit(1);
                       }
  
  
  
                      /* call times for capturing end times */
                      times(&amp;end_tms);
  
                      /* define t1, t2, variables */
                      clock_t t1,t2,t3;
  
  
                       //REMOVED!!!!
                      //nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
                      read(fd[0], &amp;t1, sizeof(clock_t));
                      read(fd[0], &amp;t2, sizeof(clock_t));
                      read(fd[0], &amp;t3, sizeof(clock_t));
  
                      printf("Received string: %s\n\n", readbuffer);
                      printf("Test t1 = %f\n\n",t1);
                      printf("Test end_tms.tms_utime = %f\n\n",end_tms.tms_utime);
                      printf("Test end_tms.tms_cutime = %f\n\n",end_tms.tms_cutime);
                      printf("Test end_tms.tms_stime = %f\n\n",end_tms.tms_stime);
  
                      /* Calculate times, unfortunately return zero, but why??? */
                      double cpu_time = end_tms.tms_cutime - t1;
                      double utime = end_tms.tms_utime - t2;
                      double stime = end_tms.tms_stime - t3;
  
                      //Unfortunately printfs return zero, but why???
                      printf("cpu time %f\n\n",cpu_time);
                      printf("cpu Utime %f\n\n",utime);
                      printf("cpu Stime %f\n\n",stime);
  
  
  }
  
  }
  </code></pre>