LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-10-2010, 12:44 AM   #1
sinu_nayak2001
Member
 
Registered: Oct 2009
Location: India
Distribution: Puppy Linux, Wary 530.
Posts: 31

Rep: Reputation: 20
Bug in Linux time() function ? Or in Linux OS calls?


Dear All,

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
 
Old 03-10-2010, 02:43 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
How repeatable is this? Does it happen every time?

Are you running the NTP update daemon (ntpd)?

What is the file system (eg ext3)? What options have been used to mount it (/etc/fstab)?

What happens if you move the 'close(fd)' after the 'printf' (instead of before it)?

(I couldn't repeat your results, which is why I am asking these questions).
 
Old 03-10-2010, 03:01 AM   #3
sinu_nayak2001
Member
 
Registered: Oct 2009
Location: India
Distribution: Puppy Linux, Wary 530.
Posts: 31

Original Poster
Rep: Reputation: 20
@neonsignal

This is always repeatable.
ntpd is running.
file system is ext3.

from fstab:
/dev/sda6 /home ext3 relatime 0 2

Actually, my problem needs exactly this king of solution, so when I tried this, I got this problem and reported. I had tried another small program where file open and close happens immediately as you have proposed. Found no issue.

someone else also has reproduced this.
"I'm able to reproduce this on a locally mounted filesystem. It's very odd..." – R Samuel Klatchko

some more info on system:
Linux new 2.6.24-19-server #1 SMP Wed Jun 18 14:44:47 UTC 2008 x86_64 GNU/Linux
 
Old 03-11-2010, 12:02 AM   #4
sinu_nayak2001
Member
 
Registered: Oct 2009
Location: India
Distribution: Puppy Linux, Wary 530.
Posts: 31

Original Poster
Rep: Reputation: 20
http://www.spinics.net/lists/kernel/msg1008110.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
function calls inside a function manas_sem Programming 2 02-28-2007 01:27 AM
function calls inside a task not working manas_sem Programming 0 02-26-2007 05:19 AM
Tracing function calls... How? ik_nitk Programming 3 06-05-2006 07:05 AM
libc function calls h/w Programming 4 02-13-2004 03:00 PM
using timer for repeated function calls poojamanu Programming 1 04-09-2003 08:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration