1

I've been wanting a pause function for a while and I found this. Being a beginner in C, I can't be for sure but it looks like functions from <clock.h>. I would like to implement this into my code, but not without understanding it.

void wait(int seconds){
    clock_t start, end;
    start = clock();
    end = clock();
    while (((end-start) / CLOCKS_PER_SEC) = !seconds)
        end = clock();
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 1
    Moreover if for some reason, even if it's unlikely, (a bg process with high priority) preempts the loop at a bad time, `(end-start) / CLOCKS_PER_SEC` may never be **equal** to `seconds`. `((end-start) / CLOCKS_PER_SEC) < seconds` would be better. – Déjà vu Feb 04 '16 at 09:03
  • 7
    I hope the actual code uses `!= seconds` instead of `= !seconds`. – Frerich Raabe Feb 04 '16 at 09:04
  • Can you apply the code to yours is depending your waiting object. If your waiting is always less 10 ms, you can; otherwise, using `sleep()`. – Holsety Feb 04 '16 at 09:07
  • Note: If your program freezes for more than a second, and at the right moment, then this will end up looping forever. That's probably rare, but it can happen, e.g. if paging from a failing hard disk. (I experienced lockups upwards of a minute once when I my hard disk's connector was loose and intermittently losing connection) – user253751 Feb 04 '16 at 09:39
  • OT: `wait()` is a bad name as there already exists a system call named `wait()` (http://man7.org/linux/man-pages/man2/wait.2.html). – alk Feb 04 '16 at 15:54

4 Answers4

5

It's just a busy-wait loop, which is a very nasty way of implementing a delay, because it pegs the CPU at 100% while doing nothing. Use sleep() instead:

#include <unistd.h>

void wait(int seconds)
{
    sleep(seconds);
}

Also note that the code given in the question is buggy:

while (((end-start) / CLOCKS_PER_SEC) = !seconds)

should be:

while (((end-start) / CLOCKS_PER_SEC) != seconds)

or better still:

while (((end-start) / CLOCKS_PER_SEC) < seconds)

(but as mentioned above, you shouldn't even be using this code anyway).

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 4
    I'd just use sleep directly – BigMike Feb 04 '16 at 09:06
  • @BigMike: you might well do that, but note that having a wrapper function means that you can add platform-specific implementations later if needed (e.g. `Sleep()` on Windows). Also the purpose of the answer was to replace the innards of the OP's `wait()` function with something better. – Paul R Feb 04 '16 at 09:10
  • 3
    You're 100% right, as usual it depends on what OP really needs, still when not strictly need I prefer to avoid wrappers and use good old preprocessor directives. I have been through way too many wrappers' hells in 20 years... (2 to n level of indirection to finally call a library function with the very same signature of the top level function - just adding useless logging and extra checks). – BigMike Feb 04 '16 at 09:20
1

Generally, clock_t is a structure in library. It returns the number of clock ticks elapsed from the program initiation till end or till you want to count the time.

If you want to know more you can read details here: http://www.tutorialspoint.com/c_standard_library/c_function_clock.htm

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
1

The clock() function returns the number of system clock cycles that have occurred since the program began execution. Here is the prototype: clock_t clock(void);

Note that, the return value is not in seconds. To convert the result into seconds, you have to divide the return value by CLOCKS_PER_SEC macro. You program just does this. Functionally it stops the program execution for seconds seconds (you pass this value as an argument to the wait function).

By the way, it uses time.h, not clock.h. And it is not the right point to start learning C.

To learn more: http://www.cplusplus.com/reference/ctime/?kw=time.h

Shuvo Sarker
  • 187
  • 9
1

clock function in <ctime>:

Returns the processor time consumed by the program.

The value returned is expressed in clock ticks, which are units of time of a constant but system-specific length (with a relation of CLOCKS_PER_SEC clock ticks per second).

reference

So, basically it returns number of processor ticks passed since the start of the program. While the processor tick is the number of processor instructions executed by a process, it does not account for IO time and any such that does not use CPU.

CLOCKS_PER_SEC Is the average number of CPU ticks executed by machine and varies from machine to machine, even it (probably) changes time to time, because doing too much IO will cause overall decrease for each process CLOCKS_PER_SEC because more time will be spent not using CPU.

Also this statement: (end-start) / CLOCKS_PER_SEC) = !seconds is not correct, because the right implementation is

while (((end-start) / CLOCKS_PER_SEC) != seconds)
    end = clock();

Does the trick of busy waiting, program will be trapped inside this while loop until seconds seconds will be passed using CPU clocks and CLOCKS_PER_SEC to determine time passed.

Although I would suggest changing it to:

while (((end-start) / CLOCKS_PER_SEC) < seconds)
    end = clock();

Because if process has low priority, or computer is too busy handling many processes chance is one CPU tick can take more than one second (probably when system is crashed, for some buggy program who take up a lot of resources and has high enough priority to cause CPU starvation).

Finally, I do not recommend using it, because you are still using CPU while waiting which can be avoided by using sleep tools discussed here

Community
  • 1
  • 1
8bra1nz
  • 717
  • 7
  • 19