Pipe定义

本文详细介绍了 Java NIO 中 Pipe 的工作原理及其内部结构。Pipe 包含两个通道:SinkChannel 和 SourceChannel,分别用于写入和读取字节序列。文章还解释了这两个通道如何实现单向数据传输,并探讨了它们的支持操作。
Channel接口定义:[url]http://donald-draper.iteye.com/blog/2369111[/url]
AbstractInterruptibleChannel接口定义:[url]http://donald-draper.iteye.com/blog/2369238[/url]
SelectableChannel接口定义:[url]http://donald-draper.iteye.com/blog/2369317[/url]
SelectionKey定义:[url]http://donald-draper.iteye.com/blog/2369499[/url]
SelectorProvider定义:[url]http://donald-draper.iteye.com/blog/2369615[/url]
AbstractSelectableChannel定义:[url]http://donald-draper.iteye.com/blog/2369742[/url]
NetworkChannel接口定义:[url]http://donald-draper.iteye.com/blog/2369773[/url]
Selector定义:[url]http://donald-draper.iteye.com/blog/2370015[/url]
AbstractSelector定义:[url]http://donald-draper.iteye.com/blog/2370138[/url]
SelectorImpl分析 :[url]http://donald-draper.iteye.com/blog/2370519[/url]
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper):
[url]http://donald-draper.iteye.com/blog/2370811[/url]
WindowsSelectorImpl解析二(选择操作,通道注册,通道反注册,选择器关闭等):
[url]http://donald-draper.iteye.com/blog/2370862[/url]
ByteChannel,分散聚集通道接口的定义(SocketChannel):
[url]http://donald-draper.iteye.com/blog/2371065[/url]
NIO-Pipe示例:[url]http://donald-draper.iteye.com/blog/2373535[/url]
前面一篇文章我们看了一个管道实例,今天来看一下管道的定义
package java.nio.channels;
import java.io.IOException;
import java.nio.channels.spi.*;


/**
* A pair of channels that implements a unidirectional pipe.
*Pipe是一对单向通道的实现
* A pipe consists of a pair of channels: A writable {@link
* Pipe.SinkChannel </code>sink<code>} channel and a readable {@link
* Pipe.SourceChannel </code>source<code>} channel. Once some bytes are
* written to the sink channel they can be read from source channel in exactly
* the order in which they were written.
*管道Pipe包括一对通道,一个可写的SinkChannel,一个可读的SourceChannel。只要有字节序列
写到sink通道,那么source通道可以按字节写的顺序读取字节序列。
* Whether or not a thread writing bytes to a pipe will block until another
* thread reads those bytes, or some previously-written bytes, from the pipe is
* system-dependent and therefore unspecified. Many pipe implementations will
* buffer up to a certain number of bytes between the sink and source channels,
* but such buffering should not be assumed.

*一个线程是否可以写字节序列到管道,取决于是否有其他线程从依赖于系统的管道,
读取这些字节序列或先前已写到管道的字节序列,因此是不确定的。许多管道的实现将会
把一定数量的字节序列在sink与source通道之前缓存起来,但这种缓存不应该assumed。
*
* @author Mark Reinhold
* @author JSR-51 Expert Group
* @since 1.4
*/

public abstract class Pipe {

/**
* A channel representing the readable end of a {@link Pipe}.
*SourceChannel表示管道的可读端
* @since 1.4
*/
public static abstract class SourceChannel
extends AbstractSelectableChannel
implements ReadableByteChannel, ScatteringByteChannel
{
/**
* Constructs a new instance of this class.
*/
protected SourceChannel(SelectorProvider provider) {
super(provider);
}

/**
* Returns an operation set identifying this channel's supported
* operations.
*返回通道支持的操作事件
* Pipe-source channels only support reading, so this method
* returns {@link SelectionKey#OP_READ}.

*由于管道的Source通道只支持读操作,所以此方只返回SelectionKey#OP_READ
* @return The valid-operation set
*/
public final int validOps() {
return SelectionKey.OP_READ;
}

}

/**
* A channel representing the writable end of a {@link Pipe}. </p>
* SinkChannel表示管道的可写端
* @since 1.4
*/
public static abstract class SinkChannel
extends AbstractSelectableChannel
implements WritableByteChannel, GatheringByteChannel
{
/**
* Initializes a new instance of this class.
*/
protected SinkChannel(SelectorProvider provider) {
super(provider);
}

/**
* Returns an operation set identifying this channel's supported
* operations.
*返回通道支持的操作事件
* Pipe-sink channels only support writing, so this method returns
* {@link SelectionKey#OP_WRITE}.

*由于管道的Sink通道只支持写操作,所以此方只返回SelectionKey#OP_WRITE
* @return The valid-operation set
*/
public final int validOps() {
return SelectionKey.OP_WRITE;
}

}

/**
* Initializes a new instance of this class.
*/
protected Pipe() { }

/**
* Returns this pipe's source channel. </p>
*获取管道的source通道
* @return This pipe's source channel
*/
public abstract SourceChannel source();

/**
* Returns this pipe's sink channel. </p>
*获取管道的sink通道
* @return This pipe's sink channel
*/
public abstract SinkChannel sink();

/**
* Opens a pipe.
*打开一个管道
* The new pipe is created by invoking the {@link
* java.nio.channels.spi.SelectorProvider#openPipe openPipe} method of the
* system-wide default {@link java.nio.channels.spi.SelectorProvider}
* object.

*
* @return A new pipe
*
* @throws IOException
* If an I/O error occurs
*/
public static Pipe open() throws IOException {
return SelectorProvider.provider().openPipe();
}
}

[size=medium][b]总结:[/b][/size]
Pipe中包含一个可写通道SinkChannel和一个可读通道SourceChannel。sink向管道写字节序序列,source从管道读取字节序列。
PipeImpl解析:[url]http://donald-draper.iteye.com/blog/2373628[/url]
03-11
### 关于 Pipe 在操作系统和编程中的用法及实现 #### 定义与基本功能 Pipe 是一种用于进程间通信(IPC)的方法,在 Linux 和 Unix 类系统中广泛应用。它允许一个进程的数据输出被另一个进程作为输入来处理,形成所谓的管道通信[^1]。 #### 工作机制 当创建了一个 pipe 后,实际上是在内存中开辟了一块缓冲区用来暂存数据。向管道(共享文件)提供输入的发送进程(即写进程)以字符流形式将大量数据送入管道;而接收管道输出的接收进程(即读进程)则从管道中接收(读)这些数据。因为发送进程和接收进程都是利用同一个管道来进行消息传递,所以这种通信方式被称为管道通信[^3]。 #### 函数调用 `pipe()` 为了建立一条匿名管道,可以使用 C 库提供的 `pipe()` 系统调用。此函数会返回两个文件描述符——fd[0] 代表读端口,fd[1] 表示写端口。通常情况下,子进程中关闭不需要的那个方向上的描述符,只保留需要用到的那一方。 ```c #include <unistd.h> int pipe(int fd[2]); ``` 如果成功,则返回 0 并设置数组 `fd[]` 中的第一个成员为读取端点,第二个成员为写入端点;如果有错,则返回 -1,并设定 errno 变量指出具体原因。 #### 使用场景举例 假设有一个父进程想要启动多个子进程并让它们之间能够相互通信,那么就可以先由父进程调用一次 `pipe()` 来获取一对连接两端的新文件描述符,之后再通过 fork 创建新的子进程。这样做的好处在于父子之间的联系更加紧密,而且可以通过简单的 read/write 操作完成复杂的任务协调工作。 #### 注意事项 - 当任意一方关闭了对应的文件描述符后,另一方继续尝试操作该已关闭的方向将会触发 SIGPIPE 信号,默认行为是终止当前程序执行。 - 如果读者全部退出但仍有未读取的信息存在于 buffer 内部的话,后续任何试图往里面追加新内容的行为都会阻塞直到有足够的空间可用为止。 - 对于无名管道而言,只有具有共同祖先关系的一组进程才能访问到相同的实例对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值