【问题标题】:Executing Binary via exec using apache thrift使用 apache thrift 通过 exec 执行二进制文件
【发布时间】:2015-06-04 21:03:35
【问题描述】:

我正在使用 Apache Thrift 来设置 C++ 服务器。在服务器端我要执行

execl("a.out","",(char *)NULL);

但是要么先执行上面的代码行,要么先执行 server.serve()。有什么方法可以让服务器启动并让这行代码在服务器之上运行?

我尝试将它放在 message_test 函数中,但我只希望它执行一次,而不是每次启动客户端时。

a.cpp:

 int main(){
     cout<<"I have started up"<<endl;
     string message;
     while (cin >> message){
         cout<<"your message is: "<<message<<endl;
     }
     cout<<"I have now exited"<<endl;
  }

服务器.cpp

class ThforkServiceHandler : virtual public ThforkServiceIf {
     public:
     ThforkServiceHandler() {
       // Your initialization goes here

     }
     void message_test(const std::string& message) {
       // Your implementation goes here
        printf("message_test\n");
     }
 };

int main(int argc, char **argv) {
    int port = 9090;
    shared_ptr<ThforkServiceHandler> handler(new ThforkServiceHandler());
    shared_ptr<TProcessor> processor(new ThforkServiceProcessor(handler));
    shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
    shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
    shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); 
    TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
    server.serve();
    return 0;
}

【问题讨论】:

    标签: c++ thrift


    【解决方案1】:

    根据文档,execl() 用调用的程序替换当前进程。另一方面,server.serve() 会阻塞,直到有人强制服务器退出里面的服务器循环。

    这就解释了为什么你只能得到两者之一。一个替换服务器,另一个调用块。本质上,这两个调用中的任何一个都将(或多或少)在服务器代码中执行的最后一件事。

    我建议使用system() 呼叫或类似的方式。

    【讨论】:

    • 或类似fork 并在并行进程中调用serve()execl。 ThforkServiceHandler 的名字甚至暗示了它:)
    • 嘿,在 main() 中,我使用了 fork ,在子节点中我调用了 execl() ,在父节点中我调用了 serve() 并且它有效。现在我只是想使用 dup2 和管道并保持 exel() 打开而不是退出。所以我可以继续调用它。
    • 很高兴您发现答案有助于解决问题。现在告诉全世界!
    • 你们知道让while(cin &gt;&gt; message) 循环永远打开的方法吗?我尝试使用管道,但关闭管道后程序继续关闭。
    猜你喜欢
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 2014-06-03
    • 1970-01-01
    相关资源
    最近更新 更多