0

What exactly is the mechanism by which the linux knows that a file has been closed ? I know commands such as INOTIFY would trigger IN_CLOSE_WRITE event when a file is closed. But how does it work ? What triggers close of a file ?

Similarly how does OS know that a file has been opened and where does it register that fact?

VMAtm
  • 27,943
  • 17
  • 79
  • 125
A J
  • 11
  • 1

2 Answers2

2

The OS (i.e. kernel) is the one that actually opens and closes files. A program will have to tell the OS to open/close files on its behalf every time it wants to do so via system calls. The OS can simply keep track of these calls that go through itself.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • Thanks. Let me ask my specific Q with an example: Say I start an FTP process on a Server A in the /tmp directory. I watch the directory using PYINOTIFY, the IN_CLOSE_WRITE event. When the FTP completes successfully, IN_CLOSE_WRITE gets triggered. But even if the FTP fails midstream for any reason, the IN_CLOSE_WRITE event gets triggered. What is the mechanism by which the OS knows that the FTP process is no longer writing the file (as opposed to a ftp process with high latency that is still writing to the file but with very high latency)? – A J Sep 14 '11 at 21:27
  • @A J: are you sure the FTP server is not manually closing the file after a burst of writes? – Mehrdad Afshari Sep 14 '11 at 21:30
  • Where does the current Linux kernel store the which files are opened? – Martin Thoma Mar 03 '13 at 14:42
  • @moose Global open file table. – Mehrdad Afshari Mar 03 '13 at 22:16
0

There is an open file table that lists all the streams that are open and where they point to in memory.

This may help: http://www.cs.kent.edu/~walker/classes/os.f07/lectures/Walker-11.pdf

Justin
  • 6,373
  • 9
  • 46
  • 72