当其他进程在 *nix 系统上写入文件时,无法追加到文件

发布于 2024-09-27 15:16:38 字数 1166 浏览 7 评论 0原文

我有一段非常简单的代码,它只是定期将少量数据写入文件。一旦我的程序创建了文件并附加了一些数据,当我在 vim(或任何其他编辑器)中打开该文件并编辑它时,我的进程似乎无法再更新该文件。我没有看到系统调用返回任何错误。我尝试跟踪系统调用,即使文件没有更新,也没有观察到任何奇怪的情况。

由于每个进程都有自己的文件表条目,该条目具有当前偏移量,因此我所期望的只是一个输出文件,其中的数据散布着来自两个非合作进程的写入(也可能是乱码)。但我观察到的是,一旦任何其他编辑器写入该文件,我的程序就无法再更新该文件。

其他一些有趣的观察结果

1)当我将某些内容写入输出文件时,我的程序可以继续更新,没有问题

2)当我自己的程序的多个实例写入同一个文件时,一切都很好

我知道存在强制锁定以防止多次写入,但我试图了解下面发生的事情。此外,这种情况对于某些记录器(如系统日志、apache 日志等)来说表现正常,

有什么想法可以解释这种行为吗?还有关于如何进一步调试这个的任何提示吗?

我的代码非常简单:

  1 int main(int argc, char** argv)
  2 {
  3     const char* buf;
  4     if(argc < 2)
  5         buf = "test->";
  6     else
  7         buf = argv[1];
  8 
  9     int fd; 
 10     if((fd = open("test.log", O_CREAT|O_WRONLY|O_APPEND, 0644)) == -1) {
 11         perror("Cannot open test.log");
 12         exit(1);
 13     }   
 14 
 15     int num_bytes = strlen(buf), num_bytes_written = -1; 
 16 
 17     while(1) {
 18         if((num_bytes_written = write(fd, buf, num_bytes)) == -1) {
 19             perror("Could not write to fd");
 20         }   
 21         fsync(fd);
 22         sleep(5);
 23     }   
 24 }   

I have a very simple piece of code which just writes a small amount of data to a file at some regular interval. Once my program has created the file and appended some data, when I open this file in vim(or any other editor for that matter) and edit it, my process cannot seem to update the file anymore. I do not see any errors being returned from the syscall. I tried tracing the system calls, and did not observe anything weird even while the file is NOT being updated.

Since each process gets its own file table entry which has the current offset, all I was expecting was an output file with data interspersed with writes from the two non-cooperating processes(possibly garbled too). But what I am observing is that my program cannot update the file anymore once any other editor writes to the file.

Couple of other interesting observations

1) When I cat something to the output file, my program can continue to update no problem

2) When multiple instances of my own program are writing to the same file, everything is fine again

I understand that there's mandatory locking to prevent multiple writes, but I am trying to understand whats happening underneath. Also this kind of scenario behaves normally for some loggers (like system log, apache logs etc)

Any ideas to explain this behavior?. Also any hints on how I can debug this further?

My code is pretty simple:

  1 int main(int argc, char** argv)
  2 {
  3     const char* buf;
  4     if(argc < 2)
  5         buf = "test->";
  6     else
  7         buf = argv[1];
  8 
  9     int fd; 
 10     if((fd = open("test.log", O_CREAT|O_WRONLY|O_APPEND, 0644)) == -1) {
 11         perror("Cannot open test.log");
 12         exit(1);
 13     }   
 14 
 15     int num_bytes = strlen(buf), num_bytes_written = -1; 
 16 
 17     while(1) {
 18         if((num_bytes_written = write(fd, buf, num_bytes)) == -1) {
 19             perror("Could not write to fd");
 20         }   
 21         fsync(fd);
 22         sleep(5);
 23     }   
 24 }   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尹雨沫 2024-10-04 15:16:38

当 vim(1) 编辑器退出时,它可能会用编辑后的版本替换原始文件。您的进程保持原始文件打开,但该文件不再存在,因为它的目录条目已被替换,因此,尚未打开该文件的进程都无法访问它。您的进程现在正在附加到任何其他进程都无法访问的文件。一旦您的进程关闭该文件,它将永远消失(除非您运行分区恢复程序)。

When the vim(1) editor exits, it's likely replacing the original file with the edited version. Your process is holding the original file open but that file no longer exists in the sense that it's directory entry has been replaced and, so, no process that doesn't already have the file open can access it. Your process is now appending to a file that can't be accessed by any other process. Once your process closes the file, it will be gone for good (unless you run a partition recovery program).

ι不睡觉的鱼゛ 2024-10-04 15:16:38

您的 vim 编辑器适用于文件的缓存版本。当您的其他程序附加到原始文件时,它会修改此缓存。当您使用 vim 保存时,您会使用更新的缓存文件覆盖原始文件并丢失所有日志。

Your vim editor works on a cached version of your file. It modifies this cache while your other programs append to the original file. When you save with vim, you overwrite the original file with the updated cached file and loose all logs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文