I am trying to display the amount of time that it took for a command to run. I'm assuming that i have the correct code:
...
else
{
printf("I am a child process and my pid is %d\n", getpid());
cout<<"Parameters are: "<<endl;
for (int j=0; j<i; j++)
cout<<doit[j]<<endl;
cout<<" EXECUTE CHILD"<<endl;
initial=time(&initial);
execve(doit[0], doit, NULL);
final=time(&final);
exit(0);
}
cout<<" KILL CHILD AND RETURN TO PARENT"<<endl;
printf("I am still the parent process and my pid is %d\n",
getpid());
//float elapsed=final-initial;
printf("It took %u seconds to execute that command.\n",
(final-initial));
...
and in my output, i keep getting "It took you 34 seconds to execute...." and I KNOW that isnt right. I have tried using %U, %S, %ld in place of %u and it either doesnt work or give me some off the wall answer.
including <sys/time.h> with initial and final as type time_t, I cant just declare initial=time(). I have to have arguments so &initial serves the purpose. I'm still getting crazy output for the number of seconds though *shrug* Oh well..I'll just keep playing with it by putting final=time(&final); in various spots before the execve command.
By using time() we are getting total elapsed time between the start and finish of the program. If we switch to clock(), we get the cpu time used. For some programs these will be nearly the same thing. But put a:
sleep(60);
in a program and now there is a big difference. For 60 seconds the process will be sleeping waiting a signal. Those 60 seconds will not show up with clock() since the cpu is not being used. They will show up with time().
And clock() also counts cpu time used by child processes that have exited and been reaped via wait(). In a multiprocessor environment the clock() time could exceed the time() time.