Hello,
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:
- Before i fork and create a child, i call the times function (in the parent process).
times(&start_tms);
- I create a pipe and i pass the times of start structure to the pipe when i'm in the child process.
- The child executes a simple "ls -l" command
- When the child finishes he execution, the father calls for the second time the times() function.
times(&end_tms);
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() {
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;
//call times before fork()!!!
times(&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(&start_tms);*/
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);
exit(0);
}
else
{
/* Parent process closes up output side of pipe */
close(fd[1]);
/* wait for the child */
if( (pid_waitpid = waitpid(childpid,NULL,0) ) == -1)
{
perror("waitpid");
exit(1);
}
/* call times for capturing end times */
times(&end_tms);
/* 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
./a.out
test
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 test.sh
-rwx------ 1 g_p g_p 81 Mar 12 2011 notepad.sh
-rwx------ 1 g_p g_p 81 Mar 12 2011 notepad2
-rwx------ 1 g_p g_p 420 Mar 12 2011 e1.sh
-rwx------ 1 g_p g_p 319 Mar 12 2011 e2.sh.save.3
-rwx------ 1 g_p g_p 300 Mar 12 2011 e3.sh.save.2
-rwx------ 1 g_p g_p 98 Mar 12 2011 e4.sh.save.1
-rwx------ 1 g_p g_p 74 Mar 12 2011 e5.sh.save
-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