Why is the close function is called release in `struct file_operations` in the Linux kernel?

后端 未结 3 1388
礼貌的吻别
礼貌的吻别 2021-02-13 12:48

I\'m trying to make a linux kernel module, which supports open, close, read and write operations. So I want to register these functions via struct file_operations, however I can

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-13 13:44

    I had a similar confusion. Perreal is correct in that release is not called when close is called. Here is an extract from the book Linux Device Drivers 3rd edition:

    int (*flush) (struct file *);
    

    The flush operation is invoked when a process closes its copy of a file descriptor for a device; it should execute (and wait for) any outstanding operations on the device. This must not be confused with the fsync operation requested by user programs. Currently, flush is used only in the network file system (NFS) code. If flush is NULL, it is simply not invoked.

    int (*release) (struct inode *, struct file *);
    

    This operation is invoked when the file structure is being released. Like open, release can be missing.

    Note that release isn't invoked every time a process calls close. Whenever a file structure is shared (for example, after a fork or a dup), release won't be invoked until all copies are closed. If you need to flush pending data when any copy is closed, you should implement the flush method.

提交回复
热议问题