A brief comparison of the three function _c languages used in the C language to exit the process

Source: Internet
Author: User

C language _exit () function: End Process Execution
header file:

#include <unistd.h>

To define a function:

void _exit (int status);

Function Description: _exit () is used to immediately end the execution of the current process, return the parameter status to the parent process, and close the file that is not closed. This function call does not return, and the SIGCHLD signal is passed to the parent process, which can take the end state of the child process by the wait function.


Additional note: _exit () does not process standard I/O buffers, use exit () if you want to update buffers.

C language On_exit () function: Set the function that is called before the program ends normally
header file:

#include <stdlib.h>

To define a function:

int On_exit (void (* function) (int void*), void *arg);

Function Description: On_exit () is used to set a function that is called before the program ends normally. When a program calls exit () or returns from Main, the function specified by the parameter function is invoked before the program is actually terminated by exit (). The argument arg pointer passes to the argument function function, see the example.

Return value: Returns 0 if the execution succeeds, otherwise returns-1, the reason for the failure is stored in errno.

Example

#include <stdlib.h>
void my_exit (int status, void *arg)
{
  printf ("Before exit ()!\n");
  printf ("exit (%d) \ n", status);
  printf ("arg =%s\n", (char*) arg);
}
Main ()
{
  char * str = ' test ';
  On_exit (My_exit, (void *) str);
  Exit (1234);
}

Perform:

Before exit ()! Exit (1234) arg = Test

C language Atexit () function: Set the function that is called before the program ends normally
header file:

#include <stdlib.h>

To define a function:

int atexit (void (*function) (void));

Function Description: Atexit () is used to set a function that is called before the program ends normally. When a program calls exit () or returns from Main, the function specified by the parameter function is invoked before the program is actually terminated by exit ().

Return value: Returns 0 if the execution succeeds, otherwise returns-1, the reason for the failure is stored in errno.

Example

#include <stdlib.h>
void my_exit (void)
{
  printf ("Before exit ()!\n");
}
Main ()
{
  atexit (my_exit);
  Exit (0);
}

Perform:

Before exit ()!

Related Article

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.