1ATEXIT(3)                  Linux Programmer's Manual                 ATEXIT(3)
2
3
4

NAME

6       atexit - register a function to be called at normal process termination
7

SYNOPSIS

9       #include <stdlib.h>
10
11       int atexit(void (*function)(void));
12

DESCRIPTION

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       The same function may be registered multiple times: it is  called  once
20       for each registration.
21
22       POSIX.1-2001  requires that an implementation allow at least ATEXIT_MAX
23       (32) such functions to be registered.  The actual limit supported by an
24       implementation can be obtained using sysconf(3).
25
26       When  a child process is created via fork(2), it inherits copies of its
27       parent's registrations.  Upon a successful call to one of  the  exec(3)
28       functions, all registrations are removed.
29

RETURN VALUE

31       The  atexit()  function returns the value 0 if successful; otherwise it
32       returns a non-zero value.
33

CONFORMING TO

35       SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
36

NOTES

38       Functions registered using atexit() (and on_exit(3)) are not called  if
39       a process terminates abnormally because of the delivery of a signal.
40
41       If  one  of the functions registered functions calls _exit(2), then any
42       remaining functions are not invoked, and the other process  termination
43       steps performed by exit(3) are not performed.
44
45       POSIX.1-2001  says  that  the  result of calling exit(3) more than once
46       (i.e., calling exit(3) within a function registered using atexit(3)) is
47       undefined.   On  some  systems  (but  not Linux), this can result in an
48       infinite recursion; portable programs should not invoke exit(3)  inside
49       a function registered using atexit(3).
50
51       The  atexit()  and  on_exit(3) functions register functions on the same
52       list: at normal  process  termination,  the  registered  functions  are
53       invoked in reverse order of their registration by these two functions.
54
55       POSIX.1-2001 says that the result is undefined if longjmp(3) is used to
56       terminate execution of one of the functions registered atexit().
57
58   Linux Notes
59       Since glibc 2.2.3, atexit() (and  on_exit(3))  can  be  used  within  a
60       shared  library  to establish functions that are called when the shared
61       library is unloaded.
62

EXAMPLE

64       #include <stdio.h>
65       #include <stdlib.h>
66       #include <unistd.h>
67
68       void
69       bye(void)
70       {
71           printf("That was all, folks\n");
72       }
73
74       int
75       main(void)
76       {
77           long a;
78           int i;
79
80           a = sysconf(_SC_ATEXIT_MAX);
81           printf("ATEXIT_MAX = %ld\n", a);
82
83           i = atexit(bye);
84           if (i != 0) {
85               fprintf(stderr, "cannot set exit function\n");
86               exit(EXIT_FAILURE);
87           }
88
89           exit(EXIT_SUCCESS);
90       }
91

SEE ALSO

93       _exit(2), exit(3), on_exit(3)
94

COLOPHON

96       This page is part of release 3.22 of the Linux  man-pages  project.   A
97       description  of  the project, and information about reporting bugs, can
98       be found at http://www.kernel.org/doc/man-pages/.
99
100
101
102Linux                             2008-12-05                         ATEXIT(3)
Impressum