-1

I use the following to compile my C++ Program.

g++ filename.cpp -o filename.o

Then I take an input file and direct the output to the another file.

filename.o < in.txt > out.txt

However sometimes the code goes into an infinite loop and I am not able to stop it (I force quit the .o from Activity Monitor).

What is want it to put an execution limit time on the program so that after it it automatically stops executing. Is there any way to this?

Something like:- g++ filename.cpp -o filename.o [execution_limit_here=10.0s]

Edit:- I am creating an Atom Package and users will compile and execute their code for a particular set of input files, using a simple key combination.

So I can’t predict beforehand if the code will go into an infinite loop.

dreamboat
  • 159
  • 1
  • 3
  • 16
  • 2
    Possible duplicate of [Command line command to auto-kill a command after a certain amount of time](https://stackoverflow.com/questions/601543/command-line-command-to-auto-kill-a-command-after-a-certain-amount-of-time) – hlt May 19 '18 at 13:33
  • 1
    **TL;DR**: check out GNU coreutils' [`timeout`](https://linux.die.net/man/1/timeout) utility (also on OS X via Homebrew) – hlt May 19 '18 at 13:34
  • 1
    There's a significant difference between slow and infinite! I'd suggest you eliminate the latter as a logical possibility. – Brett Hale May 19 '18 at 14:27

3 Answers3

3
g++ filename.cpp -o filename.o

Very bad taste (even if it could work). By convention *.o files are object files, not executables, but in your compilation command, the fileno.o is the executable output (i.e. produced by g++) by compilation and linking. Read about invoking GCC.

I guess you are on Linux or POSIX (e.g. MacOSX, Android...)

I recommend

  g++ -Wall -Wextra -g filename.cpp -o filename

In that command, we ask for all warnings and debug info, and the executable is named filename (without a confusing .o suffix).

Standard C++ does not know about time limits, but POSIX (so Linux and MacOSX) does, and I recommend using setrlimit(2) with RLIMIT_CPU. That should be coded in the application (you could call setrlimit early in your main).

Otherwise, code a shell like program (or use some shell script with ulimit) to run your executable with some CPU limit and/or use timeout(1).

See also time(7) to understand the various notions of time.

But you really should debug your program (using the gdb debugger to understand its behavior) to always avoid infinite loops. Perhaps you could use some timer facilities (e.g. setitimer(2) and catch the SIGALRM signal(7); read also signal-safety(7)) and/or query in your loops the elapsed real or CPU time (and check it against some threshold).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thanks for the answer. Yes I am using MacOS. Could you please explain why `g++ filename.cpp -o filename.o` is bad taste? And how second command is better? Also I am creating an Atom Package(see my edit) so GNU `coreutils` will get the job done for now. – dreamboat May 19 '18 at 15:50
  • 1
    I did explain that `.o` is the conventional suffix for object files. – Basile Starynkevitch May 20 '18 at 07:59
1

First of all I'd recommend putting printouts on your program so you can figure out why it's getting stuck in an infinite loop, and if you only want to have a time limit you could set a starting time with clock() (from ctime) and after each iteration compare the current time with the starting time point.

Victor
  • 32
  • 6
1

You could use timeout command while running the executable command

timeout 5 ./a.out 

This command will only run the given command (in this case executable) for 5 seconds