Gregory Hildstrom Projects Publications Resume Contact About Youtube Donate

Capture Child Process Output With Pipes

Introduction
capture_child_output.c
Makefile

Introduction

Some recent tasks at work highlighted some obvious gaps in my working knowledge of Linux pipes. I spent some personal time brushing up and learning. This page addresses how to exec a child process and capture its standard output and standard error in the parent process. I show the example program and output, but the code is also available in
pipeoutput.zip.

The man pages are extremely helpful when diving into stuff like this, but I definitely hit a wall and resorted to some Internet searching. I came across a very helpful page, as noted in the code, and I incorporated some ideas from that page in this example.

My test system runs Fedora 20 Linux with the latest updates as of August 9, 2015. The Makefile at the end shows the compile commands.

capture_child_output.c

[an error occurred while processing this directive]
Here is the output:
[ghildstrom@hplt pipeoutput]$ ./capture_child_output 
parent pid is 7391 and child pid is 7392
child pid is 7392
parent received output from child: error from client hello from client 

[ghildstrom@hplt pipeoutput]$ ./capture_child_output 
parent pid is 7413 and child pid is 7414
child pid is 7414
parent received output from child: error from client 
parent received output from child: hello from client

Makefile

[an error occurred while processing this directive]