Use and difference of exit () and _ exit () in linux

Source: Internet
Author: User
# Include & lt; stdlib. h & gt; voidexit (intstatus); unlike fork, it is difficult to understand. from the exit name, we can see that this system call is used to terminate a process. No matter where the program is located, as long as the exit system call is executed, the process will stop all the remaining operations, clear...
# Include Void exit (int status); it is not as hard to understand as fork. from the exit name, we can see that this system call is used to terminate a process. No matter where the program is located, as long as the exit system call is executed, the process will stop all the remaining operations, clear various data structures including the PCB, and terminate the operation of the process. See the following program:/* exit_test1.c */# include Main () www.2cto.com {printf ("this process will exit! \ N "); exit (0); printf (" never be displayed! \ N ") ;}run after compilation: $ gcc exit_test1.c-o exit_test1 $./exit_test1this process will exit! We can see that the program does not print "never be displayed! \ N ", because before that, when exit (0) is executed, the process has been terminated. The exit system calls the status parameter with an integer type. we can use this parameter to pass the status at the end of the process. for example, if the process ends normally or unexpectedly, in general, 0 indicates that the process has not ended normally. other values indicate that an error has occurred and the process has ended abnormally. In actual programming, we can use the wait system to call and receive the return values of sub-processes, so as to handle different situations. We will introduce the wait details in the future.
For system calls, _ exit and _ exit are a pair of twins. At this time, anyone who understands C language and has a clear mind will say that _ exit and exit are no different, but let's talk about the difference between them, these differences are mainly reflected in their definitions in the function library. _ The prototype of exit in the Linux function library is: # include Void _ exit (int status); the exit function is defined in stdlib. h, while _ exit () is defined in unistd. h, from the name, stdlib. h seems better than unistd. h. What is the difference between them?
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, that is, "clear the I/O buffer ". In the standard library of Linux, www.2cto.com has a set of functions called "advanced I/O". we are familiar with printf (), fopen (), fread (), and fwrite () they are also called "buffer I/O (buffered I/O)". they are characteristic of each open file and have a buffer in the memory, each time a file is read, several more records are read, so that the next time the file is read, it can be directly read from the memory buffer. each time a file is written, it is only written into the buffer zone in the memory, when a certain number of conditions are met, or a specific character, such as the line break \ n and the file terminator EOF, is met, and then the content in the buffer is written to the file at one time, this greatly increases the speed of reading and writing files, but it also brings us a little trouble in programming. If there is some data, we think that the file has been written, because it does not meet the specific conditions, they are only saved in the buffer, then we use _ exit () if a function is used to directly shut down the process, data in the buffer will be lost. if you want to ensure data integrity, you must use the exit () function. See the following routine/* exit2.c */# include Main () {printf ("output begin \ n"); printf ("content in buffer"); exit (0) ;}compile and run: $ gcc exit2.c-o exit2 $. /exit2output begincontent in buffer/* _ exit1.c */# include Main () www.2cto.com {printf ("output begin \ n"); printf ("content in buffer"); _ exit (0);} compile and run: $ gcc _ exit1.c-o _ exit1 $. /_ exit1output begin author lp4083331
Related Article

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.