필터 지우기
필터 지우기

wait for several system calls to finish

조회 수: 10 (최근 30일)
SA-W
SA-W 2024년 4월 20일
댓글: Mario Malic 2024년 4월 27일
I am calling three external programs and can only continue in the matlab program after all programs are finished. Each of them work independently of each other and can start at the same time
[staus1,~] = system('cmd1 &');
[staus2,~] = system('cmd2 &');
[staus3,~] = system('cmd3 &');
% how to check if all of them are finished?
Using ampersand & symbol, I reached that they start nearly at the same time, but how can I check when they are all finished? In my case, the returned status variables can be zero (successful) or negative (not successful). Hence, checking status for any value does not help, I guess.

채택된 답변

Walter Roberson
Walter Roberson 2024년 4월 20일
You have to use taskmgr (Windows) or ps (MacOS or Linux) to detect whether the processes are still alive.
Note: If you are using Windows then I recommend that you instead use System.Diagnostics.Process which is able to start processes independently, and allows their status to be checked.
  댓글 수: 3
Walter Roberson
Walter Roberson 2024년 4월 21일
You could use
ps | grep matlab | tail
and parse out the process ID. Then you could start the new process, and then
ps -g PROCESS_ID_GOES_HERE
to get the process ID of the new process.
Then you could use the java watcher https://www.mathworks.com/matlabcentral/answers/398152-listening-to-the-creation-deletion-of-a-file-with-specific-name#answer_317920 on /proc/PROCESS_ID_OF_NEW_PROCESS watching for deletion
... something like that
SA-W
SA-W 2024년 4월 25일
@Walter Roberson As for this solution: I found a way to extract the process id's of interest. Also, watchting /proc/PROCESS_ID for deletion is doable. However, it is not clear to me how I can extract the exit status of my program. When I do
[status,~] = system('myprog')
When I start the system commands in background (appending &), we have status=0, but this is not the real exit status of my program which is the integer returned in the main function of myprog.
Any idea how I can get this?

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

추가 답변 (1개)

Mario Malic
Mario Malic 2024년 4월 20일
Ampersand means that program will be started and MATLAB will not wait for it to be terminated and will continue to execute next lines of code.
What does it mean that they are finished? Does the program exits when it's done?
You can remove ampersand, then you have to close the program for MATLAB to execute next lines.
  댓글 수: 8
Walter Roberson
Walter Roberson 2024년 4월 26일
I cannot think of any way to get the information about the process exit status.
Mario Malic
Mario Malic 2024년 4월 27일
If I understood correctly, multistart does that already, it has multiple starting points. By running multistart in parallel, each of these runs may lead to the minimum that already has been found by some other multistart process.
If you are looking for a any (tedious) solution, you could write a file from your c++ code indicating the exit status and read it with MATLAB once process is deleted.

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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by