How can I kill a system process which is frozen

22 views (last 30 days)
I'm working on some genetic algorithm optimisation code which interfaces with Xfoil for the analysis, using a system() command to run Xfoil. Sometimes Xfoil freezes (maybe due to a "bad" aerofoil shape), and this halts the entire optimisation process. I've added a simple timer based "kill exe" callback function, which is designed to kill the Xfoil exe run after a certain amount of time - this should give any "good" run a chance to finish, and should close any "bad" run. However, the callback function is not able to be called while Xfoil is running. It appears that even though the timer delay has ended and the TimerFcn() is triggered, Matlab cannot process the "kill exe" callback function while a system process is running.
So basically I can't kill the frozen system process. Anyone have any way around this?

Accepted Answer

Mohammad Sami
Mohammad Sami on 29 Aug 2020
Edited: per isakson on 2 Apr 2021
You can use .Net System.Diagnostic.Process to run it. This way you will also be able to terminate the program.
process = System.Diagnostics.Process();
process.StartInfo.FileName = 'java.exe';
process.StartInfo.Arguments = 'some args';
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = false;
process.StartInfo.CreateNoWindow = false;
process.Start();
processisrunning = ~process.HasExited;
You can use the following command to kill the exe.
process.Kill();
  2 Comments
Michael Maurice
Michael Maurice on 30 Aug 2020
Thanks for your help.
The issue I'm running into here is I don't know how to work with the input and output files using the .Net format. With the original the system() code I'm using:
cmd = sprintf('cd %s && xfoil.exe < xfoil.inp > xfoil.out',wd);
[status,result] = system(cmd);
I'm not sure how to replicate "< xfoil.inp > xfoil.out" in the .Net format.
Walter Roberson
Walter Roberson on 30 Aug 2020
https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.redirectstandardoutput?view=netcore-3.1
https://stackoverflow.com/questions/16256587/redirecting-output-to-the-text-file-c-sharp
In order to redirect from a file you need to set up the process to use the command shell and then you use the typical command line.
More recommended is to not use a shell and to say that you want to redirect standard input and standard output, and then access the streams and write the appropriate content to the stream or read it from the stream. You can get buffer-fulls of content as they are generated -- including being able to set up callbacks when the stream is ready for more input or when more output is available. You can then also tie in a timer callback to know when the process has gone silent.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!