1

I use psuedo-code to express what I want to do:

FILE* fd = popen("/bin/cat", ...);
Write some data to the stdin of `/bin/cat` using fd;
Read all data from the stdout of `/bin/cat` using fd;

Is it possible?

xmllmx
  • 37,882
  • 21
  • 139
  • 300

1 Answers1

2

popen() can only read from the new process. If you need to read and write,

  • Create a pipe that will be connected to a new process using pipe().
  • Fork a new process using fork()
  • redirect the process's input and outputs to the pipes that you created earlier using dup2()
  • call exec on the child process (the new process) using exec family functions
Kam
  • 5,546
  • 5
  • 48
  • 87
  • Also, you need to multiplex input & output using e.g. [poll(2)](http://man7.org/linux/man-pages/man2/poll.2.html) and you probably would need some [event loop](http://en.wikipedia.org/wiki/Event_loop) – Basile Starynkevitch Dec 02 '14 at 06:01