我写了一个小程序,每隔 1 分钟创建一次文件。但ls命令显示的文件创建时间和最后写入时间与文件最后修改时间相差1秒。代码和输出如下所示。请让我知道哪里可能有错误?

root@new:/home/srinivas# cat b.c
#include <time.h>
#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
int main ()
{
    int fd;
    int i=0;
    time_t initial_time = time(NULL);
    time_t interval = 60;
    time_t curr_time = time(NULL);

    fd=open ("test1.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    write(fd,"abcd1",5);
    while(1)
    {
        curr_time = time(NULL);
        if(curr_time >= initial_time)
        {
            if(i==0)
            {
                close(fd);
                printf("\ntime before test2.txt fileopen= %d\n", time(NULL));
                fd=open ("test2.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
                write(fd,"abcd2",5);
                printf("time after test2.txt filewrite= %d\n", time(NULL));
                system("ls -l --time-style=+%s test2.txt");
                initial_time += interval;
                i=1;
            }
            else
            {
                close(fd);
                printf("\ntime before test1.txt fileopen= %d\n", time(NULL));
                fd=open ("test1.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
                write(fd,"abcd1",5);
                printf("time after test1.txt filewrite= %d\n", time(NULL));
                system("ls -l --time-style=+%s test1.txt");
                initial_time += interval;
                i=0;
            }
        }
        usleep(1000);
    }
    return 0;
}
root@new:/home/srinivas# gcc b.c
root@new:/home/srinivas# ./a.out

time before test2.txt fileopen= 1268203133
time after test2.txt filewrite= 1268203133
-rw-r--r-- 1 root root 5 1268203133 test2.txt

time before test1.txt fileopen= 1268203193
time after test1.txt filewrite= 1268203193
-rw-r--r-- 1 root root 5 1268203192 test1.txt

time before test2.txt fileopen= 1268203253
time after test2.txt filewrite= 1268203253
-rw-r--r-- 1 root root 5 1268203252 test2.txt

time before test1.txt fileopen= 1268203313
time after test1.txt filewrite= 1268203313
-rw-r--r-- 1 root root 5 1268203312 test1.txt

time before test2.txt fileopen= 1268203373
time after test2.txt filewrite= 1268203373
-rw-r--r-- 1 root root 5 1268203372 test2.txt

root@new:/home/srinivas# ls -ltr --time-style=+%s
total 40
-rwxrwxrwx  1 root     root      1095 1268202457 b.c
-rwxr-xr-x  1 root     root     10300 1268202459 a.out
-rw-r--r--  1 root     root         5 1268203312 test1.txt
-rw-r--r--  1 root     root         5 1268203372 test2.txt
root@new:/home/srinivas#

感谢致敬,

斯里尼瓦斯

有帮助吗?

其他提示

首先,你的代码有问题。

  • 去除 open()write() 在循环之前,他们没有做任何事情。
  • 移动两个 close() 之后立即致电 write() 来电。

这将确保在使用 ls 查看其修改时间之前数据已写入且文件已关闭。否则,write() 和 close() 之间会有 1 秒的延迟。由于您只写入 5 个字节,因此它将被缓冲。因此,当您在 write() 调用后检查时间时,您无法保证数据已被写入,因此文件可能尚未被修改,这可能会破坏您的结果。

其次,你不能假设有 1 秒的延迟,因为 time()ls 报告 1 秒差异。由于秒是最小单位,因此您应该预料到舍入差异。而且由于您使用两种不同的方法来获取自 Epoch 以来的秒数,因此它们可能使用不同的舍入规则,这很容易导致 1 秒的差异。如果我们添加存储修改时间的文件系统,您实际上有三个不同的参与者可能会影响您的结果。

另外,如果你正确地查看你的结果,你会发现它 time() 这表明晚于一秒 ls. 。所以,你根本没有延迟,你正在回到过去!舍入差异是最可能的原因。

因此,Linux time() 函数或 Linux 操作系统调用中不存在错误。

  • 写入文件需要一些时间。
  • 调用 time() 函数需要一些时间
  • time() 函数逻辑的执行需要一些时间。

所有这些都会导致 1 秒延迟..这根本不是一个错误!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top