简体   繁体   English

从函数返回值而不退出C中的函数

[英]return a value from a function without exiting the function in C

I have a C program where via tcp socket I am sending a command to the peer process. 我有一个C程序,其中我通过tcp套接字向对等进程发送命令。 On receiving this command, the peer process will start performing the requested command. 收到此命令后,对等进程将开始执行所请求的命令。 But, it takes long time and I don't want the tcp socket to wait for long time for a return value. 但是,这需要很长时间,并且我不希望tcp套接字等待很长时间才能获得返回值。 I think, fork a process will help me to do this. 我认为,分叉流程将帮助我做到这一点。 But, I want some other ways to make this thing work out. 但是,我需要其他一些方法来解决这个问题。 Please share your thoughts and experience related to this. 请分享您与此相关的想法和经验。 Please help. 请帮忙。 thanks in advance, 提前致谢,

There are many forms of Inter Process Communication (IPC) strategies in UNIX. UNIX中有多种形式的进程间通信(IPC)策略。 One of my favorite is to spawn a child process and receive a notification upon its demise. 我最喜欢的方法之一是生成一个子进程并在其终止时收到通知。 A primer on IPC can be found here and an example of asynchronously waiting on a child processes death can be found here . IPC的底漆,可以发现这里和对孩子异步等待一个例子处理死亡可以发现在这里 If you don't want to spawn a separate process you can used named pipes to have information flow back and forth between the two processes. 如果您不想产生单独的进程,则可以使用命名管道在两个进程之间来回传递信息。

You can use a thread and wait on it until the reply comes. 您可以使用线程并等待它,直到回复到来。 pthread and wait will do just fine(threads are light weight processes though). pthread和wait可以很好地工作(尽管线程是轻量级进程)。

If you really want to do this by never spawning a process, you'll have to resort to something like callbacks when the TCP reply is received, this might be available from TCP wrapper libraries but including something like this or making something like it doesn't make sense if you are not waiting for a lot of replies. 如果您真的想通过从未生成进程来执行此操作,则必须在收到TCP答复时诉诸于回调之类的方法,这可能可以从TCP包装器库中获得,但包括诸如此类或使之不具有某种意义。如果您不等待大量答复,那就没有道理了。 For a few reply waits threads are fine. 对于一些答复等待线程很好。

I don't have it clear what you mean. 我不清楚你的意思。 You say you want to give an answer before you know the result, or may be, the return value does not depend on the process itself. 您说您想在知道结果之前给出答案,或者可能是,返回值不取决于过程本身。 In term of transactional communications there two approaches, "synchronous" and "asynchronous" transactions. 在事务通信方面,有两种方法,“同步”和“异步”事务。 The former waits until the server completes de process and gives the result, the second one will send the request and gets a token, close the socket and later on it will request the result with that token. 前者一直等到服务器完成处理并给出结果,第二位服务器将发送请求并获得一个令牌,关闭套接字,然后它将使用该令牌请求结果。 So, what you could do is give to the requested peer a token where it can ask for an answer once the process is finished. 因此,您可以做的是给被请求的对等方一个令牌,该令牌可以在过程完成后请求答案。 Note that you may give an answer which indicates "the process is still running, try later". 请注意,您可能会给出一个表明“进程仍在运行,请稍后再试”的答案。 In your sever code you need to program this scenario, give each process a number, keeps the result with that number and, when requested, with that number send the results. 在服务器代码中,您需要对此场景进行编程,为每个进程指定一个编号,并使用该编号保留结果,并在请求时使用该编号发送结果。 If this is not appropriate, please explain in more detail what you're trying to accomplish. 如果不合适,请更详细地说明您要完成的工作。

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

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