3

I'm writing a program (A genetic algorithm implementation) which executes another program using "system" method to calculate fitness. The problem is that another program sometimes hangs for unlimited amount of time. How can I execute a program with some time limit from C++.

Both POSIX and c++ solutions are appreciated. And more or less this will be run once application so solution doesn't have to be very elegant.

I'm running Linux CentOS distribution and am testing on Cygwin. For compiler I use gcc 4.1.2 with boost library.

Any help is apreciated

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
Sergej Andrejev
  • 9,091
  • 11
  • 71
  • 108

3 Answers3

6

Instead of system, execute the program with the fork/exec idiom. Before the exec, set RLIMIT_CPU to a maximum value with setrlimit in the child.

Make sure the child does not ignore SIGXCPU (which is very unlikely).

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Can you explain more about making sure SIGXCPU is not ignored? – Sergej Andrejev Mar 14 '11 at 12:35
  • @Sergej: most signals by default result in the program being killed, but the programmer can decide to catch or ignore a signal explicitly with `sigaction`. I've never seen any program that catches or ignores `SIGXCPU` and I'm not sure if it's possible, but it's wise to run the program and perform a `kill -XCPU` command on it just in case. – Fred Foo Mar 14 '11 at 12:48
  • This measures CPU time, but according to my understanding of the question, Sergej is looking for a limit on wall-clock time. – Ben Voigt Mar 14 '11 at 12:53
  • @Ben: you're right, this works if the program keeps using CPU time needlessly. If it hangs waiting, Errata's solution is better. – Fred Foo Mar 14 '11 at 12:59
  • My second program hangs. It uses CPU but it will never produce any result and thus should be killed after a while. What happens (based on my subjective measures is that presolver from GLPK library used in that second program has a bug and can enter infinite loop sometimes. So what is the best approach in this situation? – Sergej Andrejev Mar 14 '11 at 14:37
  • @Sergej: you can use either method. Just realise that wall clock time != CPU time unless the program constantly uses 100% CPU. (Also realise that GLPK is designed to solve the NP-complete problem of mixed-integer linear programming, so it may well be working fine.) – Fred Foo Mar 14 '11 at 14:44
  • I realize that, but constraints to the problem are generated randomly to some extent and some perturbations are working fine but some are not. It may be also related to some computer settings (maybe my thinkpad applying some power saving options when process runs overnight. I don't know and it's quite hard to find the reason. Anyway thanks for info. – Sergej Andrejev Mar 14 '11 at 17:55
6

You could create a timer (with boost timer for example) and then try to kill the child process... this assume that you use fork and exec to launch all your child, and you stored each pid.

Errata
  • 640
  • 6
  • 10
-2
  1. If this 'another' program is yours or you have sources under public license it's better probably to make it not a separate program but a separate thread in the main program. In this case you can easily control it.

  2. If this 'another' program is yours or you have sources under public license but don't want (or can't) follow the suggestion above may be it is easier to fix the program to prevent hanging.

  3. Shitty method:

    • do fork(), remeber PID, call exec*("my-prog", ...)
    • create thread in the main program with timer.
    • when time fires kill the process using kill() and PID remembered.
maverik
  • 5,508
  • 3
  • 35
  • 55
  • 2
    Other processes can be killed, but terminating a thread leaves the whole process in an inconsistent state. – Ben Voigt Mar 14 '11 at 12:51