Recent

Author Topic: can't open named pipe with a tfilestream  (Read 803 times)

hakelm

  • Full Member
  • ***
  • Posts: 173
can't open named pipe with a tfilestream
« on: March 29, 2026, 10:31:00 am »
I have a named pipe made with mkfifo /dev/shm/pip and I want to write to it using a filestream, str:tfilestream;.
My program however hangs on str:=tfilestream.Create(fn,fmOpenWrite); without any message or exception.
I have no problem writing to the pipe if I open it as a textfile for writing or with fpsystem('echo "abc" > /dev/shm/pip'); and reading it with for instance cat /dev/shm/pip.
What is it that I haven't understood?
Håkan
on Lazarus 4.6 on FPC 3.2.2 on Ubuntu 22.04
« Last Edit: March 29, 2026, 10:39:17 am by hakelm »

dsiders

  • Hero Member
  • *****
  • Posts: 1635
Re: can't open named pipe with a tfilestream
« Reply #1 on: March 29, 2026, 11:02:13 am »
I have a named pipe made with mkfifo /dev/shm/pip and I want to write to it using a filestream, str:tfilestream;.
My program however hangs on str:=tfilestream.Create(fn,fmOpenWrite); without any message or exception.
I have no problem writing to the pipe if I open it as a textfile for writing or with fpsystem('echo "abc" > /dev/shm/pip'); and reading it with for instance cat /dev/shm/pip.
What is it that I haven't understood?
Håkan
on Lazarus 4.6 on FPC 3.2.2 on Ubuntu 22.04

The fcl-process package has both input and output pipe streams in pipes.pp. Those should do the trick.

hakelm

  • Full Member
  • ***
  • Posts: 173
Re: can't open named pipe with a tfilestream
« Reply #2 on: March 29, 2026, 11:45:45 am »
Thanks, but the pipe is created by another program.
H

Warfley

  • Hero Member
  • *****
  • Posts: 2067
Re: can't open named pipe with a tfilestream
« Reply #3 on: March 29, 2026, 12:00:56 pm »
The reason for that has nothing to do with your Pascal code but with named pipes in general. Opening of a named pipe will block until the pipe is connected, i.e. it will wait until the other process opens the pipe (for reading in that case) as well.

Example:
Create a pipe with:
Code: Bash  [Select][+][-]
  1. > mkfifo pipe.fifo
And run the following program:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.   Classes;
  5.  
  6. var
  7.   s: TFileStream;
  8. begin
  9.   s:=TFileStream.Create('pipe.fifo',fmOpenWrite);
  10.   try
  11.     s.Write('Test'#10, 5);
  12.   finally
  13.     s.Free;
  14.   end;
  15. end.
The program will be stuck and not finish as you describe. Now open a second terminal and read from the pipe:
Code: Bash  [Select][+][-]
  1. > cat pipe.fifo
  2. Test
  3. >
And the Pascal program has finished

hakelm

  • Full Member
  • ***
  • Posts: 173
Re: can't open named pipe with a tfilestream
« Reply #4 on: March 29, 2026, 06:58:55 pm »
Mea culpa.
I thought I could test all pieces i one program. Now I realise I need two programs or at least two threads.
Sorry about bothering you.
Håkan

 

TinyPortal © 2005-2018