Why close file descriptor




















The close function shall deallocate the file descriptor indicated by fildes. To deallocate means to make the file descriptor available for return by subsequent calls to open or other functions that allocate file descriptors. All outstanding record locks owned by the process on the file associated with the file descriptor shall be removed that is, unlocked. If close is interrupted by a signal that is to be caught, it shall return -1 with errno set to [EINTR] and the state of fildes is unspecified.

When all file descriptors associated with an open file description have been closed, the open file description shall be freed. If the named end has no open file descriptors associated with it and gets detached, the STREAM associated with that end shall also be dismantled. It is unspecified whether closing the master side of the pseudo-terminal flushes all queued input and output. All operations that are not canceled shall complete as if the close blocked until the operations completed. If a memory mapped file [ SHM ] or a shared memory object remains referenced at the last close that is, a process has it mapped , then the entire contents of the memory object shall persist until the memory object becomes unreferenced.

If this is the last close of a memory mapped file [ SHM ] or a shared memory object and the close results in the memory object becoming unreferenced, and the memory object has been unlinked, then the memory object shall be removed.

If fildes refers to a socket, close shall cause the socket to be destroyed. Upon successful completion, 0 shall be returned; otherwise, -1 shall be returned and errno set to indicate the error. There is an "open file description" that is reference counted. And then there is the file handle, which is not reference counted. The file structure points the way to the inode itself. The open file description contains things like the open mode and file pointer. When you call close, you always close the file handle.

When a file handle is closed, the reference count on its open file description is decremented. If it goes to zero, the open file description is also released and the reference count on the file itself is decremented.

Only if that goes to zero is the kernel's file structure freed. There is no chance for one process to release a resource another process is using because shared resources are reference counted. In this case not a lot will happen. The reference counter for the file descriptor will be decremented by one. The same file descriptor is usually held by the shell from which the program was run, so the file descriptor needs to be kept.

The kernel keeps reference counts for all files inodes that are open. As long as the reference count is greater than zero the file will be kept. I would expect a separate counter is kept for open file handles. Once this hits zero the kernel can release the memory used by the file handle.

When all references to the file directory entries and file handles have been removed, the file system code will mark the inode for reuse. Any blocks the file has are made available for allocation. Many file systems will clear the block pointers in the inode when it is released. The close function deallocates the file specified descriptor.

To deallocate means to make the file descriptor available for subsequent calls to open or other functions that allocate file descriptors.



0コメント

  • 1000 / 1000