How can I kill a system process which is frozen

8 次查看(过去 30 天)
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?

采纳的回答

Mohammad Sami
Mohammad Sami 2020-8-29
编辑:per isakson 2021-4-2
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 个评论
Michael Maurice
Michael Maurice 2020-8-30
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 2020-8-30
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Agriculture 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by