Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28383 Discussions

Problem with named pipes (fifos) in Fortran

Teuler_J_
Beginner
892 Views

I have just installed the newest Intel Fortran version (version 2016.3.210) under Linux (RHEL 6.7)

In my research activity, I routinely run two programs that communicate through named pipes. But with this version they do not communicate any more ...

I have succeeded in writing a very small case that reproduces the problem. Here it is:

1. Creation of the two named pipes

mkfifo a2b.pipe  b2a.pipe

2. Here is program a

........................................................................................................................................

program test_fifoa

  implicit none

  integer :: i
  integer :: u_a2b = 10, u_b2a = 11

  open(u_a2b, file = 'a2b.pipe', form = 'formatted', status = 'old',          &
    action = 'write')
  open(u_b2a, file = 'b2a.pipe', form = 'formatted', status = 'old',          &
    action = 'read')

  i = 1

  write(u_a2b, *) i
  flush(u_a2b)

  read(u_b2a, *) i
  print *, i

  close(u_a2b)
  close(u_b2a)

end program test_fifoa

........................................................................................................................................

3. And here is program b

........................................................................................................................................

program test_fifob

  implicit none

  integer :: i
  integer :: u_a2b = 10, u_b2a = 11

  open(u_a2b, file = 'a2b.pipe', form = 'formatted', status = 'old',          &
    action = 'read')
  open(u_b2a, file = 'b2a.pipe', form = 'formatted', status = 'old',          &
    action = 'write')

  read(u_a2b, *) i

  i = i + 1

  write(u_b2a, *) i
  flush(u_b2a)

  close(u_a2b)
  close(u_b2a)

end program test_fifob

........................................................................................................................................

4. Here is what happens as seen from a debugger

- Program a writes to a2b.pipe, and returns immediately from the write. So it flushes the unit, and goes to the next part, where it waits for the answer from b

- Program b blocks forever on the reading of a2b.pipe.

- So we have a deadlock ...

5. I have tried to add BUFFERED = 'NO' to the open specifiers, to no avail ...

6. Before this version, I used versions 2011.5.220 and 2013_sp1.2.144 that did not show this problem.

Is it a bug or am I doing something  wrong?

0 Kudos
9 Replies
Kevin_D_Intel
Employee
892 Views

Thanks for reporting this. I will try reproducing the issue and escalate it to our run-time development team for help.

0 Kudos
Teuler_J_
Beginner
892 Views

Dear Kevin,

Any news?

Did you succeed in reproducing this issue?

Best,

0 Kudos
Kevin_D_Intel
Employee
892 Views

Hi Teuler J. – my apologies for the delayed reply. I have reproduced this. I had been testing a number of other compiler releases to determine where exactly the change was introduced and finally determined the changed behavior first appeared with our PSXE 2015 Update 5 (ifort Version 15.0.5.232 Build 20150805) release; thus it is not new with our latest PSXE 2016 Update 3 release. I escalated this to our Fortran run-time team to obtain their help in understanding the cause of the changed behavior. I will let you know what I learn from them.

(Internal tracking id: DPD200411289)

0 Kudos
Teuler_J_
Beginner
892 Views

Hi Kevin,

Thanks for the update.

In the meantime I'll stick to the 2013_sp1.2.144 release ...

Best,

JM

0 Kudos
Kevin_D_Intel
Employee
892 Views

Development identified the root cause and a proposed solution that's being tested for a future release. I'll keep you updated on the availability of the resolution in a future release as I learn it.

0 Kudos
Heath_J_
Beginner
892 Views

Here is another set of test programs that I wrote based on the above - in case it is useful.  After running each, text that you type into test_fifo_swite2 will be written by test_fifo_sread2.  It works as expected using gfortran 5.2, but with Intel Fortran 15.0.6 or 16.0.3, all the output is buffered until the writer closes.  Unless there is some kind of workaround, this problem will prevent me from being able to use the Intel compilers for my project.  Am I correct in understanding that PSXE 2015 Update 3 should work?

Reader:

!  -- Run together with test_fifo_swite2
!  -- Before running: mkfifo a2b.pipe b2a.pipe
      program test_fifo_sread2

      implicit none

      character(40) :: text
      integer :: u_a2b = 10,ios,ict

      write(6,*) '== Reading from a2b.pipe'
      open(u_a2b, file = 'a2b.pipe', form = 'formatted', 
     &     status = 'old', action = 'read', access='stream')

      ict= 0
      do while (.true.)
         read(u_a2b,'(a)',iostat=ios) text
         if (is_iostat_eor(ios)) then
            write (6,*)
            cycle
         else if (is_iostat_end(ios)) then
            write(6,*) 'END reached'
            exit
         else if (ios/=0) then
            write(6,*)
            write(6,*) 'Stopping with ios= ',ios
            exit
         end if
         ict= ict + 1
         write (6,fmt='(i2," : ",a)') ict,trim(text)
         flush(6)
      enddo

      close(u_a2b)

      end program test_fifo_sread2

Writer:

!  -- Run together with test_fifo_sread2
!  -- Before running: mkfifo a2b.pipe b2a.pipe
      program test_fifo_swite2

      implicit none

      integer :: i
      integer :: u_a2b = 10
      character(80) :: text

      open(u_a2b, file = 'a2b.pipe', form = 'formatted',
     &     status = 'old', action = 'write', access='stream')

      write(6,*) '-- Enter some text or "quit" to exit'
      do while (.true.)
         read(5,*) text
         write(u_a2b,'(a)') trim(text)
         flush(u_a2b)
         if (text == "quit") exit
      enddo

      close(u_a2b)

      end program test_fifo_swite2

 

0 Kudos
Kevin_D_Intel
Employee
892 Views

No, the fix for this is not in PSXE 2016 Update 3. It was (just yesterday) approved for the upcoming PSXE 2016 Update 4 in late August. Once its available in our internal-only compiler builds then I will confirm the OP's and your test cases and provide an update. I'm sorry but there is no work around when using the 15.x/16.x compilers.

0 Kudos
jimdempseyatthecove
Honored Contributor III
892 Views

As a hack work-around, what happens if in the writer you:

! *** hack
!         flush(u_a2b)
!
close(u_a2b) ! *** hack
open(u_a2b, file = 'a2b.pipe', form = 'formatted',
     &     status = 'old', action = 'write', access='stream') ! *** hack

That may work until the fix is in.

Jim Dempsey

0 Kudos
jimdempseyatthecove
Honored Contributor III
892 Views

On further inspection, you may need to use status='append'

Your program is not using a named pipe, rather it is using a stream file.

To use a named pipe, you will need to use the USEROPEN specifier and supply a CreateFile function call to open the pipe.

Jim Dempsey

0 Kudos
Reply