Running another program from C ++ with a given timeout

I am writing a program (implementing a genetic algorithm) that runs another program using a "system" method to calculate suitability. The problem is that another program sometimes freezes indefinitely. How can I execute a program with some time limit from C ++.

Both POSIX and C ++ solutions are welcome. And more or less, this will be done after the application, so the solution does not have to be very elegant.

I am running a Linux CentOS distribution and testing Cygwin. For the compiler, I am using gcc 4.1.2 with the boost library.

Any help would be appreciated

+3
source share
3 answers

system fork/exec . exec RLIMIT_CPU setrlimit .

, SIGXCPU ( ).

+6

(, ), kill ... , fork exec, , pid.

+6
  • "" , , , , . .

  • If this “other” program belongs to you or you have sources under an open license, but you do not want (or cannot) follow the suggestion above, it may be easier to fix the program to prevent freezing.

  • Shitty Method:

    • do fork (), remid pID, call exec * ("my-prog", ...)
    • create a thread in the main program with a timer.
    • when time fires, it kills the process using kill () and PID.
-2
source

All Articles