简体   繁体   English

死简单的POSIX C ++ IPC

[英]Dead-simple POSIX C++ IPC

I am interested in a c++ linux-only (possible relaxed to posix-only) IPC solution that would behave as follows; 我感兴趣的是一个c ++ linux-only(可能放宽到posix-only)的IPC解决方案,其行为如下; a program called 'calculator' is started, and can listen to messages. 启动了一个名为“计算器”的程序,可以收听消息。 Calculator would have a loop that periodically checks for message strings and then acts on the based on their content. 计算器将有一个循环,定期检查消息字符串,然后根据其内容对其进行操作。

Another program called 'send_msg' can send messages to its pid (ideally a hostname/pid, through tcp or udp). 另一个名为'send_msg'的程序可以向其pid发送消息(理想情况下是主机名/ pid,通过tcp或udp)。

$ calculator &

// awhile later

$ send_msg <calculator pid> show calculations
Calc1: 52% complete
Calc2: 21% complete
$ send_msg <calculator pid> alter Calc2 <numeric parameters>
Ok! I'm restarting my calculations!
$

I am very versed in c++, but know nothing about network programming and am not interested in spending much time right now to learn it. 我非常精通c ++,但对网络编程一无所知,也不想花很多时间来学习它。 Is there an easy-to-use c++ package that does the above? 是否有一个易于使用的c ++包,可以做到这一点? I would rather not have to choose things like port numbers, file locations, etc. 我宁愿不必选择端口号,文件位置等等。

I think you may like zeromq (spelled 0mq), or the forked crossroadsio , as they abstract away a lot of the handholding allowing you simply pub/sub, as well as many other patterns. 我想你可能会喜欢zeromq (拼写为0mq),或者分叉的十字路口 ,因为它们抽象了很多手柄,允许你简单地发布/ sub,以及许多其他模式。 0mq has (had?) a bunch of examples starting with simple ping-pong. 0mq有一堆以简单的乒乓球开头的例子。

The setup you are requesting is anything but simple, I think. 我想,你要求的设置不过是简单的。

You'd probably do best with either a Unix-domain socket or a TCP socket (port number) for communication between the background calculator and the front end. 对于背景计算器和前端之间的通信,您可能最好使用Unix域套接字或TCP套接字(端口号)。 So, for example, you might run: 因此,例如,您可能会运行:

calculator -p 3456 &

The calculator is then listening on port 3456. Your send_msg program can then be used to make the calculator do things: 然后计算器正在侦听端口3456.然后,您的send_msg程序可用于使计算器执行以下操作:

send_msg -p 3456 show calculations

When the calculator receives the message, it acts according to the orders, sending the answer back to the send_msg progam on the socket, which then echoes it to its standard output. 当计算器收到消息时,它根据命令行动,将答案发送回套接字上的send_msg程序,然后将其回送到其标准输出。

Meanwhile, you have a calculator that may need to be multi-threaded. 同时,你有一个可能需要多线程的计算器。 It also needs to be able to determine how much work is involved in each calculation, so that it can report on the progress of each calculation. 它还需要能够确定每次计算涉及的工作量,以便它可以报告每次计算的进度。 Neither you nor I have specified how the calculation is set up, but it might be: 你和我都没有说明如何设置计算,但它可能是:

send_msg -p 3456 new calc.file

to indicate that the calculator should start a new calculation, reading the problem from the file calc.file . 表示计算器应该开始一个新的计算,从文件calc.file读取问题。 It might echo back: 它可能会回应:

Calc1: ETC = 3:15

where, by some more or less devious means, it has determined that the Estimated Time to Completion (ETC) is 3 minutes, 15 seconds. 其中,通过一些或多或少的狡猾手段,它确定预计完成时间(ETC)为3分15秒。 You can set up the second calculation in a similar way. 您可以以类似的方式设置第二个计算。 To handle this, you need a controller thread that is listening for connections from send_msg . 要处理此问题,您需要一个侦听来自send_msg连接的控制器线程。 When it gets told to create a new job, it starts a new thread (or process) to do the calculation. 当它被告知创建一个新作业时,它会启动一个新线程(或进程)来进行计算。 There has to be some agreed mechanism between the master thread in the calculator and the actual calculating threads. 计算器中的主线程与实际计算线程之间必须有一些商定的机制。 This might be as simple as a location where each thread writes its progress and the master reads. 这可能就像每个线程写入进度并且主要读取的位置一样简单。 But the calculation threads need to keep track of how much work they've done, how much there is left to do, and whether the estimates need to be changed. 但计算线程需要跟踪他们完成了多少工作,剩下多少工作,以及是否需要更改估算。

Now, I might be making things too complicated, but the interface you showed suggests that something similar to that might be necessary. 现在,我可能会把事情弄得太复杂,但是你展示的界面表明可能需要类似的东西。 If you single-thread the calculator, it has to do some sort of round-robin scheduling of its work on each calculation you set it, as well as periodically checking to see whether the send_msg program has sent a new message. 如果您对计算器进行单线程处理,则必须对您设置的每个计算进行某种循环调度,并定期检查send_msg程序是否发送了新消息。

看看RCF - 它是原生的C ++并且具有发布/订阅支持,这应该使这很容易。

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

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