In same program we can calculate function execution time but How to calculate other program (c program) execution time in another program . i tried using execve() and clock() and system() but i didn't get program execution time .
example :
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
#include<time.h>
main(int argc, char *argv[])
{
pid_t child;
pid_t tpid;
int status;
char *envp[] = {NULL};
char *newargv[] = {argv[1],argv[2],argv[3],NULL};
clock_t start, end;
double time_taken;
child = fork();
if(child == 0)
{
printf("in child\n");
start = clock();
execve(argv[1], newargv,envp);
end = clock();
time_taken = ((double)(end - start))/CLOCKS_PER_SEC;
printf("fun() took %f seconds to execute \n", time_taken);
exit(1);
}
if(child != 0)
{
/* This is run by the parent. Wait for the child
to terminate. */
// end = clock();
// time_taken = ((double)(end - start))/CLOCKS_PER_SEC;
// printf("fun() took %f seconds to execute \n", time_taken);
printf(" in parent\n");
do {
pid_t tpid;
tpid = wait(&status);
// if(tpid != child) process_terminated(tpid);
}while(tpid != child);
//return status;
}
}
execfamily of function only returns if there's an error. If there is no error then they never returns.fork. And the end time is after thewaitin the parent.