wait for several system calls to finish
Show older comments
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.
Accepted Answer
More Answers (1)
Mario Malic
on 20 Apr 2024
0 votes
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 Comments
Mario Malic
on 21 Apr 2024
Edited: Mario Malic
on 21 Apr 2024
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
on 21 Apr 2024
Note: I am not certain that system() can be executed within a background pool; you might need the full Parallel Computing Toolbox
SA-W
on 25 Apr 2024
Mario Malic
on 25 Apr 2024
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
on 26 Apr 2024
Walter Roberson
on 26 Apr 2024
I cannot think of any way to get the information about the process exit status.
Mario Malic
on 27 Apr 2024
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.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!