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

Discussion in 'C Programming' started by freehomesp@yahoo.com.cn, Aug 12, 2005.

  1. Guest

    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!!
    , Aug 12, 2005
    #1
    1. Advertising

  2. In article <>,
    <> 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
    Walter Roberson, Aug 12, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    1
    Views:
    8,998
    Gordon Beaton
    Oct 3, 2003
  2. flamesrock
    Replies:
    8
    Views:
    430
    Hendrik van Rooyen
    Nov 24, 2006
  3. sahil
    Replies:
    4
    Views:
    2,403
    Jerry Coffin
    May 28, 2008
  4. efelnavarro09
    Replies:
    2
    Views:
    899
    efelnavarro09
    Jan 26, 2011
  5. Dave Griffith

    Image::Magick->Write() doesn't want to write

    Dave Griffith, Nov 16, 2009, in forum: Perl Misc
    Replies:
    2
    Views:
    217
    Dave Griffith
    Nov 16, 2009
Loading...

Share This Page