Understanding of the Atexit function and the Exit function

Source: Internet
Author: User

follow iso C provisions , a process can enlist up to functions, which will be exit automatically called, usually this 32 functions are called termination handlers and called atexit function to register these functions.

we usually thinkCThe starting function of the language isMainfunction, essentially a program's startup function is not necessarilyMainfunction, which can be set using the linker, butGCCthe defaultMainis thatCthe entry function of the language, inMainbefore the function starts, the kernel invokes a special startup routine that takes command-line parameter values and environment variable values from the kernel, and callsMainfunction is prepared so that the corresponding programMainThe function is not a start, but corresponds toClanguage,MainThe function is the entry address, and the other linker helps us to do it, actuallyMianThe function is executed using theexecfunction, which is a family of functions, which is the only way the kernel executes a program, which is analyzed in the Process Control section.

Remember that there is a question in the interview about Main after the function exits, is it possible to execute the program's problem, using the previously mentioned atexit function.

#include <stdlib.h>int atexit (void(*func) (void));

where atexit parameter is a function address, and when this function is called without passing any arguments, the function cannot return a value, atexit function is called termination handler registration program, after registration is completed, when function termination is exit () function will invoke the previously registered functions, but exit The function calls these functions in the order in which they are registered is reversed, and I think this is essentially the result of parametric compaction, and the parameters are first in and out due to the stacking order. At the same time, if a function is registered multiple times, the function will be executed more than once.

  We know Exit is in Main called after the function call has ended, so the execution of these functions must be Main function, this is also the solution to the above interview question. That is , the atexit function is used to register the relevant execution function.

   in theExitin the introduction of the function, we know thatexit ()and the_exit ()as well_exit ()the essential difference between functions is whether to enter the kernel immediately,_exit ()as well_exit ()functions are entered into the kernel immediately after the call, and no cleanup is performed, butexit ()some cleanup processing is performed, which is why there areatexit ()the reason for the function, becauseexit ()functions need to be cleaned up, and a series of operations need to be performed, which is actually the actual execution of various so-called cleanup operations. atexitthe definition of a function also gives the programmer a way to useExitdo some cleanup operations, such as some programs require additional operations, the specific cleanup operation can use this method to remove the special operations, and so on.

1# include <stdio.h>2# include <stdlib.h>3 4 voidF1 (void){5printf"The first exit handier\n");6 }7 8 voidF2 (void){9printf"The second exit handier\n");Ten } One  A intMainvoid) - { -     if(0!=atexit (F1)) { thePerror ("fail to set exit Handier"); -Exit1); -     } -     if(0!=atexit (F1)) { +Perror ("fail to set exit Handier"); -Exit1); +     } A     if(0!=atexit (F2)) { atPerror ("Dail to set exit Handier"); -Exit1); -     } -     return 0; -}

The specific execution results are as follows:

The second exit Handierthe first exit Handierthe first exit Handier

   according toExitimplementation of this,Exitthe individual termination handlers are called first, and then are called as many times as neededfclose (),Close all open streams, which meansExitfunction executes a standardI/OCleanup Close operation of the library: calls to all open streamsfclose (), which causes all buffered output data to be flushed and written to the file.

   The only way the kernel makes the program execute is to call aexecfunction, the only way for a process to terminate voluntarily is to explicitly or implicitly invoke the(throughExitfunction) _exit ()or_exit ()function. SoExitThe essence of the function is the_exit ()or_exit ()the encapsulation of the function. Exitexecutes the custom termination handler first, and then executes theI/Olibrary function Cleanup functionfclose (),This is why you can continue to use it in the Terminate handler function.printfthe reason for such a function, becauseI/Othe Stream object of the library function has not been cleared and can of course continue to be used. Done all thefclose ()later, you can perform a real termination function_exit ()or_exit ()function.

Reference: http://blog.chinaunix.net/uid-20937170-id-3447901.html

Understanding of the Atexit function and the Exit function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.