Calling Subsidiary Mathematica Processes
| LinkLaunch["prog"] | start an external program and open a connection to it |
Connecting to a subsidiary program via MathLink.
This starts a subsidiary
Mathematica process on the computer system used here.
| Out[1]= |  |
Here is a packet representing the first input prompt from the subsidiary
Mathematica process.
| Out[2]= |  |
This writes a packet representing text to enter in the subsidiary
Mathematica process.
Here is a packet representing the output prompt from the subsidiary
Mathematica process.
| Out[4]= |  |
And here is the actual result from the computation.
| Out[5]= |  |
The basic way that the various different objects involved in a
Mathematica session are kept organized is by using
MathLink packets. A
MathLink packet is simply an expression with a definite head that indicates its role or meaning.
Basic packets used in Mathematica sessions.
The fact that
LinkRead returns an
InputNamePacket indicates that the subsidiary
Mathematica is now ready for new input.
| Out[6]= |  |
This enters two
Print commands as input.
Here is the text from the first
Print.
| Out[8]= |  |
And here is the text from the second
Print.
| Out[9]= |  |
| Out[10]= |  |
Some additional packets generated in Mathematica sessions.
If you enter input to
Mathematica using
EnterTextPacket
, then
Mathematica will automatically generate a string version of your output, and will respond with
ReturnTextPacket
. But if you instead enter input using
EnterExpressionPacket[expr] then
Mathematica will respond with
ReturnExpressionPacket[expr] and will not turn your output into a string.
Packets for representing input and output lines using expressions.
This enters an expression into the subsidiary
Mathematica session without evaluating it.
Here are the next 3 packets that come back from the subsidiary
Mathematica session.
| Out[12]= |  |
InputNamePacket and
OutputNamePacket packets are often convenient for making it possible to tell the current state of a subsidiary
Mathematica session. But you can suppress the generation of these packets by calling the subsidiary
Mathematica session with a string such as

.
Even if you suppress the explicit generation of
InputNamePacket and
OutputNamePacket packets,
Mathematica will still process any input that you give with
EnterTextPacket or
EnterExpressionPacket as if you were entering an input line. This means for example that
Mathematica will call
$Pre and
$Post, and will assign values to
In[$Line] and
Out[$Line].
Evaluating expressions without explicit input and output lines.
| Out[14]= |  |
The first packet to come back is a
TextPacket representing text generated by the
Print.
| Out[16]= |  |
After that, the actual result of the
Print is returned.
| Out[17]= |  |
In most cases, it is reasonable to assume that sending an
EvaluatePacket to
Mathematica will simply cause
Mathematica to do a computation and to return various other packets, ending with a
ReturnPacket. However, if the computation involves a function like
Input, then
Mathematica will have to request additional input before it can proceed with the computation.
This sends a packet whose evaluation involves an
Input function.
What comes back is an
InputPacket which indicates that further input is required.
| Out[19]= |  |
There is nothing more to be read on the link at this point.
| Out[20]= |  |
| Out[22]= |  |
| LinkInterrupt[link] | send an interrupt to a MathLink-compatible program |
Interrupting a MathLink-compatible program.
This sends a very time-consuming calculation to the subsidiary process.
The calculation is still going on.
| Out[24]= |  |
Now the subsidiary process has stopped, and is sending back an interrupt menu.
| Out[26]= |  |