C Programming

Fallocate 2 C Function

“The fallocate () function is used so that the caller can directly alter the disc space allocated for the file that is referenced by fd for a byte-range beginning at offset and going for len bytes. Using fallocate, you can deallocate or pre-allocate the disc space that has been assigned to a file. Preallocation is completed rapidly for filesystems that implement the fallocate system call by allocating blocks and designating them as uninitialized, needing no IO to the data blocks. This is a lot quicker than creating a file by putting all zeros in it. The fallocate () function returns a success or failure exit status of 0 or 1.

In this manual, we’ll go over this idea in great detail and give examples of C programs that use the “fallocate ()” function. Additionally, we will go over the “fallocate ()” function’s syntax in this manual and demonstrate how to send parameters to it when programming in C.”

The following is the “fallocate ()” function’s syntax: You may easily grasp the parameters of this “fallocate ()” function in C code by taking a look at this.

int fallocate (int fd, int mode, off_t offset, off_t len);

The operation to be passed out on the particular range is determined by the mode parameter. The fallocate () allocates disc space in the “offset” and “len”-specified range as its default operation (mode is zero). If “offset and len” are more than the file range, the file size will adjust. Any subregion in the range defined by “offset” and “len” that was empty prior to the call will have its initial value set to zero. This default behavior, which aims to implement the POSIX fallocate (3) library function as efficiently as possible, is very similar to that of that function.

A lucrative call guarantees that further writes into the range defined by “len” and “offset” will not be unsuccessful due to insufficient disc space.

Example # 1

The examples in this manual will all utilize Ubuntu 20.04. Installing GCC requires running a few instructions after installing Ubuntu 20.04. Installing the GCC compiler will enable us to run this C programming code in Ubuntu 20.04. When Ubuntu 20.04 has this compiler loaded, we open the text editor for coding and begin entering lines of code that will use the C programming language’s “setenv ()” function.

At the start of the C code, we employ several header files. The only thing we need to do while writing C code is to include the header files; otherwise, our C code won’t have the functions we need. As you can verify in this sample, we have the “fcntl. h” header file. Then the standard input and output header file, “stdio.h,” was positioned on the second line of the code. Then we add a second header file, “errno”, The integer value errno, which is produced by system calls as well as some library methods in the event of an error to identify something went wrong, is defined in the errno.h header file of the C programming Standard Library.

After this, we have “string. h,” which is also a header file that includes the function’s declaration, types as well as string handling functions. Now, we have the “main ()” function, and we also need to declare this function as an “int” data type, so we put this function here as “int main ()”. We then declare a variable “fd” of the “int” data type and place the “open ()” function here. We utilize this “open ()” function that is used for reading, writing, or opening the file for both.

When you are not working in the same directory as the file, use an absolute path that begins with “/”. When working in the same directory as the file, use a relative path, which consists of just the file name and its extension. Here, the “0_WRONLY” means we are opening the file for writing only, and the “O_CREAT” is used to receive a warning if the file is already present. The S_IRUSR is a global constant found in the POSIX sys/stat. h file. User read authorization bit is what it is called. The prefix S_ could mean “status of,” “RUSR,” or “Read of User”. Then we have “S_IWUSR”.

For the file’s owner, set the write authorization bit. Below, we have the “if” condition, and we place the condition that “fd” is equal to “-1”. If the condition is true, then the “perror ()” function is utilized. This “perror” is utilized for printing the error message to stderr. Then we again declare and initialize the “int” variable named “status” and place the “posix_fallocate” function here and pass three parameters. The “fd” is the first parameter, “0” is utilized as the second parameter, and then we have “100” as the third parameter of this “fallocate” function.

Below this condition of the “if” statement is “status! =0”. If it is true, then the “printf” statement will be executed for printing the error on the screen, and we also have the “return 0” at the end of this code.


This manual demonstrates how to utilize the “fallocate ()” C programming function. The “fallocate ()” function has been fully described here, along with examples of how to use it in C programming. To help you understand how to use the C “fallocate ()” function and how it returns the result, we have also included the sample code. As was already explained, the “fallocate ()” method in C programming is used so that the caller can directly alter the disc space allocated for the file that is referenced by fd for a byte-range beginning at offset and going for len bytes. We also spoke about the fact that some parameters must be passed to the “fallocate ()” function. We anticipate that this manual will aid in your understanding of C programming functions, and you will be able to do it by yourself.

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.