如何使用管道在两个程序之间发送简单的字符串?
我尝试在网上搜索,但几乎没有任何资源。一个小例子就足够了。
编辑 我的意思是,两个不同的 C 程序相互通信。一个程序应该发送“Hi”,另一个程序应该接收它。类似的事情。
I tried searching on the net, but there are hardly any resources. A small example would suffice.
EDIT
I mean, two different C programs communicating with each other. One program should send "Hi" and the other should receive it. Something like that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
常规管道只能连接两个相关的进程。它是由一个进程创建的,当最后一个进程关闭它时就会消失。
命名管道,因其行为也称为 FIFO,可以是用于连接两个不相关的进程,独立于进程而存在;这意味着即使没有人使用它,它也可以存在。 FIFO 是使用
mkfifo()
库函数。示例
writer.c
reader.c
注意:为简单起见,上述代码中省略了错误检查。
A regular pipe can only connect two related processes. It is created by a process and will vanish when the last process closes it.
A named pipe, also called a FIFO for its behavior, can be used to connect two unrelated processes and exists independently of the processes; meaning it can exist even if no one is using it. A FIFO is created using the
mkfifo()
library function.Example
writer.c
reader.c
Note: Error checking was omitted from the above code for simplicity.
从在 C 中创建管道中,这向您展示了如何派生程序以使用管道。如果您不想 fork(),可以使用命名管道。
另外还可以得到
prog1 | 的效果prog2
通过将prog1
的输出发送到 stdout 并从prog2
中的stdin
读取。您还可以通过打开名为/dev/stdin
的文件来读取 stdin(但不确定其可移植性)。From Creating Pipes in C, this shows you how to fork a program to use a pipe. If you don't want to fork(), you can use named pipes.
In addition, you can get the effect of
prog1 | prog2
by sending output ofprog1
to stdout and reading fromstdin
inprog2
. You can also read stdin by opening a file named/dev/stdin
(but not sure of the portability of that).并阅读:
但是,我认为
fcntl
可以是更好的解决方案And read:
But, I think that
fcntl
can be a better solution一个程序写入 stdout 的内容可以由另一个程序通过 stdin 读取。简单来说,使用 c 编写
prog1
使用printf()
打印某些内容,使用prog2
使用scanf()
读取某些内容代码>.然后运行What one program writes to stdout can be read by another via stdin. So simply, using c, write
prog1
to print something usingprintf()
andprog2
to read something usingscanf()
. Then just run这是一个示例:
此程序中的重要步骤是:
popen ()
调用,它在子进程和父进程中的管道之间建立关联。fprintf()
调用将管道用作普通文件来写入子进程的 stdin 或从其 stdout 读取。pclose()
调用关闭管道并导致子进程终止。Here's a sample:
The important steps in this program are:
popen()
call which establishes the association between a child process and a pipe in the parent.fprintf()
call that uses the pipe as an ordinary file to write to the child process's stdin or read from its stdout.pclose()
call that closes the pipe and causes the child process to terminate.这个答案可能对未来的 Google 员工有所帮助。
您可以在此处。
This answer might be helpful for a future Googler.
You can find an advanced two-way pipe call example here.
首先,让程序 1 将字符串写入
stdout
(就好像您希望它出现在屏幕上一样)。然后第二个程序应该从stdin
读取字符串,就像用户从键盘输入一样。然后你运行:First, have program 1 write the string to
stdout
(as if you'd like it to appear in screen). Then the second program should read a string fromstdin
, as if a user was typing from a keyboard. then you run:如果要启用两个进程之间的通信,则必须确定是否要分叉这些进程。如果您要分叉它们,则可以使用匿名管道。否则,您可以使用命名管道。
匿名管道
假设您在
fork()
之前声明了管道,那么子进程将继承父进程的管道,两者都指向管道的同一端。您可以利用这一事实在进程之间发送数据。例如,在以下程序中,父进程将字符串
Hello World!
发送到子进程,然后子进程显示它。命名管道
通过命名管道,您可以使用文件在不相关的进程之间传输数据。
您可以使用
mkfifo
在 shell 中创建命名管道。或者,您可以使用库函数mkfifo()
在 C 程序中创建它;您必须首先包含
和
库。交互式创建命名管道
我们在 shell 中创建命名管道
pipefd
:我们按如下方式实现发送方进程:
我们按如下方式实现接收方进程:
我们编译两个程序:
并运行它们:
如输出所示,父进程将字符串发送给子进程,然后子进程显示它。
If you want to enable communication between two processes, you must determine if you're forking those processes or not. If you're forking them, then you can make use of anonymous pipes. Otherwise, you can use named pipes.
Anonymous Pipes
Assuming you've declared the pipe before
fork()
, then a child process will inherit the parent process's pipe, both of which points to the same ends of the pipe. You can use this fact to send data between the processes.For example, in the following program the parent process sends the string
Hello World!
to the child process, which then displays it.Named Pipes
With a named pipe, you're using a file to transfer data between unrelated processes.
You can create a named pipe in the shell using
mkfifo
. Alternatively you can create it within a C program using the library functionmkfifo()
; you must include the<sys/types.h>
and<sys/stat.h>
libraries first.Named pipe created interactively
We create the named pipe
pipefd
in the shell:We implement the sender process as follows:
We implement the receiver process as follows:
We compile both programs:
and run them:
As the output shows, the parent process sent the string to the child process which then displayed it.