This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / MathLink and External Program Communication  /

2.12.8 Special Topic: Communication with Mathematica Front Ends

The Mathematica kernel uses MathLink to communicate with Mathematica front ends. If you start a Mathematica kernel from within a front end, therefore, the kernel will be controlled through a MathLink connection to this front end.


The link to the front end for a particular kernel.

The global variable $ParentLink specifies the MathLink connection that a particular kernel will use for input and output.
It is sometimes useful to reset $ParentLink in the middle of a Mathematica session, thereby effectively changing the front end to which the kernel is connected.

Session A


  • This creates a link on port 6000.
  • In[1]:= link = LinkCreate["6000"]

    LinkOpen::linkf: LinkOpen[6000, LinkMode -> Listen] failed.

    Out[1]=

    Session B


  • This connects to the link opened in session A.
  • In[2]:= LinkConnect["6000"]

    Out[1]=

  • This tells session B that it should use session A as a front end.
  • In[2]:= $ParentLink = %

    LinkObject::linkn: Argument LinkObject[6000@frog.wolfram.com, 4, 4] in LinkObject[6000@frog.wolfram.com, 4, 4] has an invalid LinkObject number; the link may be dead.

    Out[2]=

    Session A


  • Session A now acts as a front end to session B and gets all output from it.
  • In[3]:= Table[LinkRead[link], 4]

    LinkObject::linkv: Argument $Failed in LinkRead[$Failed] is not a valid LinkObject.

    LinkObject::linkv: Argument $Failed in LinkRead[$Failed] is not a valid LinkObject.

    LinkObject::linkv: Argument $Failed in LinkRead[$Failed] is not a valid LinkObject.

    General::stop: Further output of LinkObject::linkv will be suppressed during this calculation.

    Out[2]=

  • This releases session B again.
  • In[3]:= LinkWrite[link, EnterTextPacket["$ParentLink=."]]

    LinkObject::linkv: Argument $Failed in LinkWrite[$Failed, EnterTextPacket[$ParentLink=.]] is not a valid LinkObject.

    Out[3]=

    Much like the Mathematica kernel, the standard notebook front end for Mathematica is set up to handle a certain set of MathLink packets.
    Usually it is best to use functions like NotebookWrite and FrontEndExecute if you want to control the Mathematica front end from the kernel. But in some cases you may find it convenient to send packets directly to the front end using LinkWrite.