File System Change Notification Mechanism in Linux2.6 Kernel

Source: Internet
Author: User
Article Title: file system change notification mechanism in Linux2.6 kernel. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

I. Introduction

As we all know, Linux desktop systems are much less satisfactory than MAC or Windows. To improve this situation, the open-source community proposes that the kernel should provide some mechanisms for user States, in this way, the user State can know in time what happened to the kernel or underlying hardware device, so as to better manage the device and provide users with better services, such as hotplug, udev, and inotify. Hotplug is a kernel mechanism for notifying user-Mode Applications About some events of hot swapping devices. The desktop system can use it to effectively manage devices, inotify is a file system change notification mechanism that allows you to dynamically maintain Device Files under/dev. Events such as file addition and deletion can be immediately known to users, this mechanism was introduced by beagle, a famous Desktop Search Engine Project, and applied to projects such as Gamin.

In fact, there is a similar mechanism named dnotify before inotify, but it has many defects:

1. for each directory to be monitored, you need to open a file descriptor. Therefore, if there are many directories to be monitored, many file descriptors will be opened, especially, if the monitored directory is on a mobile media (such as a disc or USB disk), the file systems such as umount will not be available because the file descriptor opened by the application using dnotify is using the file system.

2. dnotify is directory-based. It can only get Directory change events. Of course, changes to files in the directory will affect the directory where it is located and cause Directory change events, however, to learn which file is changed through directory events, You Need To Cache a lot of data in the stat structure.

3. The Dnotify interface is unfriendly. It uses signal.

Inotify is designed to replace dnotify. It overcomes the defects of dnotify and provides a simpler and powerful file change notification mechanism:

1. inotify does not need to open a file descriptor for the monitored target. If the monitored target is on removable media, after the file system on the umount media, the watch corresponding to the monitored target will be automatically deleted and an umount event will be generated.

2. Inotify can monitor both files and directories.

3. Inotify uses system calls instead of SIGIO to notify file system events.

4. Inotify uses the file descriptor as an interface. Therefore, you can use the normal file I/O operations select and poll to monitor changes in the file system.

Inotify can monitor the following file system events:

IN_ACCESS: The file is accessed by IN_MODIFY, the file is written by IN_ATTRIB, and the file attributes are modified, such as chmod, chown, touch, and other IN_CLOSE_WRITE. The writable file is closed by IN_CLOSE_NOWRITE, the file is opened IN_MOVED_FROM, and the file is removed, such as mv IN_MOVED_TO. The file is moved, such as mv and cp IN_CREATE. The new file IN_DELETE is created, and the file is deleted, such as rm IN_DELETE_SELF, that is, when an executable file is executed, it deletes itself IN_MOVE_SELF and moves itself. That is, when an executable file is executed, it moves itself IN_UNMOUNT, the host file system is umount IN_CLOSE, and the file is closed, equivalent to (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)

IN_MOVE: The file is moved, equivalent to (IN_MOVED_FROM | IN_MOVED_TO)

Note: The files mentioned above also include directories.

[1] [2] [3] [4] [5] Next page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.