MATLAB Answers

When I make a system call from matlab in Linux, does it use a different thread?

9 views (last 30 days)
Omer Moussaffi
Omer Moussaffi on 3 Oct 2017
Commented: dpb on 3 Oct 2017
Suppose I move a couple of files using a system call, as in:
unix('mv file1 new_path/ &');
unix('mv file2 new_path/ &');
It seems as if [rarely] file2 is being moved prior to file1. Is it possible? Could it be that the system calls are being transferred to different threads?
I'm using Matlab MCR on Amazon AWS (Centos Linux server).

Answers (2)

Jan
Jan on 3 Oct 2017
unix cmd & processed the cmd in the background. The operating system is responsible then to evaluate the commands to move the files. As you have notices already, this can happen in a different order, because the two commands are different processes. It is not like you open one shell and evaluate one command after the other, but like opening two shells with one command per shell. Then the order is not exactly defined also.
  3 Comments
Jan
Jan on 3 Oct 2017
@Omer: Yes. Using this
unix('mv file1 new_path/ &');
unix('mv file2 new_path/ &');
is the method to start two independent processes. This is the way to control this already. If Matlab should wait until the first is finished before starting the second:
unix('mv file1 new_path/');
unix('mv file2 new_path/'); % Or with appended &

Sign in to comment.


dpb
dpb on 3 Oct 2017
(On reflection, I think my subsequent comment actually answers the question pretty-much in toto so I'll put it here instead --dpb)
It ( unix ) still starts a new process only without the ampersand the calling process waits for the spawned process to complete before continuing...with the ampersand, the caller returns immediately after starting the process and continues, thus starting the second process irrespective of the status of the other.
The unix (or system or dos, they're all interchangeable) command in Matlab maps into a system API that has as a parameter the behavior of whether to wait or not; the ampersand is the Matlab syntax used in making that call internally as to whether to set the flag or not. The spawned process is still a separate thread either way; nothing is different there, only whether your code will or will not wait for the first to complete before going on to the next.
It is not possible to schedule the two processes unalterably in order if the ampersand is used with the interface as TMW has implemented it in Matlab.
You could achieve that effect by embedding the commands in a batch file and submitting that to the OS instead of the two commands individually; then they would run sequentially in the same process.
  2 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!