Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Help !I want to write a program to count the running time of another program (http://www.velocityreviews.com/forums/t439057-help-i-want-to-write-a-program-to-count-the-running-time-of-another-program.html)

freehomesp@yahoo.com.cn 08-12-2005 06:01 AM

Help !I want to write a program to count the running time of another program
 
1 #include <sys/types.h>
2 #include <sys/times.h>
3 int
4 main(int argc,char* argv[])
5 {
6 struct tms before, after;
7
8 times(&before);
9
10 pid_t pid;
11 if((pid=fork())<0)
12 printf("fork() error\n");
13 else if(pid==0){
14 if(execv("%s",argv[1],&argv[2])<0)
15 printf("error occurs when exec the
file\n");
16 }
17
18 times(&after);
19
20 printf("User time: %ld seconds\n", after.tms_utime -
21 before.tms_utime);
22 printf("System time: %ld seconds\n", after.tms_stime -
23 before.tms_stime);
24
25 exit(0);
26 }
But it doesn't work properly,please help me!!


Walter Roberson 08-12-2005 06:13 AM

Re: Help !I want to write a program to count the running time of another program
 
In article <1123826479.332553.35980@g47g2000cwa.googlegroups. com>,
<freehomesp@yahoo.com.cn> wrote:

The only mechanism portable C has to run another program is
the system() library call.


> 1 #include <sys/types.h>
> 2 #include <sys/times.h>
> 3 int
> 4 main(int argc,char* argv[])
> 5 {
> 6 struct tms before, after;
> 7
> 8 times(&before);
> 9
> 10 pid_t pid;
> 11 if((pid=fork())<0)
> 12 printf("fork() error\n");
> 13 else if(pid==0){
> 14 if(execv("%s",argv[1],&argv[2])<0)
> 15 printf("error occurs when exec the
>file\n");
> 16 }
> 17
> 18 times(&after);


fork() and kin are off-topic in comp.lang.c -- try comp.unix.programmer .
hint: read the manual page for wait()
--
"I will speculate that [...] applications [...] could actually see a
performance boost for most users by going dual-core [...] because it
is running the adware and spyware that [...] are otherwise slowing
down the single CPU that user has today" -- Herb Sutter


All times are GMT. The time now is 07:35 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.