使用apache thrift通过exec执行二进制文件

Executing Binary via exec using apache thrift

本文关键字:执行 二进制文件 exec 通过 apache thrift 使用      更新时间:2023-10-16

我使用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;
  }

Server.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_testn");
     }
 };
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;
}

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

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

我建议使用system()呼叫或类似的东西来代替。