Boost :: process - как заставить его работать только ОДИН процесс? - PullRequest
1 голос
/ 16 сентября 2011

Так что с boost :: process (что я взял из последней boost sandbox svn ) мы можем сделать что-то вроде запуска одного приложения и перенаправления вывода в файл с таким кодом:

#include <string> 
#include <vector> 
#include <iostream> 
#include <algorithm>
#include <iterator>
#include <boost/asio.hpp>
#include <boost/process.hpp>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>  

std::vector<std::string> split(const std::string& s, const std::string& delim, const bool keep_empty = false) {
    std::vector<std::string> result;
    if (delim.empty()) {
        result.push_back(s);
        return result;
    }
    std::string::const_iterator substart = s.begin(), subend;
    while (true) {
        subend = search(substart, s.end(), delim.begin(), delim.end());
        std::string temp(substart, subend);
        if (keep_empty || !temp.empty()) {
            result.push_back(temp);
        }
        if (subend == s.end()) {
            break;
        }
        substart = subend + delim.size();
    }
    return result;
}

 boost::process::child start_child(boost::filesystem::path path_to_exec, std::string arguments) 
{ 

    std::string exec = path_to_exec.string();
    boost::process::context ctx; 
    ctx.environment = boost::process::self::get_environment();
    ctx.stdout_behavior =  boost::process::capture_stream(); 

    #if defined(BOOST_POSIX_API) 
        return  boost::process::launch(exec, split(arguments, " "), ctx); 
    #elif defined(BOOST_WINDOWS_API)
        return  boost::process::launch_shell(exec + " " + arguments, ctx); 
    #else 
    #  error "Unsupported platform." 
    #endif 
} 


int main() 
{ 
    boost::filesystem::path exec =  boost::filesystem::current_path();
    exec /= "CloudClient/CloudClient.exe";
     boost::process::child c = start_child(exec, "--server=127.0.0.1:4773/ --username=robota --robot > file.a"); 
     boost::process::pistream &is = c.get_stdout();
     std::string line; 
     while (std::getline(is, line)) 
         std::cout << line << std::endl; 

     boost::process::status s = c.wait(); 
     std::cin.get();
} 

Но я хочу ограничить это так, чтобы он запускал только один процесс - приложение и не мог создавать такие каналы.Можно ли как-нибудь сделать мою функцию boost::process::child start_child(boost::filesystem::path path_to_exec, std::string arguments) безопасной или хотя бы более безопасной в отношении того, что я хочу?


Кстати: на окнах я не могу использовать return boost::process::launch(exec, split(arguments, " "), ctx); без приложения для дробления, которое япопробуй начать = (

1 Ответ

0 голосов
/ 21 июня 2013

Может быть следующее.

bp::context ctx;
ctx.stdout_behavior = bp::silence_stream();
bp::launch(exec, args, ctx);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...