wait for several system calls to finish
이전 댓글 표시
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.
채택된 답변
추가 답변 (1개)
Mario Malic
2024년 4월 20일
0 개 추천
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
Mario Malic
2024년 4월 21일
편집: Mario Malic
2024년 4월 21일
It is not the most convenient, but you could use a parallel processing toolbox (if available) to run them in parallel. However, you would need to obtain information when the process is finished, through ps, as Walter mentioned. You would have to also have to relay that information to the main MATLAB client so it knows when to continue with execution.
And yes, you can use while loop to see if process with that particular ID exists with while loop, as you said.
Walter Roberson
2024년 4월 21일
Note: I am not certain that system() can be executed within a background pool; you might need the full Parallel Computing Toolbox
SA-W
2024년 4월 25일
Mario Malic
2024년 4월 25일
I don't know if the multistart in parallel is a good idea (I don't have experience with it).
Can you bruteforce your optimization? Generate all combinations for the optimization variable values and just run it. Then hit a local (or global) optimization algorithm from the best objective value.
Otherwise, I would suggest a surrogateopt if your program takes some time to run , and it's really good, compared to others, of course, it depends on the objective function.
SA-W
2024년 4월 26일
Walter Roberson
2024년 4월 26일
I cannot think of any way to get the information about the process exit status.
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.
카테고리
도움말 센터 및 File Exchange에서 Parallel Computing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!