Help !I want to write a program to count the running time of another program

F

freehomesp

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

Walter Roberson

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top