Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 7ebd25ac536d248d499a3ce2acda963a > files > 4136

Macaulay2-1.3.1-8.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8" ?>  <!-- for emacs: -*- coding: utf-8 -*- -->
<!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"	 "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>communicating with programs</title>
<link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/>
</head>
<body>
<table class="buttons">
  <tr>
    <td><div><a href="_using_spsockets.html">next</a> | <a href="_temporary__File__Name.html">previous</a> | <a href="_using_spsockets.html">forward</a> | <a href="_file_spmanipulation.html">backward</a> | <a href="___The_sp__Macaulay2_splanguage.html">up</a> | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div>

    </td>
  </tr>
</table>
<div><a href="index.html" title="">Macaulay2Doc</a> > <a href="___The_sp__Macaulay2_splanguage.html" title="">The Macaulay2 language</a> > <a href="_communicating_spwith_spprograms.html" title="">communicating with programs</a></div>
<hr/>
<div><h1>communicating with programs</h1>
<div>The most naive way to interact with another program is simply to run it, let it communicate directly with the user, and wait for it to finish.  This is done with the <a href="_run.html" title="run an external command">run</a> command.<table class="examples"><tr><td><pre>i1 : run "uname -a"
Linux x86-11.phx2.fedoraproject.org 2.6.32-71.14.1.el6.x86_64 #1 SMP Wed Jan 5 17:01:01 EST 2011 i686 i686 i386 GNU/Linux

o1 = 0</pre>
</td></tr>
</table>
To run a program and provide it with input, one way is use the operator <a href="__lt_lt.html" title="a binary operator (file output, ...)">&lt;&lt;</a>, with a file name whose first character is an exclamation point; the rest of the file name will be taken as the command to run, as in the following example.<table class="examples"><tr><td><pre>i2 : "!grep a" &lt;&lt; " ba \n bc \n ad \n ef \n" &lt;&lt; close
 ba 
 ad 

o2 = !grep a

o2 : File</pre>
</td></tr>
</table>
More often, one wants to write Macaulay2 code to obtain and manipulate the output from the other program.  If the program requires no input data, then we can use <a href="_get.html" title="get the contents of a file">get</a> with a file name whose first character is an exclamation point.  In the following example, we also peek at the string to see whether it includes a newline character.<table class="examples"><tr><td><pre>i3 : peek get "!uname -a"

o3 = "Linux x86-11.phx2.fedoraproject.org 2.6.32-71.14.1.el6.x86_64 #1 SMP
     "
     Wed Jan 5 17:01:01 EST 2011 i686 i686 i386 GNU/Linux</pre>
</td></tr>
</table>
Bidirectional communication with a program is also possible.  We use <a href="_open__In__Out.html" title="open an input output file">openInOut</a> to create a file that serves as a bidirectional connection to a program.  That file is called an input output file.  In this example we open a connection to the unix utility <tt>egrep</tt> and use it to locate the symbol names in Macaulay2 that begin with <tt>in</tt>.<table class="examples"><tr><td><pre>i4 : f = openInOut "!egrep '^in'"

o4 = !egrep '^in'

o4 : File</pre>
</td></tr>
<tr><td><pre>i5 : scan(keys Core.Dictionary, key -> f &lt;&lt; key &lt;&lt; endl)</pre>
</td></tr>
<tr><td><pre>i6 : f &lt;&lt; closeOut

o6 = !egrep '^in'

o6 : File</pre>
</td></tr>
<tr><td><pre>i7 : get f

o7 = interpreterDepth
     infoHelp
     integrate
     inducedMap
     installPackage
     installMethod
     insert
     instance
     info
     intersect
     inversePermutation
     input
     index
     indexComponents
     installAssignmentMethod
     infinity
     inducesWellDefinedMap
     inverse
     indices
     incomparable
     installHilbertFunction
     indeterminate
     in
     instances
     independentSets</pre>
</td></tr>
</table>
With this form of bidirectional communication there is always a danger of blocking, because the buffers associated with the communication channels (pipes) typically hold only 4096 bytes.  In this example we succeeded because the entire output from <tt>egrep</tt> was smaller than 4096 bytes.  In general, one should be careful to arrange things so that the two programs take turns using the communication channel, so that when one is writing data, the other is reading it.<p/>
A useful function in this connection is <a href="_is__Ready_lp__File_rp.html" title="whether a file has data available for reading">isReady</a>, which will tell you whether an input file has any input available for reading, or whether it has arrived at the end.  We illustrate it in the following example by simulating a computation that takes 5 seconds to complete, printing one dot per second while waiting.<table class="examples"><tr><td><pre>i8 : f = openIn "!sleep 5; echo -n the answer is 4"

o8 = !sleep 5; echo -n the answer is 4

o8 : File</pre>
</td></tr>
<tr><td><pre>i9 : isReady f

o9 = false</pre>
</td></tr>
<tr><td><pre>i10 : while not isReady f do (sleep 1; &lt;&lt; "." &lt;&lt; flush)
......</pre>
</td></tr>
<tr><td><pre>i11 : read f

o11 = the answer is 4</pre>
</td></tr>
<tr><td><pre>i12 : isReady f

o12 = true</pre>
</td></tr>
<tr><td><pre>i13 : atEndOfFile f

o13 = true</pre>
</td></tr>
<tr><td><pre>i14 : close f

o14 = !sleep 5; echo -n the answer is 4

o14 : File</pre>
</td></tr>
</table>
We also allow for bidirectional communication through sockets over the internet.  See <a href="_open__In__Out.html" title="open an input output file">openInOut</a> and <a href="_open__Listener_lp__String_rp.html" title="open a port for listening">openListener</a>, or the next section.<p/>
Another useful function is <a href="_wait.html" title="wait for child process">wait</a>, which can be used to wait for input to be available from any of a list of input files.</div>
</div>
</body>
</html>