The following code does what you need:
/*
Compilacao: gcc tempo.c -o tempo
Execucao: tempo <comando>
onde <comando> pode ser ls por exemplo
Sugestao de Modificacoes:
1. Retire a chamada wait(NULL) e observe o comportamento
2. Faca com que cada processo imprima o seu pid
o pid 'e obtido chamando-se getpid()
*/
#include <stdio.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/types.h>
main(int argc, char *argv[])
{
int pid;
struct timeval tv1, tv2;
double t1, t2;
if(argc != 2)
{
printf("Uso: tempo algum_comando\n");
exit(1);
}
pid = fork(); /* fork */
if(pid == 0) {
printf("\nProcesso filho vai executar %s agora.\n",argv[1]);
gettimeofday(&tv1, NULL); /* processo pai continua ... */
t1 = (double)(tv1.tv_sec) + (double)(tv1.tv_usec)/ 1000000.00;
system(argv[1]); /* processo filho executa comando */
gettimeofday(&tv2, NULL);
t2 = (double)(tv2.tv_sec) + (double)(tv2.tv_usec)/ 1000000.00;
printf("\nExecucao de %s terminou agora.\n",argv[1]);
printf("\nO tempo de execucao de %s por filho foi: %lf\n",
argv[1], (t2 - t1));
}
else{
gettimeofday(&tv1, NULL); /* processo pai continua ... */
t1 = (double)(tv1.tv_sec) + (double)(tv1.tv_usec)/ 1000000.00;
/* wait(NULL); */ /* sincroniza com o termino do filho */
gettimeofday(&tv2, NULL);
t2 = (double)(tv2.tv_sec) + (double)(tv2.tv_usec)/ 1000000.00;
printf("\nO tempo de execucao de processo pai foi: %lf\n",(t2 - t1));
}
}
Example-based: http://www.dca.fee.unicamp.br/~lotufo/cursos/EA877/sistemas-operativos/exemplos/tempo. c
If the community thinks it’s a duplicate of this question, can close. But I understand that there are two different questions. Mainly the calculation of the execution time of a program, since if the program itself is responsible for calculating its own execution time, it will influence the final result, leaving the result inaccurate.
– Avelino
Do you want to know the complexity of the algorithm or the actual time it takes? The question seems to be too generic. I don’t know what to say.
– Maniero
Sorry, but I can’t distinguish between the two terms: "Algorithm complexity" and "effective time". : S
– Avelino
Algorithm complexity is only the function itself, and the effective time would be the total time of the program (including process creation, RAM allocation, etc.)?
– Avelino
I thought you knew, after all you used a tag with the term. http://answall.com/q/33319/101. Effective time is the measured time.
– Maniero
No no, I’m still studying... (Thanks for the link). I want to measure the effective time, the total time of the program.
– Avelino
As you yourself mentioned, I understand you are duplicate of this, also yours: http://answall.com/questions/55087/ - I know you are asking for two different approaches, but in the end the answer is the same: you take the system time at the beginning and end of the program, and calculate the difference. It is negligible to capture this time, as long as you do only once at the beginning and once at the end (without putting extra code inside loops).
– Bacco
I found what I needed here. Imagine a large program, which takes time to load into memory. The loading time will no longer be negligible. I was unhappy in my question, I should have asked the time of the process. Sorry for the confusion. The program I put in the answer serves what I need. Feel free to modify my answer.
– Avelino