Note: We will be using ubuntu 20.04 for performing the specific process and its atexit function.
Working of the atexit Function:
This atexist function will register a function that you want to call it in the main process. It causes the normal program termination. After execution, If it returns 0, it means the function call is successful. If it returns -1 or any other value, the atexit function failed to call the registered function if any open-source file is opened. In contrast, the termination, atexit function will close all those files at the time of execution.
Step # 1: Create a Text Editor to write a program:
Before performing an atexit function, firstly, you have to create a C language program in your Linux server’s text editor, using the following command at your Linux terminal.
The “gedit” utility will take some time to open, after which you will be able to use it immediately. It will allow opening a text editor in which you can write your code. The text editor will appear like the image given below:
Step # 2: Code a C program Using atexit Function in Text Editor:
For using the atexit function, firstly, you have to write a simple C language code to apply the function in it. There is a C language code in the below-given image in which there are two functions one is in the main class, and another one is registered in the atexist function call.
Save this above image code with any name using extension code.c. This format shows that it is a C language code, and it allows you to show the functions and library of the C language below given is the example.
Step # 3: Execute the process to check the Output of the program:
To execute the code, firstly, you have to go back to the terminal of the Linux server. For executing, you need to give the command of “gcc –o test” and the name of the created C file. Here is the command below:
This will show you an error if the gcc compiler is not installed in your system.
Now you have to install gcc by giving the command “sudo apt install gcc” at your Linux terminal as shown below:
By hitting the enter key, it will start installing gcc in your Linux server. It will take some time; after that, it will ask you to enter yes/no, at that time. You have to enter yes; then it will start showing the progress of gcc compiler downloading as shown in the image below:
As soon as you have installed the gcc compiler on your Linux server, you can successfully compile your code in it. For getting the final output, you have to use the. /test command as below:
This command will display the output of your code. You can view the output of your code in the below-cited image.
As shown above, “Existing Successfully” is the created function registered and called by the atexit function in the main code of the program. This output shows that the function has registered successfully. If it is not registered, it will give the output as -1.
Another Example of atexit Function using Multiple Registered Functions To be Called:
For knowing more clearly how this function works, you may have another example with different code execution. Repeat the same process to write a code as mentioned above by using the command “gedit” to open the text editor and after writing your code, save it with any name by mentioning the .c extension. As shown in the below image, you can view that the multiple functions are called by atexit function in the same program.
In the above image, you can see three empty functions are called and registered by using the atexit function in the main code. To get the output of this code, you have to perform the same command as mentioned earlier. Open the terminal of your Linux server and execute the command “gcc –o test “along with the file that is created earlier. After the successful compilation of the code, you have to execute the following cited instruction in the terminal.
Here you can see all the three called functions are registered and working successfully.
Some Specific Uses of the “atexit” Function:
Above, we have seen a very comprehensive Linux server using the atexit function to normalize the executed code and have desired atexit status. When multiple functions are registered before the code’s termination, you have to use multiple atexit functions to call every registered function.
The article, as mentioned earlier, shows the usage of the atexit function in the C language. We have used the gcc compiler to compile the example code related to the atexit function in the Linux server. In the above article, there are multiple examples to understand the usage of the atexit function and how it works in Linux. Now all you have to do is take advantage of this article to use the atexit function in your Linux server.