我是新来的。我一直在努力获得执行给定二进制文件的时间(可能是也可能不是我自己开发/编译的。我从here得到了这个getTime()
函数!。
我写了一个小程序来调用另一个程序并确定执行第二个程序所花费的时间,但是我还没有获得预期的结果。该程序几乎立即退出,输出的时间非常短(即使我执行的程序时间非常紧张)
这是我写的程序。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <string.h>
#include <sys/types.h> // for pid_t
#include <sys/wait.h> // for wait
double getTime (void);
char** argsCollect(int total, int from, char* source[]);
char* newString(int len);
void freeCollected(char **collection, int len);
static double Load_time;
int main(int ac, char *av[]){
char **my_args = argsCollect(ac, 2, av);
Load_time = getTime ();
pid_t pid = fork();
if (pid==0) { // child process
execv(av[1], my_args);
exit(127); // only if execv fails
}
else { /* pid!=0; parent process */
int finished = waitpid(pid,0,0); // wait for child to exit
Load_time = getTime() - Load_time;
printf("Finished child: %d, Tiempo: %f\n", finished, Load_time);
freeCollected(my_args, (ac - 1));
}
return 0;
}
double getTime (void){
double usertime, systime;
struct rusage usage;
getrusage (RUSAGE_SELF, &usage);
usertime = (double) usage.ru_utime.tv_sec +
(double) usage.ru_utime.tv_usec / 1000000.0;
systime = (double) usage.ru_stime.tv_sec +
(double) usage.ru_stime.tv_usec / 1000000.0;
return (usertime + systime);
}
char** argsCollect(int total, int from, char* source[]){
int collect = total - from;
int i;
char** mem_dest = (char**)malloc((collect) * sizeof(char*));
for(i = from; i < total; i++){
mem_dest[i-from] = newString(strlen(source[i]));
strcpy(mem_dest[i-from], source[i]);
}
return mem_dest;
}
char* newString(int len){
char *new_string = (char*)malloc(len * sizeof(char));
return new_string;
}
void freeCollected(char **collection, int len){
int i;
for(i = 0; i < len; i++){
free(collection[i]);
}
free(collection);
}
我用这段代码测试了getTime()
函数
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
double getTime (void);
static double Load_time;
int main()
{
int i;
int max = 1000000000;
int j = 0;
Load_time = getTime ();
for(i = 0; i < max; i++){ // This is
j++; // just to
} // do something
Load_time = getTime () - Load_time;
printf("time: %.2f, j: %d\n", Load_time, j);
return 0;
}
double getTime (void){
double usertime, systime;
struct rusage usage;
getrusage (RUSAGE_SELF, &usage);
usertime = (double) usage.ru_utime.tv_sec +
(double) usage.ru_utime.tv_usec / 1000000.0;
systime = (double) usage.ru_stime.tv_sec +
(double) usage.ru_stime.tv_usec / 1000000.0;
return (usertime + systime);
}
我真的很感激如何正确地开展这项工作以及我做错了什么。 提前谢谢大家。
答案 0 :(得分:0)
记录。我是如何在两周后解决这个问题的。
我将double getTime(void)
函数修改为double getTime(int who)
函数,该函数在以下对最初代码段的修改中进行了描述。
新来源
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <string.h>
#include <sys/types.h> // for pid_t
#include <sys/wait.h> // for wait
double getTime (int who);
static double Load_time;
int main(int ac, char *av[]){
int status;
Load_time = getTime(RUSAGE_SELF);
pid_t pid = fork();
if (pid==0) { // child process
execv(av[1], &av[2]);
exit(127); // only if execv fails
}
else { /* pid!=0; parent process */
int finished = waitpid(pid,&status,0); // wait for child to exit
Load_time = getTime(RUSAGE_CHILDREN) - Load_time;
printf("Finished child: %d, Tiempo: %f\n", finished, Load_time);
}
return 0;
}
double getTime (int who){
double usertime, systime;
struct rusage usage;
getrusage (who, &usage);
usertime = (double) usage.ru_utime.tv_sec +
(double) usage.ru_utime.tv_usec / 1000000.0;
systime = (double) usage.ru_stime.tv_sec +
(double) usage.ru_stime.tv_usec / 1000000.0;
return (usertime + systime);
}
使用该参数,该函数可以告诉子进程使用的总时间。请注意,使用RUSAGE_CHILDREN调用时,struct rusage usage
结构中的时间将是由父进程正确等待的父级(包括大子级)生成的每个子级的时间。