What if a file is not closed (Using $fclose)?

Guest
I have opened a file in verilog using $fopen. Test case writes log
information in to it.
At the end of the test, i am not closing the log file with $fclose
system call.
Is it a problem? - I never faced any issue with it so far.
If this is not gonna be an issue, why should we need $fclose?


Thanks,
Muthu
 
Hi Muthu,
It is true that during a final exit, a well written simulator
shall close all opened files and hence you won't see a need for
explicit $fclose. However, don't take it for granted. Consider the
following sample arguments:

1. Your sim takes 4 hours and after 3.45 hours for some reason the sim
crashed. All information to the file of interest were already written
(an assumption, not necessarily valid in all scenarios though). Doing
an explicit $fclose would have saved all data, a crash typically
doesn't do that. You loose 3.45 hours of sim run.

2. Portability across platforms, tools etc. - say your vendor ported
their simulator to AMD-Opteron, it *may* or *may not* work out of the
box for your implicit $fclose. A different simulator may not have such
clean code. For example, I've seen it in the past that with C-code,
without fclose (or equivalent in C) files tend to remain
empty/half-empty etc.

3. In general, it is a good programming style to close what you opened.
Same is true in C etc.

4. Let' say that you have sims running for more than a day, your design
is processing some images etc. It will be very handy to run a
post-process script from terminal on the images captured on a periodic
interval. Doing an explicit $fclose helps in such cases.

Finally, let me ask the same question the other way around - why don't
you close the files you explicity opened? Sure it is not too much of a
code.

Regards
Ajeetha, CVC
www.noveldv.com
 
Other reasons:

Verilog-1995 only supported about 30 open files, because the
multi-channel descriptors used a separate bit in an integer for each
file. If you wanted to open another file after that, you would have to
close one of the currently open ones.

Verilog-2001 file descriptors remove this inherent restriction of MCDs,
but are still subject to any limits on open files that are imposed by
the underlying operating system or I/O libraries. While a limit such
as 255 open files is adequate for most purposes, there might still be
times when you need to close some so that you can open others. And
even if the system supports a large number of open files, I/O
performance may suffer due to the mechanisms used to do this.
 

Welcome to EDABoard.com

Sponsor

Back
Top