我正在尝试如何在 linux 环境中使用 c 中的 getrusage 函数,并且想知道我是否走在正确的轨道上。
我编写了一个小程序,以确保在应用到我的项目之前了解 getrusage 的工作原理。我想分别获取父进程和子进程的用户/内核时间。
void stupidFunction();
void childSort();
int main(void)
{
pid_t parent_pid=getpid();
struct rusage parent_before_function_usage;
getrusage(RUSAGE_SELF,&parent_before_function_usage);
time_t parent_before_function_user_usage_sec=parent_before_function_usage.ru_utime.tv_sec;
time_t parent_before_function_user_usage_microsec=parent_before_function_usage.ru_utime.tv_usec;
time_t parent_before_function_cpu_usage_sec =parent_before_function_usage.ru_stime.tv_sec;
time_t parent_before_function_cpu_usage_microsecsec =parent_before_function_usage.ru_stime.tv_usec;
stupidFunction();
pid_t pid;
if((pid = fork()) <0)
{
fprintf(stderr,"Failed to create a fork process\n");
}
else if (pid == 0)
{
childSort();
}
int status;
waitpid(-1,&status,0);
printf("in parent\n");
struct rusage parent_after_function_usage;
getrusage(RUSAGE_SELF,&parent_after_function_usage);
time_t parent_after_function_user_usage_sec=parent_after_function_usage.ru_utime.tv_sec;
time_t parent_after_function_user_usage_microsec=parent_after_function_usage.ru_utime.tv_usec;
time_t parent_after_function_cpu_usage_sec =parent_after_function_usage.ru_stime.tv_sec;
time_t parent_after_function_cpu_usage_microsecsec =parent_after_function_usage.ru_stime.tv_usec;
time_t parent_real_user_usage_sec=parent_after_function_user_usage_sec - parent_before_function_user_usage_sec;
time_t parent_real_user_usage_microsec= parent_after_function_user_usage_microsec - parent_before_function_user_usage_microsec;
time_t parent_real_cpu_usage_sec=parent_after_function_cpu_usage_sec - parent_before_function_cpu_usage_sec;
time_t parent_real_cpu_usage_microsec = parent_after_function_cpu_usage_microsecsec - parent_before_function_cpu_usage_microsecsec;
printf("User mode CPU time for parent: %d seconds, %d microseconds\n",parent_real_user_usage_sec,parent_real_user_usage_microsec);
printf("Kern mode CPU time for parent: %d seconds, %d microseconds\n",parent_real_cpu_usage_sec,parent_real_cpu_usage_microsec);
struct rusage child_function_usage;
getrusage(RUSAGE_CHILDREN,&child_function_usage);
time_t all_children_user_usage_sec=child_function_usage.ru_utime.tv_sec;
time_t all_children_user_usage_microsec=child_function_usage.ru_utime.tv_usec;
time_t all_children_cpu_usage_sec =child_function_usage.ru_stime.tv_sec;
time_t all_children_cpu_usage_microsec =child_function_usage.ru_stime.tv_usec;
printf("User mode CPU time for all children: %d seconds, %d microseconds\n",all_children_user_usage_sec,all_children_user_usage_microsec);
printf("Kern mode CPU time for all children: %d seconds, %d microseconds\n",all_children_cpu_usage_sec,all_children_cpu_usage_microsec);
return 0;
}
void stupidFunction()
{
int i=0;
while(i<900000000)
{
// printf("%d\n",i);
i+=1;
}
}
void childSort()
{
printf("in childSort\n");
int fd[2];
fd[0]=open("file1", O_RDONLY,0777);
fd[1]=open("file2", O_WRONLY,0777);
dup2(fd[0],0);
dup2(fd[1],1);
char* execArgs[2];
execArgs[0]="sort";
execArgs[1]=NULL;
execvp(execArgs[0],execArgs);
}
请提供一些关于代码正确性的反馈,如果我有很多孩子而不是一个孩子,这个代码会返回所有孩子的组合使用吗?