简体   繁体   中英

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; 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).

$ 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. Is there an easy-to-use c++ package that does the above? 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. 0mq has (had?) a bunch of examples starting with simple ping-pong.

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. 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:

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.

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 . 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. 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 . 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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