Author christian.heimes
Recipients christian.heimes, ferringb, georg.brandl, gregory.p.smith, ronaldoussoren, rosslagerwall
Date 2013-07-09.11:30:59
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1373369460.33.0.363639251888.issue13788@psf.upfronthosting.co.za>
In-reply-to
Content
In case someone is wondering if the approach really reduces the amount of syscalls: yes, it does. readdir() doesn't do a syscall for each entry. On Linux it uses the internal syscall getdents() to fill a buffer of directory entry structs. http://man7.org/linux/man-pages/man2/getdents.2.html

On my system os.listdir() does four syscalls:

$ strace python -c "import os; os.listdir('/home/heimes')"

openat(AT_FDCWD, "/home/heimes", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
getdents(3, /* 381 entries */, 32768)   = 12880
getdents(3, /* 0 entries */, 32768)     = 0
close(3)

On Linux you can also use /proc/self/fd instead of /proc/YOURPID/fd.

Other operating systems have different APIs to get a list of open FDs. AFAK /dev/fd is static on FreeBSD and Mac OS X:

FreeBSD:
  http://www.manualpages.de/FreeBSD/FreeBSD-7.4-RELEASE/man3/kinfo_getfile.3.html

Darwin / Mac OS X:
  proc_pidinfo()
History
Date User Action Args
2013-07-09 11:31:00christian.heimessetrecipients: + christian.heimes, georg.brandl, gregory.p.smith, ronaldoussoren, ferringb, rosslagerwall
2013-07-09 11:31:00christian.heimessetmessageid: <1373369460.33.0.363639251888.issue13788@psf.upfronthosting.co.za>
2013-07-09 11:31:00christian.heimeslinkissue13788 messages
2013-07-09 11:30:59christian.heimescreate