Why does Eclipse's Photran require 'call flush(6)' after 'write' to prevent buffering to std out?

334 Views Asked by At

I'm using Classic Eclipse 4.2.2 with the Photran plugin and cygwin's gfortran compiler on a Windows 7 Professional machine. If I remove 'call flush(6)' below, the program does not write to console until AFTER reading from std in:

program mult1
implicit none

integer :: i,j,k

!
!
! This program just multiplies two integers
! together.
!
!

write(*,*) 'Enter i,j: '
call flush(6)
read(*,*)i,j

k = i*j

write(*,*) 'The product is ', k
stop
end program mult1

Any thoughts or fixes?

2

There are 2 best solutions below

0
Koenig Cochran On BEST ANSWER

I resolved the issue by preventing ALL buffering. This is not the perfect solution (it would be nice to buffer some outputs and not others), but it solves my problem---namely, not having to call flush(6) every time I write(,).

To prevent all buffering with gfortran (NOTE: It is compiler specific),

  1. Include the environment variable: GFORTRAN_UNBUFFERED_ALL
  2. And assign it the value: 1

In the event that you are USING PHOTRAN (the eclipse plug-in for Fortran), you will have to do the following:

  1. Navigate in overhead bar to Run>Run Configurations
  2. In the left pane, choose your project in the drop down menu Fortran Local Application> Your Project
  3. In the right pane, select the "Environment Tab"
  4. Click "New" and add the variable and value listed above.
2
bob.sacamento On

A FORTRAN code will, unless given explicit direction otherwise, write to output "when it feels like it", or when the system feels like allowing it. It's all about buffering. The call to flush is put in here precisely for that reason: The writer wanted to force an output right there, for obvious reasons. However, I have never seen a code that needed a flush in this context. I would experiment with replacing write(*,*) with print *,.

ANother possibility might be to compile your code so that output buffering is not allowed. Check the man pages for the appropriate flags, if they exist.