From 9075eb00e694b0ccdd8b2569d1c011cb63df8f2e Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Fri, 10 Jun 2011 16:20:17 -0400 Subject: proper use of pipes --- mteval/external_scorer.cc | 52 ++++++++++++++++++++++++++++++++--------------- mteval/external_scorer.h | 4 ++-- 2 files changed, 38 insertions(+), 18 deletions(-) (limited to 'mteval') diff --git a/mteval/external_scorer.cc b/mteval/external_scorer.cc index 3757064b..1c09c2a1 100644 --- a/mteval/external_scorer.cc +++ b/mteval/external_scorer.cc @@ -3,6 +3,7 @@ #include // popen #include #include +#include #include #include #include @@ -16,7 +17,7 @@ map > ScoreServerManager::servers_; class METEORServer : public ScoreServer { public: - METEORServer() : ScoreServer("java -Xmx1024m -jar /Users/cdyer/software/meteor/meteor-1.3.jar - - -mira -lower -t tune -l en") {} + METEORServer() : ScoreServer("java -Xmx1024m -jar /usr0/cdyer/meteor/meteor-1.3.jar - - -mira -lower -t tune -l en") {} }; ScoreServer* ScoreServerManager::Instance(const string& score_type) { @@ -32,10 +33,30 @@ ScoreServer* ScoreServerManager::Instance(const string& score_type) { return s.get(); } -ScoreServer::ScoreServer(const string& cmd) : pipe_() { +ScoreServer::ScoreServer(const string& cmd) { cerr << "Invoking " << cmd << " ..." << endl; - pipe_ = popen(cmd.c_str(), "r+"); - if (!pipe_) { perror("popen"); abort(); } + if (pipe(p2c) < 0) { perror("pipe"); exit(1); } + if (pipe(c2p) < 0) { perror("pipe"); exit(1); } + pid_t cpid = fork(); + if (cpid < 0) { perror("fork"); exit(1); } + if (cpid == 0) { // child + close(p2c[1]); + close(c2p[0]); + dup2(p2c[0], 0); + close(p2c[0]); + dup2(c2p[1], 1); + close(c2p[1]); + cerr << "Exec'ing from child " << cmd << endl; + vector vargs; + SplitOnWhitespace(cmd, &vargs); + const char** cargv = static_cast(malloc(sizeof(const char*) * vargs.size())); + for (unsigned i = 1; i < vargs.size(); ++i) cargv[i-1] = vargs[i].c_str(); + cargv[vargs.size() - 1] = NULL; + execvp(vargs[0].c_str(), (char* const*)cargv); + } else { // parent + close(c2p[1]); + close(p2c[0]); + } string dummy; RequestResponse("SCORE ||| Reference initialization string . ||| Testing initialization string .", &dummy); assert(dummy.size() > 0); @@ -43,7 +64,7 @@ ScoreServer::ScoreServer(const string& cmd) : pipe_() { } ScoreServer::~ScoreServer() { - pclose(pipe_); + // TODO close stuff, join stuff } float ScoreServer::ComputeScore(const vector& fields) { @@ -81,21 +102,20 @@ void ScoreServer::Evaluate(const vector >& refs, const vector { diff --git a/mteval/external_scorer.h b/mteval/external_scorer.h index 59ece269..a28fb920 100644 --- a/mteval/external_scorer.h +++ b/mteval/external_scorer.h @@ -2,7 +2,6 @@ #define _EXTERNAL_SCORER_H_ #include -#include #include #include #include @@ -21,7 +20,8 @@ class ScoreServer { private: void RequestResponse(const std::string& request, std::string* response); - FILE* pipe_; + int p2c[2]; + int c2p[2]; }; struct ScoreServerManager { -- cgit v1.2.3