Table of Contents

6.2.2 Named Pipe

A named pipe provides a unidirectional communication channel (half-duplex) between processes within the same system. It is a key feature of a named pipe that the flowing data is transient. Once data has been read, it cannot be read again afterwards.

hNamedPipe = PIPE sNamedPipePath FOR [ READ ] [ WRITE ] [ WATCH ]
hNamedPipe = OPEN PIPE sNamedPipePath FOR [ READ ] [ WRITE ] [ WATCH ]

The two equivalent statements open a named pipe for reading, writing or both. If the pipe does not exist, it will be created automatically.

Example:

Public hNamedPipe As File
Public sNamedPipePath As String
 
Public Sub Form_Open()
  sNamedPipePath = Temp("FIFO1")
  hNamedPipe = Pipe sNamedPipePath For Read Watch
  ...
End

Notes:

B1
Figure 6.2.2.1: Note

You already know how pipes work from the sequential processing of commands in a console. The output of the command ls in the following example is a list of all files in the folder BildTon/Flumen including all subfolders (-R), which is used as input for the command grep. The grep command counts the number of png and PNG files - specified via the options -i and -c and the filter .png. For this to work, there is a pipe symbol '|' between the two commands:

hans@mint-183 ~ $ ls -R ~/BildTon/Blumen | grep -ic '\.png'
78

6.2.2.1 Project

The following project NamedPipeC represents a named pipe as an object that reads and processes data from the output of a process in a console as input - similar to the example above.

Der Quelltext wird vollständig angegeben:
 
[1] ' Gambas class file
[2]
[3] Public sNamedPipePath As String
[4] Public hNamedPipe As File
[5]
[6] Public Sub Form_Open()
[7]   FMain.Resizable = True
[8]   sNamedPipePath = "/tmp/LS2FIFO"
[9]   If Exist(sNamedPipePath) Then Kill sNamedPipePath
[10] End
[11]
[12] Public Sub btnCreateNamedPipe_Click()
[13]   txaOutput.Clear()
[14]   btnCreateNamedPipe.Picture = Picture["icon:/16/apply"]
[15]   btnCreateNamedPipe.Text = "Named Pipe was created and waits for data to read ..."
[16]   Wait
[17]   CreateNamedPipe()
[18] End
[19]
[20] Public Sub CreateNamedPipe()
[21]
[22]   Dim sLine As String
[23]
[24]   hNamedPipe = Open Pipe sNamedPipePath For Read
[25]   Do
[26]     Read #hNamedPipe, sLine, -256
[27]     If Not sLine Then Break
[28]     txaOutput.Insert(sLine)
[29]   Loop
[30]   If Exist(sNamedPipePath) Then Kill sNamedPipePath
[31]   btnCreateNamedPipe.Picture = Picture["icon:/16/package"]
[32]   btnCreateNamedPipe.Text = "Create Named Pipe"
[33]   If hNamedPipe Then Close hNamedPipe
[34] End
[35]
[36] Public Sub btnGetInformation_Click()
[37]   Message.Info("After a named pipe has been created and opened for reading, the application blocks until data can be read from the named pipe.")
[38] End
[39]
[40] Public Sub Form_Close()
[41]   If Exist(sNamedPipePath) Then Kill sNamedPipePath
[42] End

Notes:

B2
Figure 6.2.2.1.1: Note

B3
Figure 6.2.2.1.2: Displaying the data from the Named Pipe

A named pipe is a volatile FIFO memory, therefore it is empty after reading data!

Download

Project

Download