How can I kill a system process which is frozen

조회 수: 8 (최근 30일)
Michael Maurice
Michael Maurice 2020년 8월 28일
편집: per isakson 2021년 4월 2일
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