Coder Social home page Coder Social logo

ubports / process-cpp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lib-cpp/process-cpp

0.0 7.0 0.0 538 KB

Migrated to https://gitlab.com/ubports/development/core/lib-cpp/process-cpp

Home Page: https://gitlab.com/ubports/development/core/lib-cpp/process-cpp

License: GNU Lesser General Public License v3.0

CMake 7.74% C++ 91.61% C 0.64%

process-cpp's Introduction

process-cpp {#mainpage}

process-cpp is a simple and straightforward wrapper around process creation and control targeted towards linux. It helps both with handling child processes and with interacting with the current process. Some of its features include:

  • Thread-safe get/set/unset operation on the current process's environment.
  • Throwing and non-throwing overloads of functions when system calls are involved.
  • Seamless redirection of input, output and error streams of child processes.
  • Type-safe interaction with the virtual proc filesystem, both for reading & writing.

The library's main purpose is to assist in testing and when a software component needs to carry out process creation/control tasks, e.g., a graphical shell. To this end, the library is extensively tested and tries to ensure fail-safe operation as much as possible.

A simple echo

// Fork and run a simple echo:
posix::ChildProcess child = posix::fork(
            []()
            {
                std::string line;
                while(true)
                {
                    std::cin >> line;
                    std::cout << line << std::endl;
                }
                return EXIT_FAILURE;
            },
            posix::StandardStreamFlags()
                .set(posix::StandardStream::stdin)
                .set(posix::StandardStream::stdout));

// Check that the resulting process has a valid pid.
EXPECT_TRUE(child.pid() > 0);

// Check on echo functionality.
const std::string echo_value{"42"};
child.cin() << echo_value << std::endl;
std::string line; child.cout() >> line;
EXPECT_EQ(echo_value, line);

// Stop the process and synchronize with the process changing state.
EXPECT_NO_THROW(child.send_signal(posix::Signal::sig_stop));
auto result = child.wait_for(posix::wait::Flag::untraced);
EXPECT_EQ(posix::wait::Result::Status::stopped,
          result.status);
EXPECT_EQ(posix::Signal::sig_stop,
          result.detail.if_stopped.signal);

// Kill the stopped process and synchronize to its state change.
EXPECT_NO_THROW(child.send_signal(posix::Signal::sig_kill));
result = child.wait_for(posix::wait::Flag::untraced);
EXPECT_EQ(posix::wait::Result::Status::signaled,
          result.status);
EXPECT_EQ(posix::Signal::sig_kill,
          result.detail.if_signaled.signal);

Adjusting OOM Score Values

// Setup the manipulator with a well-known value.
posix::linux::proc::process::OomScoreAdj oom_score_adj
{
    posix::linux::proc::process::OomScoreAdj::max_value()
};
// Apply the manipulator to the current process
EXPECT_NO_THROW(posix::this_process::instance() << oom_score_adj);
// Read back the manipulators value for the current process
EXPECT_NO_THROW(posix::this_process::instance() >> oom_score_adj);
// And check that applying the manipulator was successful.
EXPECT_EQ(posix::linux::proc::process::OomScoreAdj::max_value(),
          oom_score_adj.value);
// Instantiate the observer...
posix::linux::proc::process::OomScore oom_score;
// ... and fill in its value for the current process.
EXPECT_NO_THROW(posix::this_process::instance() >> oom_score);
// Check that applying the manipulator before results in adjustments to the
// OOM score.
EXPECT_TRUE(is_approximately_equal(oom_score.value, posix::linux::proc::process::OomScoreAdj::max_value()));

process-cpp's People

Contributors

mariogrip avatar pete-woods avatar sil2100 avatar tjyrinki avatar vorlonofportland avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  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.