Linux under C Programming: Exit process

Source: Internet
Author: User
Tags assert exit data structures header printf linux
#include <stdlib.h>     
         
voidexit (int status);     
         
Intatexit (void (*function) (void)     
         
inton_exit (void (*function) (int,void *), void arg*)     
         
voidabort (void)     
# include<unistd.h>     
         
void_exit (int status)     
#include <assert.h>     
         
voidassert (int expression)

Atexit: A parameterless function that is registered in it is invoked when exiting. Successfully returned 0 failed return-1, and affected errno

On_exit: The parameter function that is registered in it is invoked when exiting. Successfully returned 0 failed return-1, and affected errno

Assert is a macro definition, check for errors, and exit if an error occurs.

Abort sends a SIGABRT message to end the current process.

Both the exit and _EXIT functions are used to terminate the process. When the program executes to exit or _exit, the system stops all remaining operations unconditionally, clears various data structures including PCB, and terminates the operation of this process. However, there is a difference between the two functions.

The biggest difference between the exit () function and the _exit () function is that the exit () function checks the file's opening before calling Do_exit and writes the contents of the file buffer back to the file.

Because of Linux's standard function library, there is an operation called buffered I/O, which is characterized by each open file and a buffer in memory. Every time you read a file, read a number of consecutive records, so that the next time you read the file can be read directly from the memory buffer; Similarly, every time you write a file is just a buffer to write to the memory, and so meet certain conditions (such as reached a certain number or encountered a certain character, etc.), The contents of the buffer are then written to the file once. This technique greatly increases the speed of file reading and writing, but it also gives the programmer a bit of trouble. For example, there are some data that have been written to the file, in fact, because they do not meet the specific conditions, they are only in the buffer, then use the _exit () function directly shut down the process, the buffer data will be lost. Therefore, to ensure the integrity of the data, you must use the exit () function.

The function of exit is declared in the Stdlib.h header file.

The _exit function is declared in the Unistd.h header file.

The following example compares the differences between the two functions. The printf function is the use of buffered I/O, which automatically reads the record out of the buffer when it encounters the "\ n" newline character. Examples are comparisons made using this property.

EXIT.C Source

#include <stdlib.h>     

#include <stdio.h>     

intmain (void)     
         
{     
    printf ("Using exit...\n");     
    printf ("This is the content inbuffer");     
    Exit (0);     
         
}

Output information:

Usingexit ...

Thisis the content in buffer

#include <unistd.h>     
         
#include <stdio.h>     
         
intmain (void)     
         
{     

    printf ("Using exit...\n");     
    printf ("This is the content inbuffer");     
    _exit (0);     
}

The output is only:

Usingexit ...

Description: After a process calls exit, the process does not immediately complete the hour, but rather leaves behind a data structure called a zombie process (Zombie). The zombie process is a very special process, it has almost abandoned all memory space, no executable code, can not be scheduled, only in the process list to keep a location, record the process of exit status and other information for other processes to collect, in addition, the zombie process no longer occupies any memory space.

View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm

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.