Atexit () function

Source: Internet
Author: User
Atexit function name: atexit

 

Header file: # include <stdlib. h>

 

Function: registers the termination function (that is, the function called after the main execution ends)

 

Usage: int atexit (void (* func) (void ));

 

Note: according to iso c, a process can register up to 32 functions, which will be automatically called by exit. The function type registered by atexit () should be void functions that do not accept any parameters. The order in which exit calls these registration functions is the opposite of the order in which they are registered. If the same function is registered multiple times, it will also be called multiple times.

 

Program example:

 

# Include <stdio. h>

 

# Include <stdlib. h>

 

Void exit_fn1 (void)

 

{

 

Printf ("exit function #1 called \ n ");

 

}

 

Void exit_fn2 (void)

 

{

 

Printf ("exit function #2 called \ n ");

 

}

 

Int main (void)

 

{

 

/* Post exit function #1 */

 

Atexit (exit_fn1 );

 

/* Post exit function #2 */

 

Atexit (exit_fn2 );

 

Return 0;

 

}

 

Output:

 

Exit function #2 called

 

Exit function #1 called

 

Process Termination method:

 

There are eight ways to terminate the process, the first five of which are normal termination, they are

 

1: return from main

 

2: Call exit

 

3: Call _ exit or _ exit

 

4: The last thread returns from its startup routine

 

5: The last thread calls pthread_exit.

 

There are three types of exceptional termination:

 

6: call abort

 

7. receive a signal and terminate

 

8: The last thread responds to the cancellation request.

 

# Include <stdlib. h?

 

Void exit (INT status );

 

Void _ exit (INT status );

 

# Include <unistd. h>

 

Void _ exit (Status );

 

None of the call _ exit and _ exit will call the termination program.

 

No exception will be terminated.

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.