Home > Backend Development > C++ > body text

How to Execute a Child Process with Piped Stdin/Stdout in Linux?

Mary-Kate Olsen
Release: 2024-11-09 03:52:02
Original
676 people have browsed it

How to Execute a Child Process with Piped Stdin/Stdout in Linux?

Executing a Child Process with Piped Stdin/Stdout in Linux

In Linux, executing a child process with pipedstdin/stdout requires a combination of Linux syscalls or POSIX functions. To achieve this, we'll utilize the following techniques:

  1. Pipe Creation (pipe syscall): Creates a unidirectional interprocess communication channel, allowing data exchange between parent and child processes.
  2. File Descriptor Duplication (dup2 syscall): Duplicates an existing file descriptor, used to redirect input or output streams.
  3. fork and exec (fork execve syscalls): Creates a new child process that executes a specified command (in our case, "foo").
  4. File Descriptor Management: Closing unused file descriptors to prevent errors and ensure correct resource handling.

Below is a C example that implements these techniques:

#include <iostream>
#include <unistd.h>
#include <stdlib.h>

using namespace std;

int main() {
  // Input string
  string s = "Hello, world!";

  // Create pipes for stdin and stdout
  int stdinPipe[2], stdoutPipe[2];
  pipe(stdinPipe);
  pipe(stdoutPipe);

  // Fork a child process
  int pid = fork();

  if (pid == 0) {  // Child process
    // Redirect stdin and stdout to pipes
    dup2(stdinPipe[0], STDIN_FILENO);  // Read from pipe
    dup2(stdoutPipe[1], STDOUT_FILENO);  // Write to pipe

    // Close unused file descriptors
    close(stdinPipe[1]);
    close(stdoutPipe[0]);

    // Execute "foo" with piped stdin
    execlp("foo", "foo", NULL);

    // Exit child process on failure
    exit(1);
  } else if (pid > 0) {  // Parent process
    // Close unused file descriptors
    close(stdinPipe[0]);
    close(stdoutPipe[1]);

    // Write to stdin pipe
    write(stdinPipe[1], s.c_str(), s.length());
    close(stdinPipe[1]);

    // Read from stdout pipe
    char buffer[256];
    int bytesRead = 0;
    string output;
    while ((bytesRead = read(stdoutPipe[0], buffer, sizeof(buffer))) > 0) {
      output.append(buffer, bytesRead);
    }
    close(stdoutPipe[0]);

    // Print output string
    cout << output << endl;
  }

  return 0;
}
Copy after login

This code snippet:

  • Creates pipes for stdin and stdout using pipe.
  • Forks a child process using fork.
  • In the child process, it redirects stdin and stdout to pipes using dup2 and then executes "foo" with piped stdin using execlp.
  • In the parent process, it closes unused file descriptors, writes to stdin pipe, and reads from stdout pipe to capture the output.

The above is the detailed content of How to Execute a Child Process with Piped Stdin/Stdout in Linux?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template