这是 Linux time() 函数或 Linux 操作系统调用中的错误吗?

发布于 2024-08-24 04:03:51 字数 2655 浏览 20 评论 0原文

我写了一个小程序,每隔 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#

谢谢和问候,

斯里尼瓦斯

I wrote a small program, that creates files at an interval of 1 minute. But the time at which the file is created and last written and the last modification time of the file as shown by ls command differs by 1 second. The code and the output is presented below. please let me know where could be the bug?

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#

Thanks and regards,

Srinivas

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

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

发布评论

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

评论(3

苦笑流年记忆 2024-08-31 04:03:51

首先,你的代码有问题。

  • 在循环之前删除 open()write(),它们没有执行任何操作。
  • 将两个 close() 调用移到 write() 调用之后。

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

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

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

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

First, there is a problem in your code.

  • Remove the open() and write() before the loop, they aren't doing anything.
  • Move the two close() call just after the write() calls.

This will ensure that the data is written and the file closed before you look its modification time using ls. Otherwise, there's a 1 second delay between the write() and the close(). Since you are writing only 5 bytes, it will get buffered. So, when you're checking the time after the write() call, you have no guarantee that the data have been written yet, so the file may not have been modified, which may screw your results.

Second, you cannot assume a 1 second delay because time() and ls report a 1 second difference. Since a second is your smallest unit, you should expect rounding difference. And since you are using two different methods to get the number of seconds since Epoch, they may use different rounding rules, which will easily result in a 1 second difference. If we add to that the filesystem which store the modification time, you actually have three different actors which may influence your results.

Also, if you look correctly at your result, you will see that its time() which indicate one second later than ls. So, you don't have a delay at all, you are going back in time! The rounding difference is the most probable reason for this.

So, there are no bug in Linux time() function or Linux OS calls.

盛夏已如深秋| 2024-08-31 04:03:51
  • 写入文件需要一些时间。
  • 调用 time() 函数需要一些时间
  • time() 函数的逻辑执行需要一些时间。

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

  • writing in a file takes some time.
  • call to the time() function takes some time
  • the execution of the logic of the time() function takes some time.

all this results in the 1 sec delay ..Its not at all a bug!

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