Difference linuxC library function _exit and exit of

1 source

In the linux standard library function, called a set of high-level I / O functions, and we know printf, fopen, fread, fwrite are on that list, they are also known as buffered I / O. Characterized by each corresponding to an open file, there is a buffer in memory has a buffer, each file is read to read a number of records, so the next file can be read directly from the memory cache withdrawn, each time writing the file is only written to the buffer memory, waiting to meet certain conditions (up to a certain number of specific characters or face, such as line feed, and the EOF end of file), and then the contents of the buffer one-time write to the file, thus greatly increasing the speed of file read and write, but it also brings a little bit of trouble for our program, if some of the data, we believe that the file has been written, in fact, because certain conditions are not met they just saved in the buffer, then we will direct the program closes with _exit function, data in the buffer will be lost, on the contrary, if you want to ensure data integrity, you must use the exit function.

2, the main difference

exit () function is the biggest difference with the _exit () function is that the exit () function before calling the exit system call to check the file is opened, the contents of the file buffer write the file is the figure of the "clean-up I / O buffer "a.

3, header files

exit () function is defined in stdlib.h, whereas _exit () defined in unistd.h.

4, principle

exit () and _exit () are used to terminate a normal function. But _exit () system call directly is a sys_exit, and exit () function is often a common function library. It will first perform some cleanup operations, such as call termination processing to perform various functions, such as closing all standard IO, then call sys_exit.

5, for example

int main(int argc , char* argv[])
{

printf(“hello\n”)
  printf(“world”);
  exit(0);
}
int main(int argc , char* argv[])
{

printf(“hello\n”)
  printf(“world”);
  _exit(0);
}

These two programs, the program output is two words:

hello

world

The program 2 output sentence:

hello

The reason is because there is no character with a special on the last word, such as line breaks and end of file, because this time the file is stored in the buffer display buffer device, in this case the file is stored in the open, because the output to write a function, we must first make a system call, this time the system will open a buffer of a display device, and the role of exit is finished, clean up, that is to check the buffer, the write data is not written to the file, _exit is immediately close the file, the contents of the file buffer will disappear, and this time it is impossible to re-export to the display device.

Published 56 original articles · won praise 6 · views 6845

Guess you like

Origin blog.csdn.net/qq_23929673/article/details/99670478