This example shows how to implement two separate MATLAB® processes that communicate with each other by writing and reading from a shared file. They share the file by mapping part of their memory space to a common location in the file. A write operation to the memory map belonging to the first process can be read from the map belonging to the second, and vice versa.
One MATLAB process (running
send.m) writes a message to the
file via its memory map. It also writes the length of the message to byte 1 in the
file, which serves as a means of notifying the other process that a message is
available. The second process (running
answer.m) monitors byte 1
and, upon seeing it set, displays the received message, puts it into uppercase, and
echoes the message back to the sender.
Prior to running the example, copy the
answer functions to files
answer.m in your current working directory.
This function prompts you to enter text and then, using memory-mapping, passes
the text to another instance of MATLAB that is running the
function send % Interactively send a message to ANSWER using memmapfile class. filename = fullfile(tempdir, 'talk_answer.dat'); % Create the communications file if it is not already there. if ~exist(filename, 'file') [f, msg] = fopen(filename, 'wb'); if f ~= -1 fwrite(f, zeros(1,256), 'uint8'); fclose(f); else error('MATLAB:demo:send:cannotOpenFile', ... 'Cannot open file "%s": %s.', filename, msg); end end % Memory map the file. m = memmapfile(filename, 'Writable', true, 'Format', 'uint8'); while true % Set first byte to zero, indicating a message is not % yet ready. m.Data(1) = 0; str = input('Enter text (or RETURN to end): ', 's'); len = length(str); if (len == 0) disp('Terminating SEND function.') break; end % Warn if the message is longer than 255 characters. if len > 255 warning('ml:ml','SEND input will be truncated to 255 characters.'); end str = str(1:min(len,255)); % Limit message to 255 characters. len = length(str); % Update len if str has been truncated. % Update the file via the memory map. m.Data(2:len+1) = str; m.Data(1)=len; % Wait until the first byte is set back to zero, % indicating that a response is available. while (m.Data(1) ~= 0) pause(.25); end % Display the response. disp('response from ANSWER is:') disp(char(m.Data(2:len+1))') end
answer function starts a server that, using
memory-mapping, watches for a message from
send. When the
message is received,
answer replaces the message with an
uppercase version of it, and sends this new message back to
send. To use
answer, call it with no
function answer % Respond to SEND using memmapfile class. disp('ANSWER server is awaiting message'); filename = fullfile(tempdir, 'talk_answer.dat'); % Create the communications file if it is not already there. if ~exist(filename, 'file') [f, msg] = fopen(filename, 'wb'); if f ~= -1 fwrite(f, zeros(1,256), 'uint8'); fclose(f); else error('MATLAB:demo:answer:cannotOpenFile', ... 'Cannot open file "%s": %s.', filename, msg); end end % Memory map the file. m = memmapfile(filename, 'Writable', true, 'Format', 'uint8'); while true % Wait until the first byte is not zero. while m.Data(1) == 0 pause(.25); end % The first byte now contains the length of the message. % Get it from m. msg = char(m.Data(2:1+double(m.Data(1))))'; % Display the message. disp('Received message from SEND:') disp(msg) % Transform the message to all uppercase. m.Data(2:1+double(m.Data(1))) = upper(msg); % Signal to SEND that the response is ready. m.Data(1) = 0; end
To see what the example looks like when it is run, first, start two separate
MATLAB sessions on the same computer system. Call the
send function with no inputs in one MATLAB session. Call the
answer function in the other
session, to create a map in each of the processes' memory to the common
send in the first MATLAB session.
Enter text (or RETURN to end):
answer in the second MATLAB session.
ANSWER server is awaiting message
Next, enter a message at the prompt displayed by the
function. MATLAB writes the message to the shared file. The second MATLAB session, running the
answer function, loops on
byte 1 of the shared file and, when the byte is written by
answer reads the message from
the file via its memory map. The
answer function then puts
the message into uppercase and writes it back to the file, and
send (waiting for a reply) reads the message and displays
send writes a message and reads the uppercase reply.
Hello. Is there anybody out there?
response from ANSWER is: HELLO. IS THERE ANYBODY OUT THERE? Enter text (or RETURN to end):
answer reads the message from
Received message from SEND: Hello. Is there anybody out there?
Enter a second message at the prompt display by the
send writes the second message to the file.
I received your reply.
response from ANSWER is: I RECEIVED YOUR REPLY. Enter text (or RETURN to end):
answer reads the second message, put it into uppercase, and
then writes the message to the file.
Received message from SEND: I received your reply.
In the first instance of MATLAB, press Enter to exit the example.
Terminating SEND function.