467,879 Members | 1,309 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,879 developers. It's quick & easy.

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!!

Nov 15 '05 #1
  • viewed: 1126
Share:
1 Reply
In article <11*********************@g47g2000cwa.googlegroups. com>,
<fr********@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
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Huzefa | last post: by
17 posts views Thread by EkteGjetost | last post: by
1 post views Thread by Rahul | last post: by
7 posts views Thread by Gasten | last post: by
15 posts views Thread by Jay | last post: by
9 posts views Thread by weidongtom | last post: by
1 post views Thread by mshroom12 | last post: by
reply views Thread by jack112 | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.