Difference between exit () and _ exit ()

Source: Internet
Author: User

Differences between exit () and _ exit ()

 

Linux source code

# DEFINE _ nr_exit 1

# DEFINE _ nR _ exit _ nr_exit/* from the File Include/asm-i386/unistd. H */
"_ NR _" is the prefix for each system call in the Linux source code. Note that there are two underscores before the first exit and only one underline before the second exit.

Linux core also provides some C language function libraries that pack and expand system calls because these libraries are closely related to system calls, these functions are also called system calls. # Define indicates that the two systems in the kernel call _ exit and exit are the same, but the C library functions _ exit () and exit () are different after encapsulation.

The exit () function is defined in stdlib. h, and the _ exit () function is defined in unistd. h;

The exit () function has the simplest function: directly stop the process, clear the memory space it uses, and destroy various data structures in the kernel; exit () some functions are encapsulated based on these elements and several processes are added before execution and exit. This is also the reason why some people think that exit is no longer a pure system call.

The biggest difference between the exit () function and the _ exit () function is that the exit () function checks the file opening before calling the exit system, and writes the content in the File Buffer back to the file, this is the so-called "clear I/O buffer ".

There are many differences between 'exit () 'and' _ exit () 'when using 'fork ()', especially 'vfork.

The basic difference between 'exit () 'and' _ exit () 'is that the user-mode constructs) clean-up and user-defined purge programs are called. The user-defined purge program is defined by the atexit function and can be defined multiple times, and executed in reverse order). Correspondingly, the last function only performs kernel cleanup for the process.
In the sub-process Branch created by 'fork () ', it is incorrect to use 'exit ()' normally, this is because the buffer of the standard input output (Translator's note: stdio: Standard Input Output) is cleared twice, and the temporary file is deleted unexpectedly: the temporary file is created by the tmpfile function in the temporary directory of the system, and the file name is randomly generated by the system ). In the C ++ program, the situation is worse, because the Destructors of static objects can be incorrectly executed. (In some special cases, such as Daemon, their parent processes need to call '_ exit ()' instead of the Child processes. The basic rule for most cases is, 'exit () 'It is called only once after each entry to the 'main' function .)
In the sub-process Branch created by 'vfork () ', the use of 'exit ()' is more dangerous because it affects the status of the parent process.

 

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/rzphenix/archive/2008/10/09/3043964.aspx

 

 

 

_ Differences between exit and exit
In the standard library functions of Linux, there is a set of functions called Advanced I/O. The well-known printf, fopen, fread, and fwrite are all listed here. They are also called buffer I/O. It is characterized by a buffer corresponding to each opened file, which has a buffer in the memory. Each read file will read several more records, in this way, the next time you read a file, you can directly retrieve it from the memory cache. Each time you write a file, it is only the buffer to be written to the memory, waiting for certain conditions to be met (reaching a certain number, or when a specific character, such as line feed and file Terminator EOF) is encountered, and then the buffer content is written to the file at one time, which greatly increases the speed of file reading and writing, but it also brings us a little trouble in programming,If some data is written to a file, they are saved in the buffer because they do not meet the specific conditions. In this case, we use the _ exit function to close the program directly, data in the buffer zone will be lost. Otherwise, if you want to ensure data integrity, you must use the exit function.

The biggest difference between the exit () function and the _ exit () function is that the exit () function checks the file opening before calling the exit system, and writes the content in the File Buffer back to the file,Is the "clear I/O buffer" item in the figure.

The exit () function is defined in stdlib. h, and the _ exit () function is defined in unistd. h. Both exit () and _ exit () are used to terminate a function normally. But _ exit () is directly called by a sys_exit system, while exit () is usually a function in a common function library. It will first execute some cleanup operations, such as calling and executing various termination processing functions, closing all standard Io, and then calling sys_exit.
For example:

Int main ()
{Printf ("this is a test function !!! /N ")
Printf ("test exit ");
Exit (0 );
}
Int main ()
{Printf ("this is a test function !!! /N ")
Printf ("test exit ");
_ Exit (0 );
}

For the two programs, program 1 outputs two sentences, while Program 2 Outputs only one sentence. The reason is that there are no special characters in the last sentence, such as line breaks and file Terminator, because the files are stored in the buffer zone at this time, in this example, files are stored in the buffer of the enabled display device. To write the output function, you must first call the system. At this time, the system will open the buffer of the display device, the function of exit is to end and clean up. That is to say, first check the buffer zone and write unwritten data to the file. While _ exit is to close the file immediately, the content of the file Buffer Zone disappears, at this time, it is impossible to output to the display device.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/goodlixueyong/archive/2010/11/15/6011021.aspx

 

 

 

 

 

 

 

 

 

 

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.