title: Easiest way to execute linux program and communicate with it via stdin/stdout in C/C++
body: <p>I have program I cant modify, <em>as is</em>, and I need to execute it, write some data to its stdin and get the answer from its stdout in programmatic manner, automated.
  What is the <em>simpliest</em> way to do this?</p>
  
  <p>I suppose something like this pseudo-C-code</p>
  
  <pre><code>char input_data_buffer[] = "calculate 2 + 2\nsay 'hello world!'";
  char output_data_buffer[MAX_BUF];
  IPCStream ipcs = executeIPC("./myprogram", "rw");
  ipcs.write(input_data_buffer);
  ipcs.read(output_data_buffer);
  ...
  </code></pre>
  
  <p>PS: I thought of popen, but AFAIK there is only one-way pipes in linux</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>It is supposed it will be one-message-from-each-side communication. Firstly parent side send input to child process' stdin, then child provides output to its stdout and exits, meanwhile parent reads its stdout. Now about communication termination: I think when child process exits it will send EOF terminator to stdout, so parent will know exactly whether child done, on the other hand it is guaranteed that parent knows what kind of input child expects for.</p>
  
  <p>Generally this program (parent) - a student's solution tester. It takes paths to two other executables from CLI, the first is student's program to test, the second is etalon correctly working program, which solves very same problem.</p>
  
  <p>Input/output of students programs is strictly specified, so tester run both programs and compares its outputs for lots of random inputs, all mismatches will be reported.</p>
  
  <p>Input/output max size is estimated at few hundreds kilobytes</p>
  
  <p>Example: ..implement insertion sort algorithm ... first line there is sequence length ... second line there is sequence of numbers a_i where |a_i| &lt; 2^31 - 1...
  output first line must be sum of all elements, the second line must be sorted sequence.</p>
  
  <p>Input:</p>
  
  <pre><code>5
  1 3 4 6 2
  </code></pre>
  
  <p>Expected output:</p>
  
  <pre><code>16
  1 2 3 4 6
  </code></pre>