简体   繁体   English

在Linux C中同步读取文件

[英]File reading synchronously in Linux C

Please see below code. 请参见下面的代码。

#include < stdio.h >    
#include < fcntl.h >    
#include < stdlib.h >    
#include < string.h >    

int main(int argc,char **argv,char **envp)
{
   int fd;
   size_t sz;
   char filebuffer[1024];
   int loop;
   fd=open("sample",O_RDONLY);
   if(fd==-1)
   {
      perror("");
      exit(1);
   }
   loop=0;
   while(++loop<300)
   {
      lseek(fd,0,SEEK_SET);
      memset(filebuffer,0,1024);
      sz=read(fd,filebuffer,1024);
      printf("%d.sz=%zd\t%s\n",loop,sz,filebuffer);
      sleep(1);
   }
   close(fd);
   return 0;
}

In this code, I am able to read file. 在这段代码中,我能够读取文件。 But when I am changing file (reading file "sample") at the same time during reading. 但是,当我在读取过程中同时更改文件(读取文件“样本”)时。 Then I am not able to read the changed file. 然后,我将无法读取更改的文件。 I tried O_SYNC flag too. 我也尝试过O_SYNC标志。 but still, it is not working, but O_DIRECT is undefined error is coming up. 但仍然无法正常工作,但是O_DIRECT是未定义错误,即将出现。 How can I ensure that I am able to read changes? 如何确保我能够读取更改? Second thing, but I observed, if I close and open the file reading, then I am able to read changed file. 第二件事,但我观察到,如果我关闭并打开文件读取功能,则可以读取更改的文件。

Question: 题:
How can I read changed file without closing and opening? 如何在不关闭和打开的情况下读取更改的文件?

I think that you're asking the following question: 我认为您是在问以下问题:

I have a program which opens a file called sample and repeatedly reads the first block of that file. 我有一个程序可以打开一个名为sample的文件,并反复读取该文件的第一块。 That works fine. 很好 However, if I edit the file sample , for example with a text editor, then my program does not see the changes, although it will if it closes and reopens the file. 但是,如果我使用文本编辑器(例如,使用文本编辑器)编辑文件sample ,则我的程序看不到更改,尽管它会关闭并重新打开文件。 How can I see the changes without closing and reopening the file? 如何在不关闭并重新打开文件的情况下看到更改?

If that's your question, then the answer is: 如果这是您的问题,那么答案是:

Sorry, you cannot, because the text editor does not modify the file. 抱歉,您不能,因为文本编辑器不会修改文件。 It creates a new file with the old name . 它将使用旧名称创建一个新文件

In Unix, once you open a file, it will not actually get deleted, even if its name is unlinked. 在Unix中,打开文件后,即使文件名未链接,它实际上也不会被删除。 If another program "deletes" the file and then creates a new file with the same name, the file you have open is no longer accessible to any other program, but it is still the same file and it will not get deleted until you close it. 如果另一个程序“删除”了该文件,然后创建了一个具有相同名称的新文件,则您打开的文件将不再可供任何其他程序访问,但是它仍然是同一文件,并且在您关闭它之前不会被删除。 。

Most Unix text utilities, even the ones which claim to work "in-place" (such as sed -i ) really do not modify files. 大多数Unix文本实用程序,甚至那些声称可以“就地”运行的实用程序(例如sed -i )实际上都不会修改文件。 That includes text editors. 其中包括文本编辑器。 So your program doesn't see changes in the file because the file is not changing ; 因此您的程序看不到文件中的更改,因为文件未更改 the name has been given to a new file. 该名称已被赋予一个新文件。

So the only way to deal with this is to close and reopen the file. 因此,解决此问题的唯一方法是关闭并重新打开文件。 When you reopen, you will be opening the new file with the old name. 重新打开时,将使用旧名称打开新文件。

The reason for not getting updated data in file could be sync time in filesystem. 无法在文件中获取更新数据的原因可能是文件系统中的同步时间。

I suggest fflush() after writing in to file. 我建议在写入文件后使用fflush() This makes your cache data to be written in file. 这样就可以将缓存数据写入文件中。

Related discussions. 相关讨论。

Small file not committed to disk for over a minute 小文件一分钟未提交到磁盘

Is fwrite non-blocking? fwrite是非阻塞的吗?

This is an adaptation of your code. 这是您的代码的改编。 It forks to create two processes. 它分叉创建两个进程。 The child contains your code, substantially unchanged (different error message, file name variable, and more care with printing the filebuffer which is not null terminated). 该子级包含您的代码,基本上未更改(不同的错误消息,文件名变量,并且更注意打印不以null终止的文件filebuffer )。 The parent writes characters (the same character, over and over) to the file. 父级将字符(一遍又一遍)写入文件。

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static char const filename[] = "sample";

int main(void)
{
    int fd;
    size_t sz;
    char filebuffer[1024];
    int loop;

    switch (fork())
    {
    case -1:
        fprintf(stderr, "Failed to fork\n");
        break;

    case 0:
        sleep(1);
        fd = open(filename, O_RDONLY);
        if (fd == -1)
        {
            fprintf(stderr, "Failed to open file %s for reading\n", filename);
            exit(1);
        }
        loop = 0;
        while (++loop < 300)
        {
            lseek(fd, 0, SEEK_SET);
            memset(filebuffer, 0, 1024);
            sz = read(fd, filebuffer, 1024);
            printf("%d.sz=%zd\t%.*s\n", loop, sz, (int)sz, filebuffer);
            sleep(1);
        }
        close(fd);
        break;

    default:
        fd = open(filename, O_WRONLY|O_CREAT, 0644);
        if (fd == -1)
        {
            fprintf(stderr, "Failed to create file %s for writing\n", filename);
            exit(1);
        }
        for (loop = 0; loop < 256; loop++)
        {
            memset(filebuffer, (loop % 64) + 33, sizeof(filebuffer));
            lseek(fd, 0L, SEEK_SET);
            write(fd, filebuffer, sizeof(filebuffer));
            sleep(1);
        }
        close(fd);
        break;
    }

    return 0;
}

Example output: 输出示例:

1.sz=1024   """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2.sz=1024   ################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
3.sz=1024   ################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
4.sz=1024   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%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&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
6.sz=1024   ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
7.sz=1024   ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
8.sz=1024   ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
9.sz=1024   ****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************

As you can see, the processes run asynchronously, so the data in the file does not always change between reads, but the process is seeing the changes. 如您所见,进程是异步运行的,因此文件中的数据在读取之间并不总是更改,而是进程看到了更改。

Try running this on your computer. 尝试在您的计算机上运行它。 It should work. 它应该工作。 (Sample output from Mac OS X 10.8.5.) If it doesn't, you'll need to identify which file system type you have, but I don't think it'll be a problem. (来自Mac OS X 10.8.5的示例输出。)如果没有,则需要确定所具有的文件系统类型,但是我认为这不会成为问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM