1 분 간격으로 파일을 생성하는 작은 프로그램을 작성했습니다. 그러나 파일이 작성되고 마지막으로 작성된 시간과 ls 명령에 표시된 파일의 최종 수정 시간은 1 초 차이가납니다. 코드와 출력은 아래에 나와 있습니다. 버그가 어디 있는지 알려주세요. 파일에Linux time() 함수 또는 Linux OS 호출의 버그입니까?
[email protected]:/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;
}
[email protected]:/home/srinivas# gcc b.c
[email protected]:/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
[email protected]:/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
[email protected]:/home/srinivas#
감사와 관련,
스 리니 바스
그것은 여기 잘 작동하지만 당신이보고있는 것은 ... 홈 디렉토리 아마도 NFS, 또는 특별한 마운트 옵션과 같은 몇 가지 "이상한"파일 시스템을 사용하고 정말 예상치 못한입니까? – Martin
@Martin - 로컬로 마운트 된 파일 시스템에서 이것을 재현 할 수 있습니다. 매우 이상합니다. –
@Martin - My/home은 ext3 형식입니다. 마운트 된 파티션이 아닙니다. –