删除文件时并发读取时的最佳技术是什么?

huangapple go评论79阅读模式
英文:

What is the best technique to delete a file during concurrent reading to this file?

问题

I need to support a class that has methods like:

  1. int read_file_with_offset(fd, offset)
  2. bool delete_file(fd)

The read API above will be called frequently.

The expected behavior is: when delete API is not called, threads can use the read API concurrently. When the delete API is called, the readers before delete API should still be able to complete successfully, but the readers after the delete API should get an error code such as "inaccessible".

Is the linux file system already able to handle this? Or we have to use some in-memory concurrent data structure to achieve this?

英文:

I need to support a class that has methods like:

  1. int read_file_with_offset(fd, offset)
  2. bool delete_file(fd)

The read API above will be called frequently.

The expected behavior is: when delete API is not called, threads can use the read API concurrently. When the delete API is called, the readers before delete API should still be able to complete successfully, but the readers after the delete API should get an error code such as "inaccessible".

Is the linux file system already able to handle this? Or we have to use some in-memory concurrent data structure to achieve this?

答案1

得分: 1

以下是翻译好的部分:

"在并发读取文件时,删除文件的最佳技术是删除文件。没有其他技术。"

"删除文件只会从目录inode中删除对文件的链接。对任何I/O操作没有影响。打开的文件描述符仍然引用文件,可以继续使用它们没有问题。文件最终在所有引用它的文件描述符关闭时才会被'删除'。通常在创建临时文件后立即打开它,然后使用unlink()删除它。"

"删除API之后的读取操作应该返回"不可访问"等错误代码。"

"在读取之前检查文件是否存在。"

int read_file_with_offset(fd, offset) {
   struct stat stat;
   if (fstatat(fd, &stat) == -1) {
       return inaccessible;
   }
   ...
}

更快的方法是创建一个布尔变量,表示特定的文件描述符是否已被删除。

英文:

> What is the best technique to delete a file during concurrent reading to this file?

The best technique to delete a file during any I/O is to delete the file. There is no other technique.

Deleting a file only removes the link to the file from the directory inode. It has no effect on any I/O operations. Open fd still refer to the file, you can still use them with no problem. The file is finally "removed" when all file descriptors that refer to the file are closed. It is typical to create a temporary file, open it, and unlink() it right after creation.

> the readers after the delete API should get an error code such as "inaccessible".

Check if the file exists before reading.

int read_file_with_offset(fd, offset) {
   struct stat stat;
   if (fstatat(fd, &stat) == -1) {
       return inaccessible;
   }
   ...
}

A faster way would be to just create your own bool variable that signifies that a specific fd has been removed or not.

huangapple
  • 本文由 发表于 2023年5月15日 00:55:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定