I've used mmap() with fopen("/dev/mem") to create a mapping to a block of physical memory shared between two processor cores in an ARM system. When the processor running Linux writes the memory, there can be a lag of over one second before the other non-Linux processor sees the written data. The long delay disappears if the Linux process makes this system call just after writing to memory:
system("sync; echo 3 > /proc/sys/vm/drop_caches" );
I've tried to duplicate that logic directly in code, but the long delay persists:
int fd;
char* data = "3";
sync();
fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
write(fd, data, sizeof(char));
close(fd);
Why does the sync() call differ in behavior from the sync system command? Does the sync command effect virtual memory flushes that the sync() call does not?
I know the manual says that the sync program does nothing but exercise the sync(2) system call, but does the fact that I call sync() from userspace affect its behavior? It acts as though a call to sync from userspace merely schedules the sync rather than blocking until its completion.