IPC – Inter Process Communications (package Combinat)
IPC – Library for Inter Process Communications
The IPC library provides various functions for Inter-Process Communications, and child process handling.
In this section, we pick a few sample applications at random. First, we export the IPC library to save us some typing:
export(IPC):
In our first example, we run a shell command, and read the output line by line:
child := popen("echo coucou1; echo coucou2", "r"):
getline(child);
getline(child);
getline(child);
close(child):
Note that there is no output for the last call to getline. Indeed, after the end of output, getline returns null().
We redo the same thing, but at a lower level. Accessing directly the streams is a little bit faster if efficiency is at a premium. This is also required if one wants to read from the standard error stream of the child slave::stderr(child):
child := popen("echo coucou1; echo coucou2", "r"):
pid := slave::pid(child):
childout := slave::stdout(child):
getline(childout);
getline(childout);
getline(childout);
close(childout):
waitpid(pid):
We demonstrate how to run some computations in a separate MuPADprocess:
child := popen2("mupkern -S -P pe"):
putline(child, "setuserinfo(NIL):"):
putline(child, "fprint(Unquoted, 0, 1+1):"):
text2expr(getline(child));
putline(child, "fprint(Unquoted, 0, 7!):"):
text2expr(getline(child));
putline(child, "fprint(Unquoted, 0, expand((x+1)^7)):"):
text2expr(getline(child));
close(child):
This allow for some basic parallelism on a multi-processor machine, or on several machines by using ssh.
Here we run some computations with GAP:
child := popen2("gap -b -q -n"):
putline(child, "1+1;"):
text2expr(getline(child));
putline(child, "G := Group((1,2,3,4));"):
getline(child):
putline(child, "Size(G);"):
text2expr(getline(child));
putline(child, "IsSolvable(G);"):
getline(child);
To conclude, we show how to create and use a very crude maple slave. Of course, this hack is not robust to errors, and only simple expressions can be manipulated. But in some easy cases this can still be useful.
domain maple
child :=
proc()
local child;
begin
child := IPC::popen2("maple -q");
IPC::putline(child, "kernelopts(printbytes=false):");
child;
end_proc();
childin := IPC::slave::stdin(dom::child);
childout := IPC::slave::stdout(dom::child);
new :=
proc(e: DOM_STRING): DOM_STRING
begin
IPC::putline(dom::childin, "lprint(".e."):");
return(IPC::getline(dom::childout));
end_proc;
begin
end:
maple("1+1");
text2expr(maple("expand((1+x)^7)"))