[c] Redirecting exec output to a buffer or file

I'm writing a C program where I fork(), exec(), and wait(). I'd like to take the output of the program I exec'ed to write it to file or buffer.

For example, if I exec ls I want to write file1 file2 etc to buffer/file. I don't think there is a way to read stdout, so does that mean I have to use a pipe? Is there a general procedure here that I haven't been able to find?

This question is related to c exec fork

The answer is


You need to decide exactly what you want to do - and preferably explain it a bit more clearly.

Option 1: File

If you know which file you want the output of the executed command to go to, then:

  1. Ensure that the parent and child agree on the name (parent decides name before forking).
  2. Parent forks - you have two processes.
  3. Child reorganizes things so that file descriptor 1 (standard output) goes to the file.
  4. Usually, you can leave standard error alone; you might redirect standard input from /dev/null.
  5. Child then execs relevant command; said command runs and any standard output goes to the file (this is the basic shell I/O redirection).
  6. Executed process then terminates.
  7. Meanwhile, the parent process can adopt one of two main strategies:
    • Open the file for reading, and keep reading until it reaches an EOF. It then needs to double check whether the child died (so there won't be any more data to read), or hang around waiting for more input from the child.
    • Wait for the child to die and then open the file for reading.
    • The advantage of the first is that the parent can do some of its work while the child is also running; the advantage of the second is that you don't have to diddle with the I/O system (repeatedly reading past EOF).

Option 2: Pipe

If you want the parent to read the output from the child, arrange for the child to pipe its output back to the parent.

  1. Use popen() to do this the easy way. It will run the process and send the output to your parent process. Note that the parent must be active while the child is generating the output since pipes have a small buffer size (often 4-5 KB) and if the child generates more data than that while the parent is not reading, the child will block until the parent reads. If the parent is waiting for the child to die, you have a deadlock.
  2. Use pipe() etc to do this the hard way. Parent calls pipe(), then forks. The child sorts out the plumbing so that the write end of the pipe is its standard output, and ensures that all other file descriptors relating to the pipe are closed. This might well use the dup2() system call. It then executes the required process, which sends its standard output down the pipe.
  3. Meanwhile, the parent also closes the unwanted ends of the pipe, and then starts reading. When it gets EOF on the pipe, it knows the child has finished and closed the pipe; it can close its end of the pipe too.

Since you look like you're going to be using this in a linux/cygwin environment, you want to use popen. It's like opening a file, only you'll get the executing programs stdout, so you can use your normal fscanf, fread etc.


You could also use the linux sh command and pass it a command that includes the redirection:

string cmd = "/bin/ls > " + filepath;

execl("/bin/sh", "sh", "-c", cmd.c_str(), 0);

After forking, use dup2(2) to duplicate the file's FD into stdout's FD, then exec.


Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to exec

I do not understand how execlp() works in Linux Running a Python script from PHP How do I execute a Shell built-in command with a C function? What are the uses of the exec command in shell scripts? php exec() is not executing the command How to call execl() in C with the proper arguments? Run Bash Command from PHP PHP shell_exec() vs exec() Ruby, Difference between exec, system and %x() or Backticks How to execute an external program from within Node.js?

Examples related to fork

How to use execvp() Example of waitpid() in use? How to make parent wait for all child processes to finish? fork: retry: Resource temporarily unavailable fork() child and parent processes How to kill a child process by the parent process? How to use shared memory with Linux in C The difference between fork(), vfork(), exec() and clone() fork() and wait() with two child processes Redirecting exec output to a buffer or file