How to stop a "system()" call after prefixed CPU time amount

26 views (last 30 days)
Hi, I would like to stop the execution of a simulation operated by an external function called in matlab by command "system()" when exceeding a maximum amount of CPU elapsed time, is it possible with matlab? Thank you

Accepted Answer

Walter Roberson
Walter Roberson on 15 Mar 2016
You would have to have added "&" to the end of the command line in order to have the process generated and the command to return immediately afterwards. You would not be able to get the output of the run, so it would have to be something that wrote to files.
With the background process going, to stop the process after a particular amount of CPU time, you would need to use your operating system task monitoring facilities in order to locate the process identifier. With the process identifier, you would use operating system tools figure out how much CPU time the process had used. When it used too much, you would use operating system facilities to kill the process. For OS-X and Linux, you would use the "kill" command to terminate a process; for MS Windows you would use taskkill
There is a different approach that can be used with OS-X and Linux. If someone on the command line that you system, before the actual command is started, you use ulimit -t you can limit the amount of CPU time allowed for the commands that follow it. For example,
result = system('ulimit -t 120; ./run_my_simulation');
  2 Comments
Andrea Manno
Andrea Manno on 15 Mar 2016
Thank you for the answer, I tried "ulimit -t" on Linux but it did not work well, however I solved with command "timeout" (see http://www.commandlinefu.com/commands/view/3592/execute-a-command-with-a-timeout). On MS Windows I could not solve the problem with taskkill (I am not very practiced on windows systems), could you please be more precise with an example? Thank you again

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!