>> utimensat system call is required to set nanosecond filestamp.
>> It is available since Linux 2.6.22.
>> (glibc support is also required but I'm not sure the exact version.)
>>
>> I guess RHEL 5/CentOS 5 is older than that.
>
> True, kernel version is 2.6.19. So is there another way to solve this or do I have to get along with microseconds?

No another way. This is kernel limitation. In other word, you can't
compare gettimeofday() result and stat() result straightforwardly.

btw, even tough you use RHEL6, you can see this issue if you use some
older filesystems.
it depend on filesystem.

btw2, http://en.wikipedia.org/wiki/Ext4 says nanosecond timestamp is a
new feature of ext4.
I suspect your "RHEL5" mean ext3.