Problem with implementing the times() function in C (struct tms times return zero/negative values)

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Programming Problem with implementing the times() function in C (struct tms times return zero/negative values)
# 1  
Problem with implementing the times() function in C (struct tms times return zero/negative values)


i'm trying to implement the times() function and i'm programming in C.
I'm using the "struct tms" structure which consists of the fields:

  • The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process.
  • The tms_stime structure member is the CPU time charged for execution by the system on behalf of the calling process.
  • The tms_cutime structure member is the sum of the tms_utime and tms_cutime times of the child processes.
  • The tms_cstime structure member is the sum of the tms_stime and tms_cstime times of the child processes.

In order to implement the times() function in my program, i do:

1. Before i fork and create a child, i call the times function (in the parent process).

2. I create a pipe and i pass the times of start structure to the pipe when i'm in the child process.
3. The child executes a simple "ls -l" command
4. When the child finishes he execution, the father calls for the second time the times() function.

Unfortunately, the times of end_tms are all zero!! Weird, but i don't know why...

Some things i don't understand in my program are:

1)In the first "printfs" the times of the struct start are negative..but why??
2) Also, when i run the program, i get zeros for times, but why??What am i doing wrong??

My program is as follows

Thanks, in advance

#include <sys/times.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main() {


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;


//once we have established the pipeline we fork the child
pid_t   childpid;
pid_t pid_waitpid;

//call times before fork()!!!

//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)

if(childpid == 0)
                        /* Child process closes up input side of pipe */
                       /* call times function */ 

                        write(fd[1], &start_tms.tms_cutime, sizeof(clock_t));
                        write(fd[1], &start_tms.tms_utime, sizeof(clock_t));
                        write(fd[1], &start_tms.tms_stime, sizeof(clock_t));

                         //execute /bin/ls
                        execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);


                        /* Parent process closes up output side of pipe */
                        /*  wait for the child */
                         if( (pid_waitpid  = waitpid(childpid,NULL,0) ) == -1)

                        /* call times for capturing end times */

                        /* define t1, t2, variables */
                        clock_t t1,t2,t3;
                        read(fd[0], &t1, sizeof(clock_t));
                        read(fd[0], &t2, sizeof(clock_t));
                        read(fd[0], &t3, sizeof(clock_t));

                        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);



and my output is as follows

Test start_tms.tms_utime = -0.000000 //negative??
Test start_tms.tms_cutime = -0.000000 //negative??
Test start_tms.tms_stime = -0.000000 //negative???
total 236
-rw-r--r-- 1 g_p g_p   71 Feb 23  2011 test.c
-rwx------ 1 g_p g_p   52 Mar 12  2011
-rwx------ 1 g_p g_p   81 Mar 12  2011
-rwx------ 1 g_p g_p   81 Mar 12  2011 notepad2
-rwx------ 1 g_p g_p  420 Mar 12  2011
-rwx------ 1 g_p g_p  319 Mar 12  2011
-rwx------ 1 g_p g_p  300 Mar 12  2011
-rwx------ 1 g_p g_p   98 Mar 12  2011
-rwx------ 1 g_p g_p   74 Mar 12  2011
-rw------- 1 g_p g_p 1167 Jun 16 12:50 d2.c
Received Time: 0.000000
Test t1 = 0.000000
Test end_tms.tms_utime = 0.000000
Test end_tms.tms_cutime = 0.000000
Test end_tms.tms_stime = 0.000000
cpu time 0.000000
cpu Utime 0.000000
cpu Stime 0.000000

# 2  
you are using wrong format for printing out the tms structure members, which are clock_t and you use %f
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #973
Difficulty: Easy
GCC is the default compiler for the Linux kernel source.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[bash] wanted: function with a clean way for multiple return values

Hi, I have a small part of a project which is done as a bash script. bash was selected as an portability issue that works out of the box. In this script I have an exec shell-function, a wrapper around arbitrary commands. I want to have STDOUT, as an addon STDERR and the EXIT-CODE of a specified... (5 Replies)
Discussion started by: stomp
5 Replies

2. Shell Programming and Scripting

Returning and capturing multiple return values from a function

Hi I am pretty confused in returning and capturing multiple values i have defined a function which should return values "total, difference" i have used as #!/usr/bin/ksh calc() { total=$1+$2 echo "$total" diff=$2-$1 echo "$diff" } I have invoked this function as calc 5 8 Now i... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. UNIX for Dummies Questions & Answers

shell program- how many times a function is called

We have a program source C and is required to indicate how many times each function is called from the C program. also print the line number where there is a call. I've tried something like this: #!/bin/sh for i in $*;do if ! then echo $i is not a C file. else echo $i... (0 Replies)
Discussion started by: oana06
0 Replies

4. Programming

Implementing function outside struct

I have this code where I have declared a struct with some functions. Trying to write the function implementation outside the struct declaration and do not know how to proceed. #ifndef ParseEl_hh #define ParseEl_hh #include <iostream> #include <fstream> #include "DynBaseObj.hh"... (7 Replies)
Discussion started by: kristinu
7 Replies

5. UNIX for Dummies Questions & Answers

Snmp Disk Problem = Negative Values

Ok, so i monitor disk space on remote machines using snmp. Works great for me. But whenever a particular partition happens to have Terabytes of data, snmp starts reporting negative values. Can someone please tell me how to get around this problem? The AllocationUnit is 512 bytes. Weird... (0 Replies)
Discussion started by: SkySmart
0 Replies

6. UNIX for Dummies Questions & Answers

Extracting column if above certain values and repeated over a number of times continuously

Hi I am new to the forum and would like to ask: i have a file in form with thousands of column id.1 A01 A01 A68 A68 id.2 A5 A5 A3 A3 1001 0 0 0.136 0.136 1002 0 0 0.262 0.183 1003 0 0 0.662 0.662 1004 0 0 ... (9 Replies)
Discussion started by: newbeeuk
9 Replies

7. UNIX for Advanced & Expert Users

problem with netfilter hook function struct skbuff *sock is null..

iam trying to built a i have used netfilter for it. in function main_hook sock_buff is returning null and in my log file continuously "sock buff null" is printed plse help to solve this problem.. (using print_string iam printing strings on current terminal (terminal we ping)) ... (1 Reply)
Discussion started by: pavan6754
1 Replies

8. Shell Programming and Scripting

Call single function multiple times diff set of parameters

Okay, not sure if it can be done, I would think it could be done and I'm just having a hard time with it. fun_close_wait(){ ipVar="${1} ${2}" portVar=`cat "${5}" | cut -d' ' -f 1` for ip in $ipVar do for port in $portVar do netstatVar=`netstat -n | grep... (4 Replies)
Discussion started by: cbo0485
4 Replies

9. AIX

how would you know your server was rebooted 3 times or 5 times

Is there such location or command to know how many times did you reboot your server in that particular day?in AIX. (3 Replies)
Discussion started by: kenshinhimura
3 Replies

10. Shell Programming and Scripting

KSH problem - how do i redirect three times?

i need to output an ls command to a file but also capture any errors from that command and output them to a log file and the screen. if it's only possible to output them to a log file and not the screen then that's fine. this is what i've tried so far, but it won't populate log.txt. i've... (16 Replies)
Discussion started by: mjays
16 Replies

Featured Tech Videos