Time Problem

Hi friends.I have to write a program which accepts one command line argument,executes the arguments as shell command,determines the time taken by it and prints the time values.I have written a program but it is showing some errors so please tell me where i have gone wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<time.h>
#include<sys/times.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
  int i;
   struct tms,start,end;
   clock_t t1,t2;
    static long clk_tck;
     if(argc<2)
     {
        printf("Enter the parameter list\n");
        exit(0);
     }
    clktck=sysconf(_SC_CLK_TCK);
     start=times(&t1);
      system(argv[1]);
      end=times(&t2);
       printf("Start Time:%f\n",start);
       printf("End Time :%f\n",end);
       printf("Diffrence:%f\n",((t2.tms_utime-t1.tms_utime)/clktck));
}


Errors
Line18 & 20:Passing argument 1 of times() from incompatible pointer type
Line23:Request for member tms_utime in something not a structure or union
You are using t1 and t2 when you should use start and end, and vice versa.
Hello thanks for the reply.Are my lines 21 and 22 are correct?.Should i put t1 and t2 as start time and end time respectively.
Yes use t1 and t2 on line 21-22.
Now if i use a command like cal it gets executed too quickly.So all the values are 0.00.Can u tell me a command which takes a bit more time to execute so that i can see the readings

./a.out cal
<cal output>
start:0.00000
end:0.000000
difference:0.0000
I don't know such a command without additional flags. Why don't you just write a program that takes long time to execute yourself?
I tried to execute one of the shell scripts i wrote but i says command not found.How can i make external commands work
If the executable file of the program you want to time is named foo you run it as ./a.out ./foo
Last edited on
ya but the script is in /home/<username>/ .....if i try like this it still says command not found
Doesn't it work if you specify the whole path? ./a.out /home/<username>/ ... /foo
the file has .sh extension....it does not work if i keep or remove the extension
Output
./a.out /home/<username>/1.sh
sh: ./home/<username>/1.sh: No such file
peter i made a mistake by not putting a dot before root.Now it locates but it says permission denied.Plz tell me how to setup execute permisiion for that file.I know we have to use chmod but i don't know the parameters.
Topic archived. No new replies allowed.