Coder Social home page Coder Social logo

wolframroesler / childprocess Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 8 KB

C++17 class to manage Unix child processes

Home Page: https://gitlab.com/wolframroesler/childprocess

License: MIT License

CMake 2.88% C++ 97.12%
cplusplus-17 unix process-manager childprocess

childprocess's Introduction

Child Process Management Library

Features:

  • Run a child process in the background
  • Specify exact parameters, not a shell command line
  • Write into the process' standard input
  • Read from the process' standard output and standard error
  • Wait until the process has terminated
  • Get the process' exit status
  • In short, encapsulates the Unix fork/exec/kill/wait system calls

Also:

  • Send a termination signal to the process (in the dtor)
  • Run an initialization function in the child process
  • Thread-safe
  • Exception-safe
  • Can be used instead of system(3) and popen(3) in CERT-compliant applications

Prerequisites:

  • C++17
  • Boost
  • Unix-like system

How to build and run the test program

This repository contains the child process library (childprocess.hpp, childprocess.cpp) together with a Boost.Test unit test program. To build and run the unit tests:

$ mkdir build
$ cd build
$ cmake ..
$ make
$ make test

How to use it in your own projects

Copy childprocess.hpp and childprocess.cpp to locations of your choise and add them to your build settings. Install and link with boost (see CMakeLists.txt for reference).

Examples

Reference code only. For complete examples refer to the unit test program (test.cpp).

Start a process and wait for termination

Run make -j test, wait for completion, and check exit status.

#include <childprocess.hpp>

auto chld = sdb::ChildProcess("/usr/bin/make", { "-j", "test" });
const auto status = child.join();
if (status!=0) {
    // Failed
}

Write to stdin, read from stdout

Run sed to convert Hello world into Good night world.

#include <childprocess.hpp>

auto chld = sdb::ChildProcess(
    "/bin/sed",
    { "s/Hello/Good night/g" },
    sdb::ChildProcess::IN | sdb::ChildProcess::OUT
);

auto in = chld.make_stdin([&data](std::ostream& os) {
    os << "Hello world\n";
});

std::string input;
auto out = chld.get_stdout([&input](std::istream& is) {
    input.assign(std::istreambuf_iterator<char>(is),std::istreambuf_iterator<char>());
});

in.get();
out.get();

const auto status = chld.join();
if (status!=0) {
    // Failed
}

// Now input is "Good night world"

Wolfram Rösler • [email protected]https://gitlab.com/wolframroeslerhttps://twitter.com/wolframroeslerhttps://www.linkedin.com/in/wolframroesler/

childprocess's People

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.