<time.h> in program

This is a discussion on <time.h> in program within the Linux Programming forums, part of the Platform Specific Boards category; I am trying to find out how much time it takes to complete an execution of a program in Linux, ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    34

    <time.h> in program

    I am trying to find out how much time it takes to complete an execution of a program in Linux, by executing it from a new process. I am using the <time.h> time() function, but I get strange numbers pushed into the clockTimeA vector, but can't figure out why. Any suggestions as to what the problem may be? Thanks.

    Here is my code:
    Code:
    #include <ctime>
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <cmath>
    #include <sys/types.h>
    #include <unistd.h>
    
    using namespace std;
    
    int main()
    {
     pid_t pid;
    
     time_t start, finish; //time variables
    
     vector <double> clockTimeA;
     double sdA;
     double meanA = 0;
     double varianceA = 0;
    
     ofstream OUTPUTFILE;
     OUTPUTFILE.open("Runtime_Data_Fork_Linux_A.txt");
    
     pid = fork();
    
    for(int i = 0; i <= 1; i++)
    {
    
    if(pid == 0)
    
    { 
     start = time(NULL);
    
     //Process A
    
      ///////////////////////////////////////////////  
    
      execlp("/A", "A", NULL);
    }// end if loop
    
    if (pid > 0)
     {
      wait();
      cout <<"waiting for child process to finish" << "\n"; 
      //get finish time for process A
    
      finish = time(NULL);
    
     cout << "A started at: " << " " << start << " " << "and finished at: " << finish << " " << "\n";
    
     clockTimeA.push_back((double) (finish-start) ) ;
     cout << "1st time is: " << clockTimeA[0] << "\n" ;
     }
    
    } // end 30 times loop
      
    
    if(pid > 0)
    {
      for(int i = 0; i < clockTimeA.size(); i++)
      {
       OUTPUTFILE << ((double) (finish-start)) << "\n";
       meanA += clockTimeA[i]/2; 
      }
    
      OUTPUTFILE << "\n" << "Mean" << "\n" << meanA << "\n" << "\n";
    
     
      for(int i = 0; i < clockTimeA.size(); i++)
      {
       varianceA += (pow((clockTimeA[i] - meanA), 2))/2 ;
      }
    
      sdA = sqrt(varianceA);
      OUTPUTFILE << "StdDev" << "\n" << sdA <<  endl;
    
    }
     return 0;
    
    }
    Last edited by Bnchs400; 10-23-2007 at 05:49 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    start is only updated in the child process, it remains uninitialised in the parent process.

    Remember, immediately after the fork(), you have TWO copies of the program running. Changing variables in one of them does not affect the other.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    32
    Why not just use
    time your_program

    ?

    Valery

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    15
    If u really need this kind of thing, you can create pipe(write/read values from that pipe) and use that for proper values, but that will involved extra overhead.

    Regards,
    Amit Sahrawat

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21