OPEN PIPE
Stream = [ OPEN ] PIPE PipeName FOR [ READ ] [ WRITE ] [ WATCH ]
打开一个命名的管道以便只读、只写或读写。如果管道不存在时,会被自动创建。
管道在同一系统的进程之间提供单向传送的通讯。换句话说,管道是半双工的,就是数据仅仅向一个方向传送。
管道的一个主要特征就是通过通讯媒介传送的数据是暂存数据,只能从读描述符读取一次。
同样,如果写连续数据到写描述符,则只能按照数据写入的顺序读取数据。
然而,在对管道进行读取或写入操作之前要两端同时打开。
为了读取合法的数据块,管道保持打开状态,直到其他的进程为了写入而打开这个管道。
自从 3.5
As of Gambas 3.15 pipes
DO NOT block when opening.
(See hint at base of page about backward compatibility and for a workaround for blocking on previous gambas versions)
Gambas实现FIFO(先进先出)管道。更多的信息请看Linux/Unix手册中的MKFIFO部分。
-
指定
READ
关键字,为读取打开管道。
-
指定
WRITE
关键字,为写入打开管道。
-
指定
WATCH
关键字,解释器将监视管道。
-
如果从管道中读取至少一个字节,那么
File_Read()
事件处理会被调用。
-
如果向管道中写入至少一个字节,那么
File_Write()
事件处理会被调用。
如果管道被成功打开,一个流对象被返回到
hStream 变量。
Pipe streams are never buffered.
自从 3.5
The
OPEN PIPE
syntax is available since Gambas 3.5.
If no process opens the pipe for writing, then any program using GTK+ and watching the pipe for reading will start to busy loop, eating 100% CPU.
This is a GTK+ feature.
The only workaround is opening the pipe both for reading and writing in the process that maintains it, so that there is always at least one writer.
自从 3.17
Since Gambas 3.17, pipes are always internally open for writing to workaround the problem. Beware that you still can write on the pipe only if you explicitly specify the
WRITE
flag.
Errors
Message
|
Description
|
禁止访问(43)
|
不允许对管道的访问请求,或者路径中的某个目录的路径检索许可被拒绝,或者不允许对其父目录写访问。
|
设备已满(37)
|
File name was to be created but the device containing Pipe name has no room for the new file.
|
非目录(49)
|
A component used as a directory in Pipe name is not, in fact, a directory.
|
系统错误... (42)
|
其它可能的系统错误:
-
解析路径时遇到太多的符号连接。
-
路径参数超过最大路径长度,或者路径的某个目录超过目录名称最大长度。
-
路径不能命名一个存在的目录,或者路径不能是空字符串。
-
容纳新文件的目录不能扩展,或者文件系统缺乏文件分配资源。
-
达到系统打开文件总数限制。
-
Pipe name 指向只读文件系统上的文件,并且有写访问请求。
|
Examples
' Prints the messages sent to a pipe
' Start that running in one window then do : ls > /tmp/FIFO1
Dim hFile AS File
Dim sLine AS String
hFile = Pipe "/tmp/FIFO1" For Read
Do
Read #hFile, sLine, -256
If Not sLine Then Break
Print sLine;
Loop
' Another way of reading a pipe if you know that the data is a bunch of text lines
Dim hFile As File
Dim sLine As String
hFile = Pipe "/tmp/FIFO1" For Read
Do
Line Input #hFile, sLine
If hFile.EndOfFile Then Break
Print sLine
Loop
See also
Hint:
In versions of Gambas below V 3.15 opening the pipe even for
Watch would use the Linux default behaviour of locking your program until the pipe received some text.
A simple fix for this was to '
echo' something to the pipe using the
Shell command just before opening like this...
Shell "echo '' >/tmp/fifo1"
hFile = Pipe "/tmp/fifo1" For Watch
Then opening the pipe would instantly receive some text and your program would continue to run.
As from Gambas V3.15+
Opening a pipe no longer locks the program waiting for text to be received as this behaviour was not considered useful.
So using the
echo command is no longer needed but for backward compatibility (If your app is intended for others) it is advisable to still use it as your program will work okay on Gambas V3.15 without it but will lock on a lesser version runtime.