I have been familiar with the atexit function for a long time. The man manual explains atexit as follows:
The atexit() function registers the given function to be called at normal process termination, either via exit() or via the program’s main(). Functions so registered are called the reverse order of their registration; no arguments are passed.
At first glance, the impression is formed: "Oh, the atexit function is used to register A function. After exiting the main function, you need to execute function, the process will completely over ".
Until a segment error occurs at work, the log shows that the thread in the main function is still running happily when the process executes the function registered by atexit, this phenomenon subverts my previous cognition, so I have to rethink when the atexit registration function will be executed? What is "Exit main Function "?
First, a simple code:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> bye( printf( sleep( printf( *do_thread( ( printf( sleep( main( pthread_create(&pid_t, NULL, ( * }
The above program first registers an execution function after the program Exits normally with atexit, then creates a thread to continuously output information, and then the main thread executes exit;
What will the running program output?
I initially guessed that the running result should be:
Before bye
After bye
Or:
-------------- I'm thread!
Before bye
After bye
In my understanding, bye () is executed after exiting the main function. At that time, the threads created in the main function should no longer exist, the code will quietly execute the bye () function. It turns out that I take it for granted.
The actual running result of the above program is:
--------------I --------------I --------------I --------------I --------------I --------------I --------------I --------------I --------------I --------------I after bye
Why is the thread still running when bye () is executed?
Let's take a look at the source code of the exit () function:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sysdep.h> #include #include DEFINE_HOOK (__libc_atexit, ( __run_exit_handlers ( status, exit_function_list ** (*listp != exit_function_list *cur = * (cur->idx > exit_function * f = &cur->fns[--cur-> (f-> (*atfct) ( (*onfct) ( status, * (*cxafct) ( *arg, onfct = f-> onfct (status, f-> atfct = f-> cxafct = f-> cxafct (f-> *listp = cur-> (*listp != exit ( __run_exit_handlers (status, &__exit_funcs, libc_hidden_def (exit)
From the source code above, we can see that exit () First executes the atexit registration function, and then executes the _ exit function. _ exit closes all file descriptors of the process, clean up memory and other kernel cleanup functions. Therefore, when exit () is executed to _ exit (), the previously created thread stops running. The concept of "Exit main Function" in my mind is too abstract. What is behind it is actually an action stream that will execute the function registered by atexit and refresh the stream (stdin, stdout, stderr), writes the content of the file Buffer back to the file, closes all file descriptors of the Process, cleans up the memory and other kernel cleanup functions. The source code of exit () and _ exit () needs to be well studied.
Let's go back to the atexit explanation in the first article.