简体   繁体   English

使用Apache Thrift通过exec执行二进制文件

[英]Executing Binary via exec using apache thrift

I am using Apache Thrift to set up a c++ server. 我正在使用Apache Thrift设置c ++服务器。 On the server side I want to execute 在服务器端,我要执行

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

but either the above line of code is executed first or server.serve() is executed first. 但以上代码行首先执行,或者server.serve()首先执行。 Is there any way for the server to start and have the line of code run on top of the server? 服务器有什么办法启动,并使代码行在服务器之上运行吗?

I tried putting it in the message_test function but I only want it to execute once not every time when I start up a client. 我尝试将其放在message_test函数中,但我只希望它在每次启动客户端时都执行一次。

a.cpp: 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 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_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;
}

According to the docs, execl() replaces the current process with the program called. 根据文档, execl()将当前进程替换为所调用的程序。 On the other hand, server.serve() will block until someone forces the server to exit the server loop inside. 另一方面, server.serve()将阻塞,直到有人强迫服务器退出服务器内部的服务器循环为止。

That explains why you get only one of the two. 这就解释了为什么只得到两者之一。 The one replaces the server and the other call blocks. 一个替换服务器,另一个替换呼叫块。 In essence, either one of these two calls will be (more or less) the last thing that is executed in the server code. 本质上,这两个调用中的任何一个(或多或少)将是服务器代码中执行的最后一件事。

I'd suggest to use a system() call or something like that instead. 我建议改用system()调用或类似的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM