How can I kill a system()-spawned process if it takes too long to finish?

조회 수: 5 (최근 30일)
Roman Müller-Hainbach
Roman Müller-Hainbach 2016년 1월 10일
댓글: Walter Roberson 2016년 1월 11일
I am using the system()-command to utilize external programs (command line tools) that compute stuff for me. Usually they finish rather quickly. Sometimes though these programs take way too long, hang up or do something else that prevents them from exiting. Therefor MATLAB waits forever.
I need some kind of timer-driven solution to kill some processes if they don't exit fast enough.
I tried setting up a timer with a start delay which I start right before the system()-command. In its callback-function I determine the correct PID and issue a kill-command again through system(). The problem though is that the timer doesn't seem to fire if MATLAB is waiting for a system()-command to finish.
Is there a way to allow timer callbacks (or whatever) to fire and execute while MATLAB is waiting for system()-command?
Update: To clarify my goals: I'm calling an external program many times inside a loop using system(). I need to be able to know when the process terminated on its own and to terminate it myself if it runs longer than a certain time. Unfortunately I don't know of any ways to get signals from processes that run asynchronously beside MATLAB. If I was able to start a process asynchronously and interact with it, sending and receiving signals, that would be ideal.
Sincerely,
Roman
  댓글 수: 3
Roman Müller-Hainbach
Roman Müller-Hainbach 2016년 1월 11일
function run
command = 'top'; % an example of a command that doesn't exit on its own
t = timer('TimerFcn',{@callback,command}, 'StartDelay',1);
start(t);
system(command);
stop(t);
delete(t);
end
function callback(~,~,command)
[~,cmdout] = system(['top -l 1 | grep' command]);
if ~isempty(cmdout)
cmdout = strsplit(cmdout,'\n');
cmdout = cmdout(~cellfun(@isempty,cmdout));
PIDs = cellfun(@(x) (x(1:find(isspace(x),1)-1)), cmdout, 'un',0);
cellfun(@(x) (system(['kill ' x])), PIDs);
warning('Had to kill PIDs %s.', strjoin(PIDs,','));
end
end
Roman Müller-Hainbach
Roman Müller-Hainbach 2016년 1월 11일
I am aware of the asynchronous execution method by appending '&'. However this would make my problem worse, wouldn't it? I need to be able to monitor multiple processes at the same time. Each with its own starting time. I'm open for ideas though.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2016년 1월 11일
As you are using Linux or OS-X, you may be able to take advantage of popen() as that will start the process asynchronously; closing the fid returned should end the process.
It might need a bit of work to be able to determine that the child is closed the descriptor (because it exited) without doing an I/O operation that might end up waiting for input.
  댓글 수: 2
Roman Müller-Hainbach
Roman Müller-Hainbach 2016년 1월 11일
Thanks for your suggestion!
I will look into that function and how I can use it to achieve what I wanted. In the long run I will need something platform-independent, though.
Walter Roberson
Walter Roberson 2016년 1월 11일
top and kill are not available on MS Windows; you would need to use taskmanager
The code for popen() that is given there might not immediately work on MS Windows, but Windows XP SP1 and later are supposedly POSIX compliant and so should have popen() available as a call. If they do not then you can use _popen()
It might be easier to push it all down into a bit of POSIX C or C++ code. It appears your only reason to be working asychronously at that point is to detect that your task has gone on too long.
You could look at http://courses.cs.vt.edu/~cs5565/spring2012/projects/project1/posix-timers.c which has the benefit of being already written, but it is overkill for your purposes, as the elementary checks for I/O are signal safe... and besides those POSIX checks provide for built-in timeout without needing a timer.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by