The fsync is used to coordinate a file’s in-core state with any storage device. The fsync() system call passes (“flushes”) all altered in-core content of (i.e., altered buffer for) the file descriptor fd to a disc machine (or any other perpetual storing device) where such a file remains. It even cleans up the file’s details. The fsync system call works on a single file. Any updates to the file will be flushed. If several tasks are edited to the same file, the fsync system call will be suspended before all updates have been transferred to the disc. If the system announces that the conversion is successful, the call will be blocked. Using fsync() doesn’t guarantee that perhaps the file’s inclusion in the filesystem has entered the disc as well. An appropriate fsync() on the folder’s file descriptor is, however, required for this.
So the fsync system call needs some libraries to be installed to work on it. If somebody wants to check the additional information regarding the fsync command, he/she must have manpages-dev packages installed on their Linux system. So we will see how to do that. Use the Ctrl+Alt+T key from the keyboard to launch the Linux command-line terminal. Now, to install the manpages-dev package, try out the below following apt install command, followed by a package named “manpages-dev”. The process of installation will require your account password for Linux to proceed. So, enter your current account Linux password and hit the Enter key from the keyboard. It will quickly start the installation and configuring manpages in your Linux system. Sit back until it completes the installation process.
After the installation, you can use the below command to check the information about the fsync system.
The output for the man command is appended below. You can read any information regarding it to understand the concept of the “fsync” system call in the Linux system. Press the “q” button from the keyboard to quit this window.
Another prerequisite required to use the fsync system call id the GCC compiler. This is because we are using the fsync system call within the C language. So, to execute and compile C language code, we must have some compiler package installed in our system. So, here we have a GCC compiler. You can install it using the very simple sudo apt command as below. It will complete its installation in a few minutes, and you can easily write C codes in files.
Let’s start working on the fsync command to understand it well. First of all, you have to create a C-type file using the nano editor in the command-line shell. For this, try to run the below simple query in the shell.
After the file has been created and opened, it’s time to write some C code. So you can see the below long code in the nano file. The first 7 lines have header files that are necessary to be included in the editor file for the proper working of the “fsync” system call. After these header files, we have defined the main function with integer return type as “int”. This function contains two parameters. One of them is an integer type, and the other one is a character type array. In the space of the main function, we have declared a character type string “*str” and defined a string type value to it. After that, we have also declared two integer type variables which will be used as file descriptors in further lines. In the next line, we have used the create system call function to create a new file named “test.txt” and read and write privileges as an offset in parameter. We have returned this file content to a file descriptor “fd”. We have defined if statement in this C code. If the value or index pointer of the file descriptor is “-1” it will throw the error to call the creat() function to create a new file. The descriptor “fd” less than “-1” means that the pointer is at a -1 position.
The exit() function will be called before the closing of the if statement. Next, we have been using the write method call to collect the string value from the buffer and write it to the file “test.txt” using the file descriptor “fd”. This data has been returned to the variable “ret”. If the value of variable “Ret” is less than -1, this means that the data has not been written to a file. So, you have to again call the write function and exit the if statement. We have defined the fsync system call and give it a file descriptor “fd” as a parameter value to sync the file descriptor to a disk device. Upon calling the “close” method, it will quickly write the data into a disk drive and close the file descriptor. After that, the main function will be closed. Save this file using a keyboard shortcut key, “Ctrl+S,” and close the file “new.c” using the “Ctrl+X” shortcut key.
After closing the nano file, now it’s time for you to compile the above-presented code. For the compilation of code, we have to use the gcc compiler. So, execute the below gcc query to compile the “new.c” file.
Now, we have to execute this code using the simple a.out command in the shell as below. This command will simply execute the code and return nothing, but the data has been written to a disk drive in the back of the process.
Check the file that has been created in the C code, and it has a string value in it.
We have briefly discussed a very simple and linear example of the fsync system call to understand its concept.I hope now you can easily use the fsync system call in Linux.