Name

msync - synchronizes a file with a memory map

Library

libc.lib

Synopsis

  #include <sys/mman.h>
  int msync (void *addr, size_t len, int flags);

Return values

Upon successful completion, msync() shall return 0; otherwise, it shall return -1 and set errno to indicate the error.

Detailed description

The msync system call writes any modified pages back to the file system. if len is non-zero, only those pages containing addr and len-1 succeeding locations will be examined. The flags argument may be specified as follows:

MS_ASYNC
  This flag is currently not supported.
MS_SYNC
  Perform synchronous writes
MS_INVALIDATE
  Invalidate all cached data


Examples

/*
* Detailed description: Example to sync changes on mapped memory to file.
* Precondition: None
*               
*/
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
int main(void)
{
    int fd = -1;
    char* mapaddr;
    int len = getpagesize();
    int prot = PROT_WRITE | PROT_READ;
    if((fd = open("C:\\Test.txt", O_RDWR | O_CREAT, 0666) ) < 0)
    {
         printf("File open failed");
    }
    mapaddr = (char*)mmap((void*)0, len, prot, MAP_SHARED, fd, 0);
    if(mapaddr == MAP_FAILED)
    {
         printf("mmap on file failed");
    }
    strcpy(mapaddr, "This is a write through mapped memory ");
    if(-1 == msync(mapaddr, len, MS_SYNC))
    {
        printf("Sync on mapped memory to file failed");
    }
    printf("Sync on mapped memory to file succeeded");
}

         

Errors

The msync system call will fail if:
[EBUSY]
  Some or all of the pages in the specified region are locked and MS_INVALIDATE is specified. (This errno is currently not supported)
[EINVAL]
  The addr argument is not a multiple of the hardware page size.
[EINVAL]
  The len argument is too large or negative.
[EINVAL]
  The flags argument was both MS_ASYNC and MS_INVALIDATE. Only one of these flags is allowed or the flags are invalid.
[ENOMEM]
  The addresses in the range starting at addr and continuing for len bytes are outside the range allowed for the address space of a process or specify one or more pages that are not mapped.

See also

mlock, mprotect, munmap
The msync system call first appeared in BSD 4.4.

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top