Thread: Approximate number of clock ticks per second

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    40

    Approximate number of clock ticks per second

    I have a homework problem that I'm working on tonight that I don't have the faintest idea how to approach! Please help! The problem says:

    Write a program that uses library functions "sleep" and "times" to determine the approximate number of clock ticks per second.

    I appreciate the help!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Clock ticks per second means process clock ticks?
    Basically what you need to do is measure how much time elapses when it sleeps for a second.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    Okay so I use a line like sleep(1000) to stop the process for 1 second. How do I use the "times" function to measure the time that elapses?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are several depending on what you need. C has a function called clock.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    I looked at that one but the problem is pretty specific. I need to use a library function called times. I looked at the man page but I am SO new to linux and C it isn't funny. More help?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is no function called times in C. Of Linux API I know nothing since I don't use Linux.
    In that case you'll need to wait until someone who has the knowledge pops up or if you can find the function on your own.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    times() will give you the time used by the processor.
    http://www.hmug.org/man/3/times.php

    sleep() will allow you to specify time that the process sleeps.

    I'm not entirely sure how you combine those to get the tick-length tho'.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by black_spot1984 View Post
    I have a homework problem that I'm working on tonight that I don't have the faintest idea how to approach! Please help! The problem says:

    Write a program that uses library functions "sleep" and "times" to determine the approximate number of clock ticks per second.

    I appreciate the help!
    Pretty simple -- times() fills in a structure that looks like this:

    Code:
    struct tms {
                  clock_t tms_utime;  /* user time */
                  clock_t tms_stime;  /* system time */
                  clock_t tms_cutime; /* user time of children */
                  clock_t tms_cstime; /* system time of children */
    };
    These numbers are in units of "ticks." The total time spent is the sum of tms_utime and tms_stime. Divide this by a number in seconds, and you get ticks per second.

    So I would suggest calling times() to get a start value. Then call sleep(10) (or some other amount), then call times() again. Take the difference in the ticks, and divide by how many seconds you slept.

    This seems kind of fishy though -- do tms_utime and tms_stime really increment during sleeping? I would have thought not, but presumably the instructor knows something I don't.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    tms_utime and tms_stime should not increment during a sleep.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program giving errors
    By andy bee in forum C Programming
    Replies: 5
    Last Post: 08-11-2010, 10:38 PM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  5. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM