Exit Function and _exit

Source: Internet
Author: User

As you can see, the _exit function is to stop the process from running, clear its used memory space, and clear its various data structures in the kernel; the exit function does some tricks on these bases and adds several operations to the exit before executing. The most important difference between the exit () function and the _exit () function is that the exit () function checks the opening of the file before calling the exit system, and writes the contents of the file buffer back to the file. That is, "clean I/O buffering" in the figure.

Header file required: Exit: #include <stdlib.h>

_exit: #include <unistd.h>

Function prototype: exit:void exit (int status)

_exit:void _exit (int status)

Function pass-through value: status is an integer parameter that can be used to pass the state at the end of the process. Generally speaking, 0 means the normal end; other values indicate an error and the process is not properly terminated. When actually programmed, the parent process can take advantage of the wait system call to receive the return value of the child process, thus handling different things differently.

Exit () and _exit () instance analysis

printf (const char *FMT,...) The function uses buffered I/O, which automatically reads the record from the buffer when it encounters a "\ n" line break.

< code samples >

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>


int main ()
{

pid_t result;
result = fork ();
if (result<0)
Perror ("fork");
if (result = = 0)
{
printf ("This is _exit test\n");
printf ("The content in the buffer000");
_exit (0);
}
Else
{
printf ("This is Exit test\n");
printf ("The content in the buffer");
Exit (0);
}
return 0;
}

Here is the result of the operation:

Results Analysis: running _exit (0) in the sub-process does not print thisis the content in the buffer000, and exit (0) running in the parent process will thisis the content in the buffer Printed out. Note that exit (0) clears the buffered I/O content before terminating the process, so even if no \ n is printed in printf, _exit (0) terminates the process directly and does not clean up the buffered I/O content, so it will not be printed.

In the standard library of Linux, there is a set of functions called "Advanced I/O", which are known as printf (), fopen (), Fread (), Fwrite(), which are also called " buffered I/O (buffered i/ O) ", which is characterized by the corresponding to each open file, in memory has a buffer, each time the file read, will read a number of records, so that the next time the file can be read directly from the memory buffer, each time the file is written, it is only written in memory buffer, etc. meet a certain condition ( reach a certain number, or encounter certain characters, such as newline and file Terminator EOF, and then write the contents of the buffer once to the file, which greatly increases the speed of file read and write, but also for our programming a little bit of trouble. If there are some data that we think have been written to, in fact, because we do not meet certain conditions, they are only in the buffer, then we use the _exit () function to close the process directly, the data in the buffer is lost, conversely, if you want to ensure the integrity of the data, Be sure to use the exit () function.

Exit Function and _exit

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.