与来自 C++ 的脚本进行通信程序

发布于 2024-10-07 18:41:40 字数 592 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丘比特射中我 2024-10-14 18:41:40

当然有一些方法可以在没有中间文件的情况下做到这一点。最常见的方法是使用命令行参数进行输入,使用管道进行标准输出;其他人也使用管道进行输入。 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 use popen.

我爱人 2024-10-14 18:41:40

在类 Unix 系统上?也许 pipe (2) 适合您?

从手册页(Mac OS X 10.5 版本):

概要
#include

int pipeline(int fildes[2]);

描述
pipeline() 函数创建一个管道(一个允许单向的对象)
数据流)并分配一对文件描述符。第一个描述——
tor 连接到管道的读取端;第二个连接到
写完。

当然,您必须使用 forkexec 对来创建管道。也许这已经被详细回答了,现在你知道要搜索什么...


我这样做已经有一段时间了,但是:

  • 在主进程中,在分叉子进程之前,你调用 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):

SYNOPSIS
#include <unistd.h>

int pipe(int fildes[2]);

DESCRIPTION
The pipe() function creates a pipe (an object that allows unidirectional
data flow) and allocates a pair of file descriptors. The first descrip-
tor connects to the read end of the pipe; the second connects to the
write end.

You will, of course, have to follow the creation of the pipes with a fork and exec 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:

  • In the main process, before forking the sub-process you call pipe twice. Now you have two pipes and control both ends of both of them.
  • You fork.
    • The main process will read from one pipe and write from the other. It doesn't matter which is which, but you need to be clear about this.
    • The child process will call one of the exec family of function to replace it's image with that of the shell you want to run but first you will use dup2 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.

北座城市 2024-10-14 18:41:40

您可以使用管道或(也许更方便)套接字 - 例如 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-level fork() and exec().

It's rather complicated so please, be more specific about your environment and what you need to clarify.

弥繁 2024-10-14 18:41:40

您问的是有关进程间通信(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文