8

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

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

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
Simon
  • 83
  • 1
  • 1
  • 3

6 Answers6

17

You want to use popen. It gives you a unidirectional pipe with which you can access stdin and stdout of the program.

popen is standard on modern unix and unix-like OS, of which Linux is one :-)

Type

man popen

in a terminal to read more about it.

EDIT

Whether popen produces unidirectional or bidirectional pipes depends on the implementation. In Linux and OpenBSD, popen produces unidirectional pipes, which are read-only or write-only. On OS X, FreeBSD and NetBSD popen produces bidirectional pipes.

freespace
  • 16,529
  • 4
  • 36
  • 58
  • 1
    Note that it's stdin _or_ stdout, not both at once. – wnoise Sep 19 '08 at 00:54
  • In modern implementations, it is stdin AND stdout. The pipe is allowed to be bidirectional, though traditionally it is unidirectional. – freespace Sep 19 '08 at 02:31
  • "man popen" on Ubuntu 9.04 says "Since a pipe is by definition unidirectional, the type argument may specify only reading or writing, not both". – Brent Bradburn Jan 15 '11 at 08:07
  • @freespace which implementations are those? Can you quote their documentation? – n. m. could be an AI Feb 27 '17 at 06:41
  • @n.m. on OS X `popen` allows for unidirectional pipes: https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/ . The same appears to be true for the *BSDs. – freespace Feb 27 '17 at 10:42
  • @freespace no wonder they do. "Unidirectional" means going in one direction, not two. You have made a claim about bidirectional pipes, can you support it with evidence? – n. m. could be an AI Feb 27 '17 at 10:46
  • @n.m. apologies, that was a typo in my comment. I meant to say bidirectional. The link I provided is also quite useless as it doesn't link directly to OS X's popen manpage. A better link is https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/popen.3.html . I will update my answer to point out the differences between Linux and *BSD derivatives. – freespace Feb 27 '17 at 12:17
  • Wow that's very interesting, but it is indeed confined to BSD systems. POSIX doesn't suport it. – n. m. could be an AI Feb 27 '17 at 13:04
11

I wrote some example C code for someone else a while back that shows how to do this. Here it is for you:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

void error(char *s);
char *data = "Some input data\n";

main()
{
  int in[2], out[2], n, pid;
  char buf[255];

  /* In a pipe, xx[0] is for reading, xx[1] is for writing */
  if (pipe(in) < 0) error("pipe in");
  if (pipe(out) < 0) error("pipe out");

  if ((pid=fork()) == 0) {
    /* This is the child process */

    /* Close stdin, stdout, stderr */
    close(0);
    close(1);
    close(2);
    /* make our pipes, our new stdin,stdout and stderr */
    dup2(in[0],0);
    dup2(out[1],1);
    dup2(out[1],2);

    /* Close the other ends of the pipes that the parent will use, because if
     * we leave these open in the child, the child/parent will not get an EOF
     * when the parent/child closes their end of the pipe.
     */
    close(in[1]);
    close(out[0]);

    /* Over-write the child process with the hexdump binary */
    execl("/usr/bin/hexdump", "hexdump", "-C", (char *)NULL);
    error("Could not exec hexdump");
  }

  printf("Spawned 'hexdump -C' as a child process at pid %d\n", pid);

  /* This is the parent process */
  /* Close the pipe ends that the child uses to read from / write to so
   * the when we close the others, an EOF will be transmitted properly.
   */
  close(in[0]);
  close(out[1]);

  printf("<- %s", data);
  /* Write some data to the childs input */
  write(in[1], data, strlen(data));

  /* Because of the small amount of data, the child may block unless we
   * close it's input stream. This sends an EOF to the child on it's
   * stdin.
   */
  close(in[1]);

  /* Read back any output */
  n = read(out[0], buf, 250);
  buf[n] = 0;
  printf("-> %s",buf);
  exit(0);
}

void error(char *s)
{
  perror(s);
  exit(1);
}
Steve Baker
  • 4,323
  • 1
  • 20
  • 15
8
  1. Create two pipes with pipe(...), one for stdin, one for stdout.
  2. fork(...) the process.
  3. In the child process (the one where fork(...) returns 0) dup (...) the pipes to stdin/stdout.
  4. exec[v][e] the to be started programm file in the child process.
  5. In the parent process (the one where fork) returns the PID of the child) do a loop that reads from the child's stdout (select(...) or poll(...), read(...) ) into a buffer, until the child terminates (waitpid(...)).
  6. Eventually supply the child with input on stdin if it expects some.
  7. When done close(...) the pipes.
Sled
  • 18,541
  • 27
  • 119
  • 168
Jorge Ferreira
  • 96,051
  • 25
  • 122
  • 132
5

For simple unidirectional communication, popen() is a decent solution. It is no use for bi-directional communication, though.

IMO, imjorge (Jorge Ferreira) gave most of the answer (80%?) for bi-directional communication - but omitted a few key details.

  1. It is crucial that the parent process close the read end of the pipe that is used to send messages to the child process.
  2. It is crucial that the child process close the write end of the pipe that is used to send messages to the child process.
  3. It is crucial that the parent process close the write end of the pipe that is used to send messages to the parent process.
  4. It is crucial that the child process close the read end of the pipe that is used to send messages to the parent process.

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.

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.

You have to be aware of pipe capacity (platform dependent, and might be as little as 4KB) and design the programs to avoid deadlock.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

You can use the system call, read manpage for system(3)

0

I think you can use

freopen

for this .

Vhaerun
  • 12,806
  • 16
  • 39
  • 38