Programming |
Memory Mapping Demo
In this demonstration, two separate MATLAB processes 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.
The send Function
This function prompts you to enter a string and then, using memory mapping, passes the string to another instance of MATLAB that is running the answer
function.
Copy the send
and answer
functions to files send.m
and answer.m
in your current working directory. Begin the demonstration by calling send
with no inputs. Next, start a second MATLAB session on the same machine, and call the answer
function in this session. To exit, press Enter.
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 send string (or RETURN to end): ', 's'); len = length(str); if (len == 0) disp('Terminating SEND function.') break; end str = str(1:min(len, 255)); % Message limited to 255 chars. % 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
The answer Function
The 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 inputs.
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 till 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+m.data(1)))'; % Display the message. disp('Received message from SEND:') disp(msg) % Transform the message to all uppercase. m.data(2:1+m.data(1)) = upper(msg); % Signal to SEND that the response is ready. m.data(1) = 0; end
Running the Demo
Here is what the demonstration looks like when it is run. First, start two separate MATLAB sessions on the same computer system. Call the send
function in one and the answer
function in the other to create a map in each of the processes' memory to the common file:
% Run SEND in the first MATLAB session. send Enter send string (or RETURN to end): % Run ANSWER in the second MATLAB session. answer ANSWER server is awaiting message
Next, enter a message at the prompt displayed by the send
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 send
, 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 it:
% SEND writes a message and reads the uppercase reply. Hello. Is there anybody in there? response from ANSWER is: HELLO. IS THERE ANYBODY IN THERE? Enter send string (or RETURN to end): % ANSWER reads the message from SEND. Received message from SEND: Hello. Is there anybody in there?
send
writes a second message to the file. answer
reads it, put it into uppercase, and then writes the message to the file:
% SEND writes a second message to the shared file.
I received your reply.
response from ANSWER is:
I RECEIVED YOUR REPLY.
Enter send string (or RETURN to end): <Enter
>
Terminating SEND function.
% ANSWER reads the second message.
Received message from SEND:
I received your reply.
Deleting a Memory Map | Importing Text Data |
© 1994-2005 The MathWorks, Inc.