C++ Linux Interact Another Program stdin / stdout

I have an executable in Linux that can interact with stdin/stdout. I'm trying to write a program in C++ that can interactively call this program, send commands to its stdin, and capture it's stdout.

I've been getting really confused. I don't want to fork my program (do I?). I do want my program to be able to start the "client", send data, get output, send more data, get more output, ..., and close the "client".

P.S. Yes, I'm sure this has been asked before, but I've spent a few hours really scratching my head- probably not using the correct keywords.


The only way to execute another program is via one of the exec() system calls. This is the only way. And, as you know, exec() replaces the executing program with the program specified by exec(). The process that issued the exec() will not exist any more, it's PID is now used by the new program.

It therefore logically follows, that unless you want your program to be replaced by that other executable, your program must fork(), and the child process uses exec() to execute the new executable. This is the traditional way to start a new process, and continue running the original process. A fork() is required for that.

The situation you describe is fairly typical, paint-by-the-numbers situation, that's been done countless of times:

<ol> <li>

Use pipe() to create two pipes, one for the piped stdin, one for the piped stdout.

</li> <li>

Use fork(). The child process dup2()s the read end of the stdin pipe to 0, the write end of the stdout pipe to 1, closes both ends of each of the original pipes, and exec()s the new process.

</li> <li>

The parent process closes the read end of the stdin pipe, the write end of the stdout pipe, then proceeds to interact with the child process using the write end of the stdin pipe and the read end of the stdout pipe.

</li> </ol>


  • Why does my SharePoint workflow fail when the client is running Vista or Windows 7?
  • Serving documents outside the web root folder.
  • Android date format parse throwing an Unhandled Exception
  • How can I loop through a delimited string and assign the contents of the string to local delphi vari
  • Which is Android/Java corresponding method to the C#/C++ method GetTickCount()?
  • How to make Node.js handle multiple requests efficiently?
  • Why does Redshift need to do a full table scan to find the max value of the DIST/SORT key?
  • Additional scripts in PATH
  • How to browse android SDK sources in Android studio 2.0
  • Is it possible to ask GIT, don't show a file?
  • How to generate an asynchronous reset verilog always blocks with chisel
  • PHP multiple file uploads
  • How to create two column output from a single column
  • Portable JRE on Linux - possible?
  • Android cannot disable cut copy paste
  • How to retrieve information from antrun back to maven?
  • Calculate time difference in hh:mm:ss with simple javascript/jquery
  • Spring Cloud Microservice Architecture Confusion
  • Transactional Create with Validation in ServiceStack Redis Client
  • How to install a .deb file on a jailbroken iphone programmatically?
  • Handling un-mapped Rest path
  • Illegal mix of collations for operation for date/time comparison
  • PHP - How to update data to MySQL when click a radio button
  • Optimizing database types to compact database (SQLite)
  • TFS: Get latest causes slow project reloading
  • Running a C# exe file
  • Deserializing XML into class C#
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • embed rChart in Markdown
  • How does Linux kernel interrupt the application?
  • python draw pie shapes with colour filled
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Reading document lines to the user (python)
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app