C Programming

How to Use posix_fadvise syscall in C

Programs may use posix_fadvise() system call to inform the kernel that they plan to view data files in a certain format in the long term, enabling the kernel to optimize accordingly. Through storing previously used document blocks in storage, the system file buffer (buffer cache) aids programs in getting to the data blocks quicker. When you duplicate a massive file tree, it has a disastrous impact on the buffer, and all of the replicated content ends up inside the buffer as well, forcing all data blocks away. This harms device output, and all other activities on the machine that seem to have the chunks of information inside the buffer even before replication began would have to read data from disc instead. You will tell the OS to exclude those file frames from the buffer while consuming posix_fadvise.

We will be using the posix_fadvise system call feature to tell the OS what you want to do with the relevant information via an open filehandle. Any subsequent page buffer is depleted any time we apply posix_fadvise() through POSIX_FADV_DONTNEED. Throughout this part, we’ll focus on consuming the posix_fadvise system call to give the kernel recommendations on regular file I/O. Let’s look at its syntax first.

Syntax

#include <sys/fcntl.h>
Int posix_fadvise( int fd, off_t offset, off_t len, int advice );

We have first to include the “fcntl.h” library to make the code work efficiently. The offset marks the beginning of the field on which you’re offering advice. The width of the field seems to be the len. While length is 0, the call would impact all bytes beginning at offset. The form of advice is defined by the advice attribute.

Advice Parameter

The following are appropriate attributes for advice:

POSIX_FADV_NORMAL:
This demonstrates that perhaps the program seems to have no advice to offer about its information access format. That’s the standard presumption if no guidance is provided for an open file.

POSIX_FADV_SEQUENTIAL:
The program anticipates simultaneous access to the required information (with inferior offsets read beforehand upper ones).

POSIX_FADV_RANDOM:
In a randomized fashion, the required information will be obtained.

POSIX_FADV_NOREUSE:
Just one time can the specified data be obtained.

POSIX_FADV_NOREUSE:
As in the immediate future, the defined information will be gathered.

POSIX_FADV_DONTNEED:
In the immediate future, the listed information can not be accessible.

Example of Posix_Fadvise

Let’s start working on the posix_fadvise system call. Log in from your Linux system as a root user and try to open the command console terminal. Try opening it using the “Ctrl+Alt+T” key. If this doesn’t work for you, then try to direct to the Activity bar highlighted on the left side of your Linux system. Click on it, and it will open a “search bar” for you to use. Type “terminal” in it and press the “Enter” control. In some seconds, the terminal will be opened, and you can use it. But before using the C language code in any file, we must have some language compiler mounted on our Linux distribution. We recommend you configure the “GCC” C language compiler on your system. For installation, try out the below query in the console terminal to avoid problems in the future. If it asks for your account password, write out to proceed.

$ sudo apt install gcc

Now the compiler “GCC” has been efficaciously fixed. It’s to work on some C language script. For that reason, you have to generate a fresh file with a “C” extension at its end. If you want to write code immediately after generating a file, you can generate it via the GNU Nano editor. Henceforth, use the below-stated instruction in the console and press the “Enter” key to see the output. We have used “test” as the name of a file; you can change it as well.

$ nano test.c

The GNU Nano editor 4.8 has been opened; we will write out the C script in it.  Firstly we have defined some libraries, e.g., fcntl and unistd. These libraries are necessary because without this code; it wouldn’t work. Then we have specified the main function with two parameters. One of them is an integer type, and the other is a character type array. This main() method call has defined an integer “fd” to be used as a descriptor. The open system call has been used to open the array content concerning its index “1”. It will read the content and revert it to the integer “fd” file descriptor. Now the important step is here. We will bound this file descriptor “fd” with the kernel using the “fdatasync” function, passing this “fd” descriptor in the argument. So, we have used the posix_fadvise system call having “fd” as the first parameter. We have defined the beginning offset as 0, and the length of the field is specified as 0. Then we have used POSIX_FADV_DONTNEED as the advice parameter. The piece of advice we’re looking for is named POSIX_FADV_DONTNEED. It informs the operating system that the requested bytes would not be required again. The bytes would be issued from the document system buffer as a result of all this. The accompanying mini-program instructs the OS to clear the buffer of all information combined with a certain file. At last, the “close” system call will be used to close the file descriptor “fd” and the main function will be ended. Press “Ctrl+S” to save the code and “Ctrl+X” from leaving the file.

Let’s compile the code first to make it work accurately. For that, use the “gcc” compiling instruction alongside the name of a C kind file as follows:

$ gcc test.c

After the compilation, you have to run the file using the “a.out” query in the console. It is showing no output because the kernel has been informed, and it worked correctly.

$ ./a.out

Conclusion

We have discussed the posix_fadvise system call along with its different “advice” parameters. Try out the other advice parameters to understand it fully.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.