MSYNC(3POSIX)
π PROLOG π
okay bestie, real talk before we even start: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface might not even be implemented on Linux at all. π no cap.
β¨ NAME β¨
msync β synchronize memory with physical storage πΎβ¨
π¦ SYNOPSIS π¦
#include <sys/mman.h>
int msync(void *addr, size_t len, int flags);
π DESCRIPTION π
okay so here's the vibe. The msync() function shall write all modified data to permanent storage locations, if any, in those whole pages containing any part of the address space of the process starting at address addr and continuing for len bytes. and if no such storage exists? msync() need not have any effect. it's giving "nothing to sync, nothing to stress about". π
If requested, the msync() function shall then invalidate cached copies of data.
The implementation may require that addr be a multiple of the page size as returned by sysconf(). lowkey important, bestie.
For mappings to files, the msync() function shall ensure that all write operations are completed as defined for synchronized I/O data integrity completion. It is unspecified whether the implementation also writes out other file attributes. When the msync() function is called on MAP_PRIVATE mappings, any modified data shall not be written to the underlying object and shall not cause such data to be made visible to other processes. It is unspecified whether data in MAP_PRIVATE mappings has any permanent storage locations. The effect of msync() on a shared memory object or a typed memory object is unspecified. The behavior of this function is unspecified if the mapping was not established by a call to mmap(). π« yeah. don't do that.
The flags argument is constructed from the bitwise-inclusive OR of one or more of the following flags defined in the <sys/mman.h> header:
MS_ASYNCβ Perform asynchronous writes. β‘MS_SYNCβ Perform synchronous writes. π’MS_INVALIDATEβ Invalidate cached data. π§Ή
When MS_ASYNC is specified, msync() shall return immediately once all the write operations are initiated or queued for servicing; when MS_SYNC is specified, msync() shall not return until all write operations are completed as defined for synchronized I/O data integrity completion. Either MS_ASYNC or MS_SYNC shall be specified, but not both. pick a lane bestie, you can't have both. π
When MS_INVALIDATE is specified, msync() shall invalidate all cached copies of mapped data that are inconsistent with the permanent storage locations such that subsequent references shall obtain data that was consistent with the permanent storage locations sometime between the call to msync() and the first subsequent memory reference to the data. β¨ it's giving "trust me, the data is fresh now".
If msync() causes any write to a file, the file's last data modification and last file status change timestamps shall be marked for update. π iconic behavior honestly.
π₯ RETURN VALUE π₯
Upon successful completion, msync() shall return 0 π; otherwise, it shall return -1 and set errno to indicate the error. π
πΈ ERRORS πΈ
The msync() function shall fail if:
EBUSYβ Some or all of the addresses in the range starting ataddrand continuing forlenbytes are locked, andMS_INVALIDATEis specified. πEINVALβ The value offlagsis invalid. π«ENOMEMβ The addresses in the range starting ataddrand continuing forlenbytes are outside the range allowed for the address space of a process or specify one or more pages that are not mapped. π«₯
The msync() function may fail if:
EINVALβ The value ofaddris not a multiple of the page size as returned bysysconf(). π
The following sections are informative. (iykyk, they're the bonus content π)
π EXAMPLES π
None. literally none. not a single one. the void. π³οΈβ¨
π APPLICATION USAGE π
The msync() function is only supported if the Synchronized Input and Output option is supported, and thus need not be available on all implementations. so like, vibe check your platform first bestie.
The msync() function should be used by programs that require a memory object to be in a known state; for example, in building transaction facilities. very main character energy of you. π
Normal system activity can cause pages to be written to disk. Therefore, there are no guarantees that msync() is the only control over when pages are or are not written to disk. β οΈ yeah the kernel has a whole life of its own, it does not wait for you.
π§ RATIONALE π§
The msync() function writes out data in a mapped region to the permanent storage for the underlying object. The call to msync() ensures data integrity of the file. periodt.
After the data is written out, any cached data may be invalidated if the MS_INVALIDATE flag was specified. This is useful on systems that do not support read/write consistency. π¦
β FUTURE DIRECTIONS β
None. the future is... nothing. slay. π
π SEE ALSO π
mmap()sysconf()
The Base Definitions volume of POSIX.1β2017, <sys_mman.h> π
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
ok that's the whole page bestie, you made it to the end π«Ά this absolute banger of a page came from the fine folks at IEEE/The Open Group, 2017, and yes the manual name really is MSYNC(3POSIX) β go forth and sync responsibly. mwah. πβ¨π
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms