与 Linux 进程进行可靠的双向通信?

发布于 2024-08-11 00:30:20 字数 256 浏览 1 评论 0原文

实现与 Linux 进程的双向通信的可靠方法是什么?

我看到 popen 似乎不支持“r”和“w”访问同时...或者至少这就是所暗示的:

类型参数是一个指向以 null 结尾的字符串的指针,该字符串必须是“r”(用于读取)或“w”(用于写入)。

(我现在很想念Erlang)

What is the reliable way of implementing bidirectional communication to a Linux process?

I see that popen does not seem to support "r" and "w" access at the same time... or at least that's what is implied:

The type argument is a pointer to a null-terminated string which must be either 'r' for reading or 'w' for writing.

(I am so missing Erlang at the moment)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

可可 2024-08-18 00:30:20

Unix 域套接字是你的朋友。

Unix 域套接字或 IPC 套接字
(进程间通信套接字)
是一个数据通信端点
类似于互联网套接字,但是
不使用网络协议
沟通。它用于 POSIX
进程间操作系统
沟通。

您为通信通道保留一个名称,例如 /myapp/ipc,然后两个进程都使用 UNIX 套接字打开该地址:

struct sockaddr_un local;
int len;

s = socket(AF_UNIX, SOCK_STREAM, 0);
local.sun_family = AF_UNIX;
strcpy(local.sun_path, "/myapp/ipc");
len = strlen(local.sun_path) + sizeof(local.sun_family);
bind(s, (struct sockaddr *)&local, len);

现在您可以使用 listenconnect 或套接字系列中的其他内容。这需要一点工作,但却是在 Linux 上实现 IPC 的最佳方法。

由于 Erlang 是一种很好的语言,用于指定通过命名管道(进程)进行通信的小型服务器(进程),因此该模型应该会让您感到舒服。

Unix domain sockets are your friend.

A Unix domain socket or IPC socket
(inter-process communication socket)
is a data communications endpoint that
is similar to an Internet socket, but
does not use a network protocol for
communication. It is used in POSIX
operating systems for inter-process
communication.

You reserve a name for your communications channel, such as /myapp/ipc, and then both processes open that address using a UNIX socket:

struct sockaddr_un local;
int len;

s = socket(AF_UNIX, SOCK_STREAM, 0);
local.sun_family = AF_UNIX;
strcpy(local.sun_path, "/myapp/ipc");
len = strlen(local.sun_path) + sizeof(local.sun_family);
bind(s, (struct sockaddr *)&local, len);

Now you can use listen or connect or whatever else in the socket family. It's a little bit of work, but is the best way to achieve IPC on Linux.

Since Erlang is just a nice language for specifying little servers (processes) that communicate over named pipes (processes), this model should feel comfortable to you.

兮子 2024-08-18 00:30:20

良好的旧 TCP/IP 连接对我来说一直很有效。

Good old TCP/IP connections have always worked well for me.

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