Linux C Development: processing when the program exits

Source: Internet
Author: User
Tags function prototype

Sometimes, you want to be able to do some processing when the program exits, such as saving state and releasing some resources. C language development of the Linux program, there may be normal exit (exit), there may be abnormal crash, and abnormal crash may be in response to a signal of the default processing. Here is a summary of these situations, how to get a unified exit processing point, White is to write a callback function, let him when the program normal or abnormal exit call.

    • Look at the normal exit, that is, call exit or the main function return or the last thread exits normally, how to capture the Exit event.
      Use the Atexit function. Header file: #include<stdlib.h> , function prototype: void atexit(void (*func)(void));
      Atexit can be called multiple times, registering multiple callback functions, called when the process exits, and the same function can be registered multiple times. Examples of Use:
void Server_on_exit (void) {    //do something when process exits}int main (int argc, char *argv[]) {    atexit (server_on _exit);    return 0;}

  

    • Looking at abnormal exits, such as abort, the default handling for some signal is an exception exit, and a direct kill process.
      The processing method is to use the signal function, register its own callback function, and avoid the system default callback, the system default callback may be directly crash out the program. Examples of Use:
#include <signal.h>void signal_crash_handler (int sig) {    server_backtrace (SIG);    Exit (-1);} void Signal_exit_handler (int sig) {    exit (0);} int main (int argc, char *argv[]) {    atexit (server_on_exit);    Signal (SIGTERM, signal_exit_handler);    Signal (SIGINT, signal_exit_handler);    Ignore Sigpipe    signal (sigpipe, sig_ign);    Signal (Sigbus, signal_crash_handler);     Bus error    signal (SIGSEGV, signal_crash_handler);    SIGSEGV, illegal memory access    signal (SIGFPE, signal_crash_handler);       SIGFPE, mathematical-related anomalies, such as being removed by 0, floating-point overflow, etc.    signal (SIGABRT, signal_crash_handler);     SIGABRT, generated by the call to abort function, the process is not normally exited    return 0;}

  

This example is in fact the exception exit processing and normal exit processing combined. For Sigterm (that is, the kill process) and SIGINT (that is, the ctrl-c end foreground process), we assume that exit is normal, and in its signal processing function, exit (0) is called directly, and exit (0) is captured by Server_on_exit. For exception exits are similar, except that the exit (-1) call is an exception. At the same time abnormal exit we will print out the current process stack information, server_backtrace implementation of the next article. Also note that the Sigkill signal is not captured. The exit caused by the call to abort is also captured by the SIGABRT signal to be processed. Other types of abnormal exit signals are also common and are captured for processing. So for the exception exit, we can unify the log stack information, and can continue the normal exit process.

Linux C Development: processing when the program exits

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.