1ATEXIT(3) Linux Programmer's Manual ATEXIT(3)
2
3
4
6 atexit - register a function to be called at normal process termination
7
9 #include <stdlib.h>
10
11 int atexit(void (*function)(void));
12
14 The atexit() function registers the given function to be called at nor‐
15 mal process termination, either via exit(3) or via return from the pro‐
16 gram's main(). Functions so registered are called in the reverse order
17 of their registration; no arguments are passed.
18
19 POSIX.1-2001 requires that an implementation allow at least ATEXIT_MAX
20 (32) such functions to be registered. The actual limit supported by an
21 implementation can be obtained using sysconf(3).
22
23 When a child process is created via fork(), it inherits copies of its
24 parents registrations. Upon a successful call to one of the exec()
25 functions, all registrations are removed.
26
28 The atexit() function returns the value 0 if successful; otherwise it
29 returns a non-zero value.
30
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35
36 void bye(void) {
37 printf("That was all, folks\n");
38 }
39
40 int main(){
41 long a;
42 int i;
43
44 a = sysconf(_SC_ATEXIT_MAX);
45 printf("ATEXIT_MAX = %ld\n", a);
46
47 i = atexit(bye);
48 if (i != 0) {
49 fprintf(stderr, "cannot set exit function\n");
50 return EXIT_FAILURE;
51 }
52 return EXIT_SUCCESS;
53 }
54
56 Since glibc 2.2.3, atexit() (and on_exit()) can be used to within a
57 shared library to establish functions that are called when the shared
58 library is unloaded.
59
60 Functions registered using atexit() (and on_exit()) are not called if a
61 process terminates abnormally because of the delivery of a signal.
62
64 SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
65
67 _exit(3), exit(3), on_exit(3)
68
69
70
71 2003-11-01 ATEXIT(3)