Navigation

Operators and Keywords

Function List:

C++ API

: [in, out, pid] = popen2 (command, args)

Start a subprocess with two-way communication.

The name of the process is given by command, and args is an array or cell array of strings containing options for the command.

The file identifiers for the input and output streams of the subprocess are returned in in and out. If execution of the command is successful, pid contains the process ID of the subprocess. Otherwise, pid is -1.

For example:

[in, out, pid] = popen2 ("sort", "-r");
fputs (in, "these\nare\nsome\nstrings\n");
fclose (in);
EAGAIN = errno ("EAGAIN");
done = false;
do
 s = fgets (out);
 if (ischar (s))
   fputs (stdout, s);
 elseif (errno () == EAGAIN)
   pause (0.1);
   fclear (out);
 else
   done = true;
 endif
until (done)
fclose (out);
waitpid (pid);

  -| these
  -| strings
  -| some
  -| are

Note that popen2, unlike popen, will not "reap" the child process. If you don’t use waitpid to check the child’s exit status, it will linger until Octave exits.

See also: popen, waitpid.

Package: octave