#include <sys/types.h> #include <sys/mman.h> int msync( void *addr, size_t len, int flags );Service Program Name: QP0LLIB1
The msync() function can be used to write modified data from a shared mapping (created using the mmap() function) to non-volatile storage or invalidate privately mapped pages. The data located through mapping address addr for a length of len are either written to disk, or invalidated, depending on the value of flags and the private or shared nature of the mapping.
The following table shows the symbolic constants allowed for the flags parameter.
Symbolic Constant | Decimal Value |
Description |
---|---|---|
MS_ASYNC | 1 | Perform asynchronous writes. |
MS_SYNC | 2 | Perform synchronous writes. |
MS_INVALIDATE | 4 | Invalidate privately cached data |
The MS_SYNC and MS_ASYNC options are mutually exclusive. The MS_SYNC and MS_ASYNC options are ignored if the memory map was created with the MAP_PRIVATE option.
The MS_INVALIDATE option is used to discard changes made to a memory map created with the MAP_PRIVATE option. The private memory map is synchronized with the current data in the file. Any reference subsequent to the execution of the msync() function that invalidates a page will result in a reference to the current value of the file. The first modification of a page after the privately mapped page is invalidated results in the creation of a fresh private copy of that page. Subsequent modifications of this page prior to the next execution of an msync that invalidates the page will result in modifications to the same private copy of the page.
The MS_INVALIDATE value is ignored if the memory map was created with the MAP_SHARED option.
No authorization is required.
Upon successful completion, the msync() function returns 0.
When the msync() function fails, it returns -1 and sets errno as follows.
Error condition | Additional information |
---|---|
[EINVAL] |
For example, the value of the len parameter may be zero. Or, the value of the addr may not be a multiple of the page size or is out of the allowed range. |
[ENOTAVAIL] | |
[EUNKNOWN] |
The following messages may be sent from this function.
Message ID | Error Message Text |
---|---|
CPE3418 E | Possible APAR condition or hardware failure. |
CPFA0D4 E | File system error occurred. Error number &1. |
CPF3CF2 E | Error(s) occurred during running of &1 API. |
CPF9872 E | Program or service program &1 in library &2 ended. Reason code &3. |
Process termination does not automatically write changed pages to disk. Some or all pages may be eventually written by the paging subsystem, but no guarantee is given. Therefore, if the data must be preserved the msync() function must be used to ensure changes made through a shared memory map are written to disk.
If the DTAMDL(*LLP64) parameter is used when compiling an ILE C program, this limitation does not apply as the pointers will be 8 byte pointers, and their pointer attribute will be preserved.
See Code disclaimer information for information pertaining to code examples.
The following example creates a file, creates a memory map, stores data into the file, and writes the data to disk using the msync() function.
#include <errno.h > #include <fcntl.h > #include <unistd.h > #include <stdio.h > #include <stdlib.h > #include <string.h > #include <sys/types.h > #include <sys/mman.h > main(void) { size_t bytesWritten =0; int fd; int PageSize; const char textÝ = "This is a test"; if ( (PageSize = sysconf(_SC_PAGE_SIZE)) < 0) { perror("sysconf() Error="); return -1; } fd = open("/tmp/mmsyncTest", (O_CREAT | O_TRUNC | O_RDWR), (S_IRWXU | S_IRWXG | S_IRWXO) ); if ( fd < 0 ) { perror("open() error"); return fd; } off_t lastoffset = lseek( fd, PageSize, SEEK_SET); bytesWritten = write(fd, " ", 1 ); if (bytesWritten != 1 ) { perror("write error. "); return -1; } /* mmap the file. */ void *address; int len; off_t my_offset = 0; len = PageSize; /* Map one page */ address = mmap(NULL, len, PROT_WRITE, MAP_SHARED, fd, my_offset); if ( address == MAP_FAILED ) { perror("mmap error. " ); return -1; } /* Move some data into the file using memory map. */ (void) strcpy( (char*) address, text); /* use msync to write changes to disk. */ if ( msync( address, PageSize , MS_SYNC ) < 0 ) { perror("msync failed with error:"); return -1; } else (void) printf("%s","msync completed successfully."); close(fd); unlink("/tmp/msyncTest"); }
Output:
This is a test.
Top | UNIX-Type APIs | APIs by category |