public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jb at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libfortran/48587] New: Avoid exhausting unit number with NEWUNIT=
Date: Wed, 13 Apr 2011 07:40:00 -0000	[thread overview]
Message-ID: <bug-48587-4@http.gcc.gnu.org/bugzilla/> (raw)

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48587

           Summary: Avoid exhausting unit number with NEWUNIT=
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: libfortran
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: jb@gcc.gnu.org


As was recently pointed out by Tobias Burnus in a thread on comp.lang.fortran,
the current implementation of NEWUNIT= doesn't reuse unit numbers. Hence it's
possible that a program might exhaust the available unit numbers (this requires
that the program repeatedly closes and reopens files, as OS's have limits on
the number of file descriptors a process may have concurrently opened,
typically 1024 or something like that)

Currently there is just a (mutex protected) static variable which is
decremented for each time an OPEN statement with NEWUNIT= is issued, with a
wraparound check that generates an error if wraparound is detected.

IMHO an elegant solution would be to just reuse the kernel provided file
descriptor. E.g. 

int fd = open(...);
if (fd == -1)
{
    /* Handle error... */
}
new_unit_number = -fd;

This should work because a successful open() will always return a positive fd
number (see
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html ), and
the kernel takes care of reusing file descriptor numbers of closed files.


             reply	other threads:[~2011-04-13  7:40 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-13  7:40 jb at gcc dot gnu.org [this message]
2011-04-13 10:20 ` [Bug libfortran/48587] " burnus at gcc dot gnu.org
2011-04-13 10:47 ` jb at gcc dot gnu.org
2011-04-13 18:59 ` jvdelisle at gcc dot gnu.org
2011-04-14  8:12 ` jb at gcc dot gnu.org
2011-04-15  9:13 ` jb at gcc dot gnu.org
2011-04-19 13:41 ` fxcoudert at gcc dot gnu.org
2014-03-03 22:19 ` jb at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-48587-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).