独立的 C 模块通过控制模块下的 stdin stdout 协同工作

Independent C modules work together through stdin stdout under a control module

假设我有三个 C 模块:

Is that a possible scenario?

是的。考虑这样一个事实,您可以键入如下命令:cat /dev/urandom | hexdump,它完全按照您的描述执行,其中 cat 是您的 'modules' 之一,而 hexdump 是另一个。在这种情况下,shell 本身就是您的第三个模块,启动其他两个可执行文件并将它们的 io 捆绑在一起。

I just wonder if my above scenario is possible. If possible, what kinds of knowledge does it need?

您可以阅读用于启动进程的 API,尤其是如何配置它们的标准 IO 流。

例如,在 Linux 上,通常的做法是使用 fork() 创建进程,execv() 加载您的 'modules',pipe() 创建进程之间的连接,dup2() 将这些连接放入正确的 (stdio) 位置。

Here's一个例子。

I found that many examples on the Internet is just defining a function outside a C source file and call it in the main function. That's too weak relationship.

并不是说这是相关的,但实际上我认为这种关系比你所说的更牢固。这使用系统链接器将两位代码绑定在一起。唯一更紧密的绑定是如果您不使用函数而只是一起编写代码。