1

I need to specify that a program ends after N seconds. I assume that would mean enclosing the entire program in some sort of do while loop, but I can't figure out the specifics. How would this be done?

For a very simple example, lets say I want it to run the line

printf("Hello world\n");

For N seconds

Bob
  • 715
  • 2
  • 11
  • 32
  • it would depend on the code. Can you show us yours? – Jean-François Fabre Oct 01 '16 at 20:08
  • 1
    Why would the code itself matter? It could be as simple as printing "Hello World" for N seconds. – Bob Oct 01 '16 at 20:11
  • if the program is reading a 2Tb file, that's different. – Jean-François Fabre Oct 01 '16 at 20:13
  • Which platform are you on? Is it POSIX-like enough to have [`alarm()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/alarm.html)? If so, then `int main(void) { int N = 3; alarm(N); while (1) printf"Hello world\n"); return 0; }` — the return will never be executed because you didn't set a signal handler for alarm signals. – Jonathan Leffler Oct 01 '16 at 20:25
  • @JonathanLeffler alarm() is a really nice solution if available. On Windows, there appear to be altenatives http://stackoverflow.com/questions/4511732/alarm-function-on-linux-and-windows-cant-find-a-equivalent-for-windows-c – jforberg Oct 01 '16 at 20:36
  • Also consider clarifying whether it should end "at some moment soon after `N` seconds", or be abruptly "killed after precisely `N` seconds". It's usually a better idea to *avoid* killing threads and processes, so, if possible, try to design your program to finish gracefully. – vgru Oct 01 '16 at 20:44
  • GNU provides a `timeout` command in the Core Utils package. – Jonathan Leffler Oct 01 '16 at 20:45

1 Answers1

2

This stupid code prints hello world during at least 3 seconds

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

int main()
{

    time_t start_timer = time(NULL);
    while(1)
    {
        printf("Hello world\n");
        if (time(NULL)-start_timer > 3)
        {
            break;
        }
    }
    return 0;
}

Of course, if instead of just printing a message, the program performed a big computation / input/output operation / network operation, it could take longer than 3 seconds.

In that case, as a pertinent comment suggested, it would be better (but more complex, so it has to be necessary) to create a "watching" thread and check time lapse in parallel of the computation (using a cpu-passive time wait mechanism to avoid eating 100% CPU), killing the computing thread if taking too long.

(note that if a big I/O operation is in progress, the thread kill could take a while because the thread is not active at this time)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • This is a common pattern and would usually be in a separate "watchdog" thread which can kill the main thread if it's taking too long. This is very useful in e.g. function tests which could sometimes hang. – jforberg Oct 01 '16 at 20:33
  • @jforberg true. But it a watchdog, you would have to insert some "sleep" or your watchdog would eat 100% of your CPU. – Jean-François Fabre Oct 01 '16 at 20:35
  • Yes, naturally. But sleep isn't standard C and stack overflow tends to eat you up if you suggest non cross-platform solutions under the "C" tag... – jforberg Oct 01 '16 at 20:39
  • @jforberg oh! almost walked on a landmine here :) I took that into account in my edit. – Jean-François Fabre Oct 01 '16 at 20:41
  • 1
    "*sleep isn't standard C*": With C11 we have `thrd_sleep()`. – alk Oct 02 '16 at 08:52