Question

If 2 file descriptors were dupped to the same file (i.e. 506 and STDOUT), will invoking close(506) cleanup the object associated by both and render STDOUT unusable? Or does kernel implement reference counting for its files?

Was it helpful?

Solution

The kernel implements reference counting, so the kernel object is not closed until all the file handles pointing to it are closed.

OTHER TIPS

Reference counters are widely used inside the kernel to avoid race conditions due to the concurrent allocation and releasing of a resource. A reference counter is just an atomic_t counter associated with a specific resource such as a memory page, a module, or a file. The counter is atomically increased when a kernel control path starts using the resource, and it is decreased when a kernel control path finishes using the resource. When the reference counter becomes zero, the resource is not being used, and it can be released if necessary.

You might care to see this if you want to look through this for an overview of Linux Kernel reference counting implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow