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
  • 39,765
  • 26
  • 162
  • 323

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,878
  • 10
  • 53
  • 97
  • 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