User Tools

Site Tools


k6:k6.2:k6.2.2:start

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.

  • If the READ keyword is specified, the pipe is opened for reading.
  • If the WRITE keyword is specified, then, the pipe is opened for writing.
  • If the keyword WATCH is specified, the pipe is monitored by the interpreter. If data can be read from the pipe, then the event handler File_Read() is called. If data can be written to the pipe, then the event handler File_Write() is called.
  • If the named pipe was opened successfully, a stream object is returned.

Example:

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

Notes:

  • Gambas implements a named pipe according to the first-in-first-out (FIFO) memory principle.
  • A named pipe is a special file that has a path (name) in the file system. Two processes can connect to this file and send or receive data unidirectionally.
  • Opening a named pipe for reading normally blocks the application until another process opens the same named pipe for writing. In general, a named pipe must be open at both ends at the same time.

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:

  • First, start the programme by opening the executable file read_from_namedpipe.gambas. In the procedure CreateNamedPipe() in lines 20 to 36, a named pipe is created for reading (line 24). However, this blocks the programme because no data can be read from the named pipe.
  • The note 'After a named pipe has been created and opened for reading, the application blocks until data can be read from the named pipe' points out this special feature! You can recognise the blocking, among other things, by the fact that you cannot close the programme.

B2
Figure 6.2.2.1.1: Note

  • Then change to a directory of your choice in your home directory. Open a console there and type ls -l > /tmp/LS2FIFO. This redirects the output of ls -l to the pipe and the named pipe - in the special file LS2FIFO - is where the data is stored.
  • Finally, you will see the data from the first process in the TextArea of the NamedPipeC programme, as data could now be read from the pipe (lines 25 to 29) and the programme no longer blocks:

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

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k6/k6.2/k6.2.2/start.txt · Last modified: 16.01.2022 (external edit)

Page Tools