A brief talk on some common functions of Linux under the summary (must see article) _linux

Source: Internet
Author: User
Tags exit in

1.exit () function

Exit (int n) is actually a direct exit program,

Because the default standard program entry is int main (int argc, char** argv), the return value is type int.

Generally under the shell, run a program, and then use the command echo $? You can get the return value of the program, which is the exit value, in main (), you can use returns N, also can directly with exit (n) to do. The UNIX default habit of correctly exiting is to return 0, error return not 0.

Emphasis: A separate process is returned to the operating system. If it is a multiple process, it is returned to the parent process.

Call the Waitpid () function in the parent process to get the status of the child process exit for different processing

The return value cannot exceed 255.

There's a definition in stdlib.h.

#define    exit_success    0 
#define    exit_failure    

The termination of the C program is divided into two categories: normal termination and abnormal termination.

Normal termination is divided into: return, exit, _exit, _exit, Pthreade_exit

The abnormal middle finger is divided into: Abort, SIGNAL, thread response cancellation

Mainly say the first 4 kinds of normal termination, that is, the exit series functions.

#include <stdlib.h>
void exit (int status);
void _exit (int status);
#include <unistd.h>
void _exit (int status);

The difference between the above 3 functions is:

Exit () (or return 0) invokes standard I/O cleanup programs (such as fclose) that terminate handlers and user space, and _exit and _exit are not invoked and are cleaned directly by the kernel.

#include <stdlib.h>
int atexit (void (*function) (void))

Return value: Successfully returns 0, failure returns to zero.

ISO C stipulates that a process can register 32 termination processing functions, which are automatically invoked by exit in the reverse order of registration. If the same function is registered more than once, it will be called multiple times.

#include <stdlib.h>
#include <unistd.h>
static void My_exit1 ()
{
printf (" Handlern\n ");
}

static void My_exit2 ()
{
 printf ("Second exit handlern\n");
}

int main ()
{
 if (atexit (my_exit2)!= 0)
printf ("Can ' t register my_exit2n\n");
 if (atexit (my_exit1)!= 0)
printf ("Can ' t register my_exit1n\n");
 if (atexit (my_exit1)!= 0)
printf ("Can ' t register my_exit1n\n");

 printf ("Main is donen\n");
 return 0;
}
addia@addia-lenovo-b470:~$./test Main is Donen-A-Handlern-i-
exit Handlern
Exit Handlern

The above is a small series for everyone to talk about Linux under some of the common functions of the summary (must read) all content, I hope that we support cloud Habitat Community ~

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.