Is this a bug in the Linux time function () or calls to the Linux operating system?

I wrote a small program that creates files with an interval of 1 minute. But the time when the file is created and written last, and the time of the last file modification, as shown by the ls command, differs by 1 second. Code and output are presented below. tell me where the error may be?

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#

Thank you and welcome

Shrinivas

+3
source share
3 answers

Firstly, there is a problem in your code.

  • Delete open()and write()before the cycle, they do nothing.
  • Move the two calls close()immediately after the calls write().

, , , ls. write() close() 1 . 5 , . , write(), , , , , , .

-, 1- , time() ls 1- . - , . Epoch, , 1 . , , , .

, , , time(), ls. , , ! .

, Linux() Linux.

+2
  • .
  • time()
  • the execution of the logic of the time () function takes some time.

all this leads to a delay of 1 second. It's not a mistake!

-1
source

Source: https://habr.com/ru/post/1736260/


All Articles