Syntax:
Int msync( void * addr, size_t len, int flags);
Addr: The start of the addresses you wish to synchronize as a range.
Len: The number of the locations’ length expressed in bytes.
Explanation:
Within these full pages that comprise any portion of the address space of the process, beginning at the address addr and running for length bytes, the msync() method must write all the updated information to persistent storage areas. Msync() should not have an impact if there is no such memory. The msync() method will then remove the stored backups of information if required. Addr must be a multiple of the number of pages provided by the sysconf() function for the implementation to work.
The msync (*) method must make sure that all the write processes are finished as required for the synchronized I/O data security completeness for mappings to documents. The implementation may optionally write out additional file characteristics, although this is not defined. Any updated data must not be transferred to the core object or made freely available to certain other operations whenever the msync() method is performed on MAP_PRIVATE translations. It is not known where the information in MAP_PRIVATE translations is stored permanently. [SHM|TYM] [Choice Start] Msync() does not specify whether it affects the shared memory objects or the typed storage objects.
End of Option: If a request to map wasn’t used to create the translation, this method’s functionality is undefined.
Flag Options Available for Msync()
The options parameter is made up of the bitwise-inclusive OR of several flags listed in the following that are specified in the header file “sys/mman.h”:
- MS_ASYNC: Carry out simultaneous writing
- MS_INVALIDATE: Inaccurate mappings
- MS_SYNC: Carry out simultaneous writing
- MS_CACHE_ONLY: Since QNX Neutrino currently supports POSIX compatibility for memory-mapped documents, msync() carries out its desired purpose of writing modifications in the translated memory back to the actual file. MS CACHE ONLY (QNX Neutrino addition; QNX Neutrino Core OS 6.3.2 or higher). If you prefer the QNX Neutrino addition to the POSIX standard behavior, use this bit to instruct the method to flush or saturate the data cache (through the MS ASYNC, MS SYNC, or MS INVALIDATE flags).
- If you’re dynamically changing the code, use the QNX Neutrino extension MS INVALIDATE ICACHE to ensure that the updated code is what is executed.
- Only work on cleaned pages with the QNX Neutrino extension MS_CLEAN ONLY (version 7.0 or later).
Before the request, all the write links to the system memory were accessible to the succeeding read processes on the document. It is unclear if the read pointers to the storage area may access the modifications to the very same area of the document that was made even before the request. If the unaltered pages within the particular range are additionally written to the fundamental hardware, this is not mentioned. When all the write processes are scheduled, the method may respond right away if the flag is MS_ASYNC. When the flags are MS_SYNC, the method somehow doesn’t return unless all the write processes are finished.
By synchronizing the storage region’s data with the existing information stored, MS_INVALIDATE makes them coincide. Successive read accesses to the linked storage area can see all the writes to the document’s translated area that was made even before the request. It is unclear if the read pointers to the storage areas that were translated to the very same area of the document utilizing the MAP_SHARED even before calling through any function would be able to see the area. The file’s st_ctime and st_mtime columns are flagged for updating if the usage of the msync() function results in any write operations performed on the directory.
Returned Value:
- Msync() function yields 0 if it is completed.
- Msync() function outputs -1 and assigns an errno to one of the following error parameters if it fails.
Application Usage:
The msync () method does not have to be accessible on all variants since it is only allowed if the Storage Mapped Documents and Coordinated Incoming and Outgoing options are provided. Applications that need a storage object to be in a defined position such as those that provide transaction facilities, must utilize the msync() method. The pages may be transferred to disc during the routine system activity.
There is no certainty that msync() seems to be the only factor influencing whether or not the pages are transferred to disc. The msync() method sends the data from a mapped area to the base entity’s persistent storage. The document’s integrity of data is ensured by the request to msync(). If somehow the MS_INVALIDATE flag was provided, any stored data may be expired when the data is entered out. On the platforms that do not provide read/write integrity, this is helpful.
Example:
We created a new C file using the vim instruction on the shell. It is created within the project1 folder of our Kali Linux system as per the output of the list “ls” instruction in the following image, i.e. “mysync.c”.
To open this file, we use the vim editor as per the following instruction:
The empty file opens within the vim editor of our system. We add the necessary headers in this code file first. The stdio.h, unistd.h, and string.h are used to get the standard input and output. While the use of “sys/mman.h” and fcntl.h are used to utilize the msync() function and files in the code. The main() function initializes the value of the file descriptor to -1 and gets the page size by using the getpagesize() function. We open the “newfile.txt” file within the read and write mode. If there is no file with this name, the “O_CREATE” option creates a new one.
The returned value is stored in the file descriptor and the “if” statement utilizes it to see if the value is less than 0 or not. If so, it will print that the “file opening failed”. The map_address pointer is used to map the address of a file and if it fails, the “if” statement will print out the respective error message. The strcpy() function is here to copy a string to a map_address variable and the “if-else” statement uses the msync() function to synchronize the mapped memory to the file and display the respective output, i.e. failed or succeeded.
We compiled this C file with the GCC compiler and created its object file. Then, we executed its object file and got the message that the Mapped memory has been successfully synchronized to the file.
Conclusion
This is all about the use of the msync() 2 function of C in our system. We discussed its syntax along with it’s parameter, separately. We also discussed the options used within it and the errors we can get from its failure. We added one detailed example to explain its usage in the C programming Language.