与来自 C++ 的脚本进行通信程序
我有一个 C++ 程序(非常复杂,代码和执行时间都很长)。 该程序偶尔会停止并调用用户指定的 shell 脚本。
在调用脚本之前,我的程序会创建一个包含当前数据的 .out
文件。我通过 system() 命令调用脚本。然后,该脚本读取 .out
文件,并创建自己的 script.out
文件并退出。
然后system()
函数调用结束,我的程序读取并解析script.out
文件。
问题:有没有更好的方法来执行我的 C++ 程序和随机 shell 脚本之间的通信?
我的目的是让两者之间进行充分的沟通。脚本实际上可以“询问”程序“您现在有什么数据?”程序会以一些严格的约定来回复。然后脚本可能会说“添加此数据...”,或“删除所有以前的数据”等等。
我需要这个的原因是因为 shell 脚本告诉程序修改其数据。放入原始 .out 文件中的确切数据。所以修改完成后——程序实际保存的数据与.out文件中写入的数据并不对应。
谢谢!
聚苯乙烯 我发誓我已经四处寻找,但每个人都建议使用中间文件。
I have a c++ program (very complicated, and lengthy both in code and execution time).
Once in a while this program stops and calls a user-specified shell script.
Before calling the script, my program creates a .out
file with current data. I call the script via system()
command. The script then reads the .out
file, and creates its own script.out
file and exits.
Then the system()
function call ends, and my program reads and parses the script.out
file.
Question: is there a better way to execute communication between my c++ program and a random shell script?
My intent is to have full communication between the two. Script could virtually "ask" the program "What data do you have right now?" and the program would reply with some strict convention. Then the script could say "Add this data...", or "delete all your previous data" etc.etc.
The reason I need this is because the shell script tells the program to modify its data. The exact data that was put in the original .out file. So after the modification is done -- the actual data held by the program does not correspond to the data written in the .out file.
Thanks!
P.S.
I swear I've searched around, but everyone suggests an intermediate file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当然有一些方法可以在没有中间文件的情况下做到这一点。最常见的方法是使用命令行参数进行输入,使用管道进行标准输出;其他人也使用管道进行输入。
system
最直接的替代方案是使用打开
。There are certainly ways to do that without intermediate files. The most common approach is to use command line arguments for input, and pipes for standard output; others also use pipes for input. The most straight-forward alternative to
system
then is to usepopen
.在类 Unix 系统上?也许
pipe (2)
适合您?从手册页(Mac OS X 10.5 版本):
当然,您必须使用
fork
和exec
对来创建管道。也许这已经被详细回答了,现在你知道要搜索什么...我这样做已经有一段时间了,但是:
pipe两次。现在您有两个管道并控制它们的两端。
exec
系列函数之一,将其图像替换为您要运行的 shell 的图像但是首先您将使用dup2
将其标准输入和输出替换为两个管道的末端(同样,这是您需要清楚哪个管道是谁的地方)。此时您有两个进程,主进程可以将内容发送到一个管道中,它们将在脚本的标准输入上接收,并且脚本写入其标准输出的任何内容都将通过另一个管道发送到控制进程。所以他们轮流进行,就像与 shell 交互一样。
On a unix-like system? Perhaps
pipe (2)
will work for you?From the man page (Mac OS X 10.5 version):
You will, of course, have to follow the creation of the pipes with a
fork
andexec
pair. Probably this has already been answered in detail, and now you know what to search on...It's been a while since I did this, but:
pipe
twice. Now you have two pipes and control both ends of both of them.exec
family of function to replace it's image with that of the shell you want to run but first you will usedup2
to replace it's standard input and output with the ends of the two pipes (again, this is where you need to be clear about which pipe is which).At his point you have two processes, the main process can send things into one pipe ad they will be received on the standard input of the script, and anything the script writes to it's standard output will be sent up the other pipe to the controlling process. So they take turns, just like interacting with the shell.
您可以使用管道或(也许更方便)套接字 - 例如 gdb 的前端,或者期望这样做。这需要更改您的 shell 脚本,并从
system()
切换到更底层的fork()
和exec()
。它相当复杂,因此请更具体地了解您的环境以及您需要澄清的内容。
You can use pipes or (maybe more convenient) sockets - for example frontends to gdb, or expect do that. It would require changes to your shell scripts, and switching from
system()
to more low-levelfork()
andexec()
.It's rather complicated so please, be more specific about your environment and what you need to clarify.
您问的是有关进程间通信(IPC)的问题。
有很多方法可以做到这一点。您只需进行简单的搜索,互联网就会为您返回大多数答案。
如果我没记错的话,Google Chrome 使用了一种称为“命名管道”的技术。
无论如何,我认为最“便携的方式”可能是文件。但如果您知道自己正在使用哪个操作系统,那么您绝对可以使用大多数 IPC 技术。
You are asking the question on Interprocess Communication (IPC).
There are a lot of ways to do that. You can do a simply search and Internet will return you most answers.
If I am not wrong, Google chrome uses a technique called Named Pipe.
Anyway, I think the most "portable way" is probably a file. But if you know you are working on which operating system, you can definitely use most of the IPC techniques.