The role of Exit () and _exit () functions in Linux

Source: Internet
Author: User
Tags exit data structures printf

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 exit () function is used to stop running directly, clear the memory space it uses, and clear its various data structures in the kernel; the _exit () function does some packaging on this basis. A number of operations were added prior to execution of the exit. The most important difference between the exit () function and the _exit () function is that the exit () function checks the file's opening before calling the exit system, 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>
    
int main (void)
{
    printf ("Using exit...\n");
    printf ("The content in Buffer");
    Exit (0);
}

Output information:

Using exit ...
    
The content in Buffer
    
#include <unistd.h>
#include <stdio.h>
    
int main (void)
{
    printf ("Using exit...\n");
    printf ("The content in Buffer");
    _exit (0);
}

The output is only:

Using exit ...

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.

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.