实在搞不明白,为什么wait4()已经有返回值了,而rusage里的时间却还是0呢?

zhoude5 2014-07-15 09:36:59
实际上做的是一个检测进程运行时间的程序,本想将所检测的进程创建到子进程,调用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;
}
...全文
211 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
long tv_usec=100; long tv_sec=0; int usedtime=tv_sec*1000+tv_usec/1000; printf(%d",usedtime); output:0
zhoude5 2014-07-17
  • 打赏
  • 举报
回复
引用 8 楼 micropentium6 的回复:
=100; long tv_sec=0; int usedtime=tv_sec*1000+tv_usec/1000; printf(%d",usedtime); output:0
之前还有一个问题就是无论如何long tv_usec等的值都是零,这是不科学的,于是拿到同学电脑上试了,结果有值,于是决定自己的系统有问题,重装之后运行也有值了。 此问题都完结了,感谢您的耐心帮助,同时也仍感谢KangRoger
zhoude5 2014-07-17
  • 打赏
  • 举报
回复
引用 8 楼 micropentium6 的回复:
long tv_usec=100; long tv_sec=0; int usedtime=tv_sec*1000+tv_usec/1000; printf(%d",usedtime); output:0
我貌似明白了,是因为tv_usec/1000是取整,所以就是零了吧
zhoude5 2014-07-16
  • 打赏
  • 举报
回复
引用 6 楼 micropentium6 的回复:
struct timeval { time_t tv_sec; /* seconds */ long tv_usec; /* microseconds */ }; 1 microsecond=1.0e-6 second executing for(i=0;i<10000;i++) on a modern PC, the following statements will return 0 for sure coz they are wrong... 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); so the correct statements should be: usedtime += (ruse.ru_utime.tv_sec * 1000 + ruse.ru_utime.tv_usec); usedtime += (ruse.ru_stime.tv_sec * 1000 + ruse.ru_stime.tv_usec); note that the ru_stime will be 0 since there is almost no system cpu usage here at all...
I think you misunderstand my meaning. You have already knew that 1 microsecond=1.0e-6 second, and 1 milliseconds=1.0e-3 second. But I just want to get the milliseconds data. So I think there is no wrong in the statements. And I use this statement to print every variable in struct timeval.
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);
But the answers are still zero. That's what I didn't understand. Thank you every much for your help all the same. Is there something we didn't noticed? I have struggled one day in this problem... And forgive my English.
  • 打赏
  • 举报
回复
引用 2 楼 KangRoger 的回复:
子进程执行exec函数后,进程的用户空间代码和数据完全被替换,所以exec后的代码不会被执行。 楼主要确保子进程的推出,因为case 0:中的exit(0)不会执行。
Bingo! replacing function execl with function system could be the alternative...
  • 打赏
  • 举报
回复
struct timeval { time_t tv_sec; /* seconds */ long tv_usec; /* microseconds */ }; 1 microsecond=1.0e-6 second executing for(i=0;i<10000;i++) on a modern PC, the following statements will return 0 for sure coz they are wrong... 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); so the correct statements should be: usedtime += (ruse.ru_utime.tv_sec * 1000 + ruse.ru_utime.tv_usec); usedtime += (ruse.ru_stime.tv_sec * 1000 + ruse.ru_stime.tv_usec); note that the ru_stime will be 0 since there is almost no system cpu usage here at all...
KangRoger 2014-07-16
  • 打赏
  • 举报
回复
子进程执行exec函数后,进程的用户空间代码和数据完全被替换,所以exec后的代码不会被执行。 楼主要确保子进程的推出,因为case 0:中的exit(0)不会执行。
zhoude5 2014-07-16
  • 打赏
  • 举报
回复
最新发现,就算我不要runme()这个函数,而随便写一个for(i=0;i<10000;i++) (void)getppid();在子进程里面,还是老问题,wait4已经返回了正确的pid,而ruse里面的时间值还是0
zhoude5 2014-07-16
  • 打赏
  • 举报
回复
这是修改后的,结果还是0 啊
#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);
	system("./normal");
	//int i;
	//for(i=0;i<10000000000;i++) (void)getppid();
	fclose(stdout);
	fclose(stdin);
	exit(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;
	}
zhoude5 2014-07-15
  • 打赏
  • 举报
回复
哦,忘了说execl("./normal","./normal",(char *)NULL);运行的是一个a+b=c的简单程序,只不过用freopen重新定向输入输出流到文件了

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧