In addition to what others said:
Filehandles are a limited resource. Typically, a process is limited to 20 to about 200 file handles, trying to open more handles will result in errors. The reason for this limit is that file handles have a price. The OS needs to allocate some memory for the file meta-data (where on the disk is the file, at what offset will data be read/written, and so on) and some more memory for the file contents. Unwritten data has to be buffered in memory, because disk writes and even flash writes are waaaaay slower than writing to RAM. So every common OS attempts to write only when really needed, preferably without blocking the entire system.
Aborting a program which still has file handles opened is unpredictable, because the libc can also buffer data, in addition to the operating system. Wheather or not the data buffered in the libc ends in the file depends on how the libc is implemented and how the program is aborted. Therefore, the general advice is to keep files opened as short as possible, so that libc and OS can flush their buffers to the disk.
Alexander
In reply to Re: Why do we need to close filehandles?
by afoken
in thread Why do we need to close filehandles?
by tej
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |