<p>How should I run another program from within my <code>C</code> program? I need to be able to write data into <code>STDIN</code> of the launched program (and maybe read from it's <code>STDOUT</code>)</p>

<p>I am not sure if this is a standard C function. I need the solution that should work under Linux.</p>

[{'answer_id': 70848, 'author': 'freespace', 'author_id': 8297, 'author_profile': 'https://Stackoverflow.com/users/8297', 'pm_score': 5, 'selected': True, 'text': '<p>You want to use <code>popen</code>. It gives you a unidirectional pipe with which you can access stdin and stdout of the program.</p>\n\n<p>popen is standard on modern unix and unix-like OS, of which Linux is one :-)</p>\n\n<p>Type</p>\n\n<pre><code>man popen\n</code></pre>\n\n<p>in a terminal to read more about it.</p>\n\n<p><strong>EDIT</strong></p>\n\n<p>Whether <code>popen</code> produces unidirectional or bidirectional pipes depends on the implementation. In <a href="https://manpages.debian.org/jessie/manpages-dev/popen.3.en.html" rel="nofollow noreferrer">Linux</a> and <a href="http://man.openbsd.org/OpenBSD-current/man3/popen.3" rel="nofollow noreferrer">OpenBSD</a>, <code>popen</code> produces unidirectional pipes, which are read-only or write-only. On <a href="https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/popen.3.html" rel="nofollow noreferrer">OS X</a>, <a href="https://www.freebsd.org/cgi/man.cgi?query=popen&amp;apropos=0&amp;sektion=0&amp;manpath=FreeBSD+11.0-RELEASE+and+Ports&amp;arch=default&amp;format=html" rel="nofollow noreferrer">FreeBSD</a> and <a href="http://netbsd.gw.com/cgi-bin/man-cgi?popen++NetBSD-current" rel="nofollow noreferrer">NetBSD</a> <code>popen</code> produces bidirectional pipes.</p>\n'}, {'answer_id': 70857, 'author': 'Vhaerun', 'author_id': 11234, 'author_profile': 'https://Stackoverflow.com/users/11234', 'pm_score': 0, 'selected': False, 'text': '<p>I think you can use </p>\n\n<blockquote>\n  <p><code>freopen</code></p>\n</blockquote>\n\n<p>for this .</p>\n'}, {'answer_id': 70858, 'author': 'Jorge Ferreira', 'author_id': 6508, 'author_profile': 'https://Stackoverflow.com/users/6508', 'pm_score': 3, 'selected': False, 'text': "<ol>\n<li>Create two pipes with <code>pipe(...)</code>, one for <code>stdin</code>, one for <code>stdout</code>. </li>\n<li><code>fork(...)</code> the process.</li>\n<li>In the child process (the one where <code>fork(...)</code> returns 0) <code>dup (...)</code> the pipes to <code>stdin</code>/<code>stdout</code>.</li>\n<li><code>exec[v][e]</code> the to be started programm file in the child process.</li>\n<li>In the parent process (the one where <code>fork</code>) returns the PID of the child) do a loop that reads from the child's <code>stdout</code> (<code>select(...)</code> or <code>poll(...)</code>, <code>read(...)</code> ) into a buffer, until the\nchild terminates (<code>waitpid(...)</code>). </li>\n<li>Eventually supply the child with input on <code>stdin</code> if it expects some.</li>\n<li>When done <code>close(...)</code> the pipes.</li>\n</ol>\n"}, {'answer_id': 70954, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>You can use the system call, read <a href="http://www.linuxmanpages.com/man3/system.3.php" rel="nofollow noreferrer">manpage for system(3)</a></p>\n'}, {'answer_id': 80866, 'author': 'Jonathan Leffler', 'author_id': 15168, 'author_profile': 'https://Stackoverflow.com/users/15168', 'pm_score': 3, 'selected': False, 'text': '<p>For simple unidirectional communication, popen() is a decent solution.  It is no use for bi-directional communication, though.</p>\n<p>IMO, imjorge (Jorge Ferreira) gave most of the answer (80%?) for bi-directional communication - but omitted a few key details.</p>\n<ol>\n<li>It is crucial that the parent process close the read end of the pipe that is used to send messages to the child process.</li>\n<li>It is crucial that the child process close the write end of the pipe that is used to send messages to the child process.</li>\n<li>It is crucial that the parent process close the write end of the pipe that is used to send messages to the parent process.</li>\n<li>It is crucial that the child process close the read end of the pipe that is used to send messages to the parent process.</li>\n</ol>\n<p>If you do not close the unused ends of the pipes, you do not get sensible behaviour when one of the programs terminates; for example, the child might be reading from its standard input, but unless the write end of the pipe is closed in the child, it will never get EOF (zero bytes from read) because it still has the pipe open and the system thinks it might sometime get around to writing to that pipe, even though it is currently hung waiting for something to read from it.</p>\n<p>The writing processes should consider whether to handle the SIGPIPE signal that is given when you write on a pipe where there is no reading process.</p>\n<p>You have to be aware of pipe capacity (platform dependent, and might be as little as 4KB) and design the programs to avoid deadlock.</p>\n'}, {'answer_id': 83456, 'author': 'Steve Baker', 'author_id': 13566, 'author_profile': 'https://Stackoverflow.com/users/13566', 'pm_score': 4, 'selected': False, 'text': '<p>I wrote some example C code for someone else a while back that shows how to do this. Here it is for you:</p>\n\n<pre><code>#include &lt;sys/types.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;stdio.h&gt;\n\nvoid error(char *s);\nchar *data = "Some input data\\n";\n\nmain()\n{\n  int in[2], out[2], n, pid;\n  char buf[255];\n\n  /* In a pipe, xx[0] is for reading, xx[1] is for writing */\n  if (pipe(in) &lt; 0) error("pipe in");\n  if (pipe(out) &lt; 0) error("pipe out");\n\n  if ((pid=fork()) == 0) {\n    /* This is the child process */\n\n    /* Close stdin, stdout, stderr */\n    close(0);\n    close(1);\n    close(2);\n    /* make our pipes, our new stdin,stdout and stderr */\n    dup2(in[0],0);\n    dup2(out[1],1);\n    dup2(out[1],2);\n\n    /* Close the other ends of the pipes that the parent will use, because if\n     * we leave these open in the child, the child/parent will not get an EOF\n     * when the parent/child closes their end of the pipe.\n     */\n    close(in[1]);\n    close(out[0]);\n\n    /* Over-write the child process with the hexdump binary */\n    execl("/usr/bin/hexdump", "hexdump", "-C", (char *)NULL);\n    error("Could not exec hexdump");\n  }\n\n  printf("Spawned \'hexdump -C\' as a child process at pid %d\\n", pid);\n\n  /* This is the parent process */\n  /* Close the pipe ends that the child uses to read from / write to so\n   * the when we close the others, an EOF will be transmitted properly.\n   */\n  close(in[0]);\n  close(out[1]);\n\n  printf("&lt;- %s", data);\n  /* Write some data to the childs input */\n  write(in[1], data, strlen(data));\n\n  /* Because of the small amount of data, the child may block unless we\n   * close it\'s input stream. This sends an EOF to the child on it\'s\n   * stdin.\n   */\n  close(in[1]);\n\n  /* Read back any output */\n  n = read(out[0], buf, 250);\n  buf[n] = 0;\n  printf("-&gt; %s",buf);\n  exit(0);\n}\n\nvoid error(char *s)\n{\n  perror(s);\n  exit(1);\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/70842', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6974/']