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

O_CLOEXEC on OPEN intrinsic

ereisch
New Contributor II
241 Views

Does the Fortran standard say anything about setting the (Linux) O_CLOEXEC flag on files that are opened via the OPEN() intrinsic?  It appears as though this flag is not set for any such calls when I do a "strace" on one of my Fortran programs.  GNU gfortran appears to set that bit, but there's no citation as to whether that's a "just makes sense" change, or if there is a part of the standard that requires it.  I would be surprised if it did though, because I don't believe Windows has that capability.

It would seem as though setting this flag would be something you'd want to do with Fortran, since there's no real easy way of passing I/O unit numbers across an exec() call (unless they were in the program arguments, but even calling exec() directly from Fortran seems overly onerous).  Perhaps a compiler switch to alter this functionality in the RTL?

0 Kudos
4 Replies
Steve_Lionel
Black Belt Retired Employee
228 Views

I was unfamiliar with this option, but even after reading a description it's not clear to me. The reading I did suggested that the file was closed as soon as you did an exec(). I can't quite imagine why one would want that as a default in Fortran, but perhaps I misunderstand it. Perhaps you can set this in a USEROPEN routine?

ereisch
New Contributor II
219 Views
The default on Linux is to preserve file descriptors across an exec(). From the execve() manpage:
"By default, file descriptors remain open across an execve(). File descriptors that are marked close-on-exec are closed; see the description of FD_CLOEXEC in fcntl(2)."

I would expect, as you note, with Fortran there would be no need to preserve these file descriptors across the exec. But it seems as though the current behavior is to leave them open in the new process space.
ereisch
New Contributor II
218 Views
P.S., the file would remain open in the "parent" process space, but would be closed in the child's (if O_CLOEXEC is asserted). If it isn't (the default), it would remain open with the same file descriptor *in both process spaces*.
Steve_Lionel
Black Belt Retired Employee
202 Views

I would guess that the issue never came to the attention of the Intel developers.  If you have support, you could file a feature request.

Reply