C Programming

Mlock() 2 C Function

The physical pages linked to the virtualized address range beginning at the addr are locked into ram for len characters mostly by the mlock system function. The pages that have been restricted through one or even more mlock methods are unlocked by the munlock() function. The memory should be assigned throughout its whole range. The specified pages after such an mlock call won’t result in erroneous pages or address translation issues unless they are released. On systems with development tool TLBs, they could nevertheless result in protection-violation failures or TLB-miss problems. Once all the restricted mappings for the pages are deleted, the actual pages are kept in storage.

Text Description automatically generated

Through their virtual address translations, the different operations may block the very same real pages. Through various virtual translations of the very same pages or stacked mlock requests on the very identical address range, a solitary method can also have several pages that are locked. An invocation to munmap() function that de-slots the untracked address range does the unlocking either expressly or impliedly.

After just a fork, the blocked translations are not passed on to the child process. Threads can only close a certain amount of physical ram as it is a relatively scarce commodity. Both the per-process RLIMIT_MEMLOCK asset restriction and the platform-wide “wired pages” limitation can be blocked by a single thread. Although the NetBSD implementations would round to the nearest multiple of the number of pages, the transportable code must make sure that perhaps the addr and len arguments are synchronized to one.

Return Values

Returning 0 means that the call is successful and almost all of the pages in the scope were either blocked or released. The lock state of every page in the domain remains unaltered if the numerical value is -1, indicating an exception. The mistake is indicated in this situation by setting the global location errno.

Errors

The mlock() function may fail due to several reasons. Within this part of the article, we will discuss the number of exceptions we got after the failure of the mlock() function along with their reasons for happening in execution. Here are a few of the errors that we may find so far:

  • EAGAIN: This error may occur when locking the specified range would go further than either the server or per-process threshold for restricted storage, thus the mlock() crashes in such a case.
  • EINVAL: This error may occur when the provided address but rather length is not page coordinated. In this case, the solution doesn’t work the way it should.
  • ENOMEM: A page’s fault or translation problem occurs when this error is found. It is because there may be some unallocated addresses in several of the provided address ranges.
  • EPERM: The EPERM error is found so far on a platform in which locking page accountancy is not supported and the mlock() function is invoked by some non-root user.

The munlock() function that is used simultaneously with the mlock() function may fail due to several reasons. Within this part of the article, we will discuss the number of exceptions that we get after the failure of the munlock() function along with their reasons for happening in execution. Here are some of the errors that we may find so far:

  • EINVAL: The solution doesn’t round and neither the address nor the length provided is page aligned.
  • ENOMEM: There are unallocated addresses in several of the provided address ranges. The given address range is not restricted entirely.

Example:

To understand the concept of the mlock() C function, we take a look at a simple example. We created a new C file named “mseg.c” within the Kali Linux “works” folder.

We start this code with the use of the standard C libraries required for the smooth execution of the C code, i.e. stdio.h and unistd.h. The sys/mman.h header is used to allow the use of the memory locking functions. After this, the execution starts from the initialization of a DATA_SIZE integer variable to 2048. The character array “data_lock” of the DATA_SIZE is defined and passed to the user-defined memory_lock() function along with the DATA_SIZE variable as an argument within the first “if” statement.

The user-defined memory_lock() function gets executed and gets the size of a page in the page_size variable. The page_offset is calculated and the address is adjusted to the page boundary. The size is adjusted with the page_offset and the mlock() function is invoked and returned using the “size” and “address” variables. The “if” statement perror() method will throw an error “lock_memory” if the returned result is equal to -1. Otherwise, the else statement will print the “Memory is locked in Ram” via the printf function.

Another “if-else” statement is used to release the lock. The memory_unlock() function is invoked within the “if” statement by passing the data_lock and DATA_SIZE as its arguments. The same procedure is followed by this function as we followed within the memory_lock() function. The only change is the use of the munlock() function with the very same arguments to release the lock and return the result. The second “if” statement will throw an “unlock_memory” exception using the perror function if the result is equivalent to -1. Otherwise, the else statement will display that “the memory is unlocked” using the printf function.

We created an object file of this C file using the GCC compilers “-o” option.

After executing the object file, we got the display messages as the memory was locked and unlocked via the use of mlock() and munlock() functions.

Conclusion

This is about the use of the mlock function of C in the Kali Linux operating system to show you the demonstration of how a memory can be locked and unlocked without any issue. We added the explanation, syntax, errors, and return value of the mlock function along with one detailed example. This tutorial would be enough for the users to get their hands with this function in the C programming language.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.