着实搞不明白,为什么wait4()已经有返回值了,而rusage里的时间却还是0呢
实在搞不明白,为什么wait4()已经有返回值了,而rusage里的时间却还是0呢?
实际上做的是一个检测进程运行时间的程序,本想将所检测的进程创建到子进程,调用wait4()系统函数来返回进程所用的资源情况,可是始终没有接受到,不知道怎么回事
------解决方案--------------------
子进程执行exec函数后,进程的用户空间代码和数据完全被替换,所以exec后的代码不会被执行。
楼主要确保子进程的推出,因为case 0:中的exit(0)不会执行。
------解决方案--------------------
long tv_usec=100;
long tv_sec=0;
int usedtime=tv_sec*1000+tv_usec/1000;
printf(%d",usedtime);
output:0
实际上做的是一个检测进程运行时间的程序,本想将所检测的进程创建到子进程,调用wait4()系统函数来返回进程所用的资源情况,可是始终没有接受到,不知道怎么回事
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/resource.h>
void runme(){
if(freopen("out.txt","w",stdout)==NULL)
printf("redirecting error");
if(freopen("in.txt","r",stdin)==NULL)
printf("redirecting error");
execl("./normal","./normal",(char *)NULL);
int i;
for(i=0;i<10000000000;i++) (void)getppid();
fclose(stdout);
fclose(stdin);
//retuen 0;
}
int main(){
int status;
struct rusage ruse;
int usedtime=0;
pid_t tpid;
switch(tpid = fork()){
case -1:printf("open pid error\n");exit(-1);
case 0:
printf("open child\n");
runme();
exit(0);
default:
printf("father, child pid is %d \n", tpid);
printf("waited child's pid is %d \n",wait4(tpid, &status, WUNTRACED, &ruse));
usedtime += (ruse.ru_utime.tv_sec * 1000 + ruse.ru_utime.tv_usec / 1000);
usedtime += (ruse.ru_stime.tv_sec * 1000 + ruse.ru_stime.tv_usec / 1000);
}
printf("%d\n%d\n%d\n%d\n",ruse.ru_utime.tv_sec,ruse.ru_utime.tv_usec,ruse.ru_stime.tv_sec,ruse.ru_stime.tv_usec);
printf("total time is %d ms", usedtime);
return 0;
}
------解决方案--------------------
子进程执行exec函数后,进程的用户空间代码和数据完全被替换,所以exec后的代码不会被执行。
楼主要确保子进程的推出,因为case 0:中的exit(0)不会执行。
------解决方案--------------------
long tv_usec=100;
long tv_sec=0;
int usedtime=tv_sec*1000+tv_usec/1000;
printf(%d",usedtime);
output:0