Find the answer to your Linux question:
Results 1 to 8 of 8
This program provides the processor execution time of the program at the end of the program. #include <time.h> #include <stdio.h> int main(int argc, char *argv[]) { clock_t ticks; int i=0; ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    5

    Time based output-output for every 10 millisec

    This program provides the processor execution time of the program at the end of the program.

    #include <time.h>
    #include <stdio.h>

    int main(int argc, char *argv[]) {

    clock_t ticks;

    int i=0;

    while(i<50000)
    {
    printf("Work work %d\n", i);
    i++;
    ticks = clock();

    }



    printf("Used %0.2f seconds of CPU time. \n", (double)ticks/CLOCKS_PER_SEC);
    }

    I need the output(processor execution time) for every 10 milliseconds instead of getting the output at the end of the program.How to do it?.

    It helps me to draw the graph with time on x axis and processor utilisation on y axis.

    expecting ur replies folks

    regards
    Sai Viki

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    if your trying to get a loop to start and stop every 10 milliseconds

    try info nanosleep()

    ...Gerard4143

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    gerard4143, I suspect that he's in a compute-bound loop and wants to keep track of the compute time used during that loop. If he introduces calls to nanosleep(), all that does is to guarantee that each loop will use a certain amount of clock time. Instead, he wants to output compute (not clock) time used every time that compute time goes up by 10 milliseconds.

    vivasaayi, what you need to do is two things:
    1. move your final printf() statement to inside the loop; and
    2. every time you execute that printf() statement, store the value of ticks in another variable. Let's call that value printed_ticks. Then, when it comes time to print this again, don't do the actual print unless the new value exceeds the old one by 10 milliseconds. If it doesn't, then don't print it this time around, and don't update printed_ticks either.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Just Joined!
    Join Date
    Dec 2008
    Posts
    5

    wje_lf

    wje_lf and gerard,

    Hearty thanks for ur reply

    wje_lf,Ur response is close to my requirement.Let me put the question in more clear way.

    1.Suppose this program(loop) takes 15 milliseconds as processor time - the time it has used processor.

    2.suppose the total time taken by program to complete is 1000 milliseconds.Because the higher priority processes use the processor along with this program.

    so total time to finish the process = 1000 milliseconds

    total time it has actually spent on processor = 15 milliseconds

    The output for the current program shows the processor execution time for total runtime.

    But I need to print the time spent on the processor for every 100 milliseconds in that 1000 milliseconds of overall runtime.


    expecting ur replies folks

    Regards

    sai viki

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Now I'm completely confused as to what you need.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  6. #6
    Just Joined!
    Join Date
    Dec 2008
    Posts
    5
    oohhh...no!

    I will put it in simple terms.

    the program takes 1000 milliseconds to finish overall.


    the output should be like

    the cpu time used by the program for first 100 ms = 15 ms

    the cpu time used by the program for second 100ms = 20 ms

    the cpu time used by the program for the third 100 ms = 10 ms

    .
    .
    .
    the cpu time used by the program for the tenth 100ms = 2 ms

    so during the overall execution period of 1000 ms - the cpu time used by the program is (15+20+10+......+2).

    Is it clear now?

    actually now my program outputs the cpu time used during the whole execution period of the program...i need it like the way i have listed above

  7. #7
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Each time you go through the loop, call gettimeofday() to help you determine whether you've printed the amount of compute time within the most recent 100 milliseconds. If you have, do nothing more. But if you haven't, then store the new time of day as the time at which you printed the amount of compute time, use clock() to get the accumulated compute time, and subtract the previously-observed amount of compute time from that figure to get the amount of compute time to report for this interval.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  8. #8
    Just Joined!
    Join Date
    Dec 2008
    Posts
    5

    wje_lf

    thanks u so much

    Regards

    sai viki

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...