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 126820开发者_C百科2459 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
First, there is a problem in your code.
- Remove the
open()
andwrite()
before the loop, they aren't doing anything. - Move the two
close()
call just after thewrite()
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.
http://www.spinics.net/lists/kernel/msg1008110.html
- 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!
精彩评论