1

This question is a continuation of my question in another thread.Since the point of this question is slightly different,I thought I'd post it as a new question.

I experimented getting accessed time for some files in my linux machine(ubuntu lucid),using python .The idea is to have a function which goes through files in a particular directory,see if they have been accessed in the last 2 minutes,and print only those files which have not been accessed.

For accessing a file ,I defined a readfile() method

def readfile(fname):
    with open(fname) as f:
        ct = f.read()
        print 'read file at:',time.time()
        print 'length of file:',len(ct)

For convenience ,I defined some filenames

f1='/home/me/dev/misc/usedfiles/one.txt'
f2='/home/me/dev/misc/usedfiles/two.txt'
f3='/home/me/dev/misc/usedfiles/three.txt'
f4='/home/me/dev/misc/usedfiles/four.txt'

sothat calling readfile(f1) would access the file one.txt

I defined another function to go through the directory and print those files not accessed in last 2 minutes

def files_not_accessed():
    dirname = '/home/me/dev/misc/usedfiles'
    filenames = os.listdir(dirname)
    filenames = [os.path.join(dirname,filename) for filename in filenames]
    for filename in filenames:
        try:
            last_access = os.stat(filename).st_atime #secs since epoch
        except IOError:
            print 'could not get info about %s' % filename
        else:
            timediff =  time.time()-last_access
            print filename,'  last_access=',last_access
            print filename,'  timediff=',timediff
            if timediff > 2*60:
                print filename,'---older than 2 mts'
        print ''

Initially I ran the function files_not_accessed() and it gave the following output

/home/me/dev/misc/usedfiles/two.txt   last_access= 1341459500.0
/home/me/dev/misc/usedfiles/two.txt   timediff= 11668.9905779
/home/me/dev/misc/usedfiles/two.txt ---older than 2 mts

/home/me/dev/misc/usedfiles/one.txt   last_access= 1341460126.0
/home/me/dev/misc/usedfiles/one.txt   timediff= 11042.990674
/home/me/dev/misc/usedfiles/one.txt ---older than 2 mts

/home/me/dev/misc/usedfiles/three.txt   last_access= 1341459504.0
/home/me/dev/misc/usedfiles/three.txt   timediff= 11664.99072
/home/me/dev/misc/usedfiles/three.txt ---older than 2 mts

/home/me/dev/misc/usedfiles/four.txt   last_access= 1341459510.0
/home/me/dev/misc/usedfiles/four.txt   timediff= 11658.990757
/home/me/dev/misc/usedfiles/four.txt ---older than 2 mts

Now,I ran the readfile(f1) function to read the file one.txt

read file at: 1341471195.88
length of file: 47293

As you can see from output of files_not_accessed() function,the file one.txt was last accessed at 1341460126.0 ,and now it is again read at 1341471195.88 by readfile(f1)

And (immediately)ran the files_not_accessed() function and expected that ,it would not output the file one.txt since it was recently accessed.I expected its last_access time to be 1341471195.88

Strangely,I am again getting the previous output

/home/me/dev/misc/usedfiles/two.txt   last_access= 1341459500.0
/home/me/dev/misc/usedfiles/two.txt   timediff= 11715.1272521
/home/me/dev/misc/usedfiles/two.txt ---older than 2 mts

/home/me/dev/misc/usedfiles/one.txt   last_access= 1341460126.0
/home/me/dev/misc/usedfiles/one.txt   timediff= 11089.1273479
/home/me/dev/misc/usedfiles/one.txt ---older than 2 mts

/home/me/dev/misc/usedfiles/three.txt   last_access= 1341459504.0
/home/me/dev/misc/usedfiles/three.txt   timediff= 11711.1273921
/home/me/dev/misc/usedfiles/three.txt ---older than 2 mts

/home/me/dev/misc/usedfiles/four.txt   last_access= 1341459510.0
/home/me/dev/misc/usedfiles/four.txt   timediff= 11705.1274359
/home/me/dev/misc/usedfiles/four.txt ---older than 2 mts

I cannot understand why the file one.txt has last_access= 1341460126.0 instead of the expected 1341471195.88

Any idea why this happens?

1 Answer 1

1

Ubuntu lucid by default mounts filesystems with "relatime". If you need strict atimes, you need to mount with "strictatime" (at a performance cost).

Sign up to request clarification or add additional context in comments.

2 Comments

True, but isn't that mostly due to the fact that ext4 uses relatime by default?
@KlausWarzecha: Indeed this is the default behavior of the Linux kernel, but in this case Ubuntu Lucid does not deviate from upstream behavior. (IIRC Linux does this for other filesystems than ext4 as well)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.