In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what is the difference between exit and exit in Linux system commands". In daily operation, I believe that many people have doubts about the difference between exit and exit in Linux system commands. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the difference between exit and exit in Linux system commands?" Next, please follow the editor to study!
Note: exit () is the exit, the input parameter is the status code when the program exits, 0 means normal exit, other means abnormal exit, generally use-1 or 1, standard C has EXIT_SUCCESS and EXIT_FAILURE two macros, with exit (EXIT_SUCCESS); readability is better.
As far as system calls are concerned, _ exit and exit are twin brothers. To what extent they are similar, we can find the answer from the source code of Linux:
# define _ _ NR__exit _ _ NR_exit / * excerpt from line 334 of file include/asm-i386/unistd.h * /
"_ _ NR_" is a prefix for each system call in the source code of Linux. Note that there are two underscores before the first exit and only one underscore before the second exit. At this point, anyone who knows C and has a clear head will say that there is no difference between _ exit and exit, but let's talk about the difference between the two, mainly in their definition in the function library. The prototype of _ exit in the Linux function library is:
# i nclude void _ exit (int status)
Compared with exit, the exit () function is defined in stdlib.h, while _ exit () is defined in unistd.h. In terms of name, stdlib.h seems to be a little more advanced than unistd.h, so what's the difference between them? The function of _ exit () is the simplest: directly stop the process, clear the memory space it uses, and destroy its various data structures in the kernel; exit () function makes some wrappers on this basis, adding a number of procedures before execution and exit, for this reason, some people think that exit can no longer be regarded as a pure system call. The biggest 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 call and writes the contents of the file buffer back to the file, that is, "clean the Ishock O buffer".
Before exit () ends the process that calls it, it takes the following steps:
1. Call the function registered by atexit () (exit function); call all the functions registered by ATEXIT in the opposite order when it is registered, which allows us to specify to perform our own cleanup action when the program terminates. For example, save program state information in a file, unlock the lock on the shared database, and so on.
2.cleanup (); closes all open streams, which causes all buffered output to be written and all temporary files created with the TMPFILE function deleted.
3. Finally, the _ exit () function is called to terminate the process.
_ exit does 3 things (man): 1 open file descriptors belonging to the process are closed any open file descriptors belonging to the process are closed 2 any children of the process are inherited by process 1, init 3 the process's parent is sent a SIGCHLD signal
After the exit completes the cleanup, it calls _ exit to terminate the process.
In addition, another explanation:
Simply put, the exit function terminates the calling process. Before exiting the program, all files are closed, the buffer output refreshes the definition, and all refreshed "exit functions" (defined by atexit) are called.
_ exit: this function is defined by Posix, does not run exit handler and signal handler, and does not run the flush standard Iexit O stream in UNIX systems.
To put it simply, _ exit terminates the calling process without closing the file, clearing the output cache, or calling the exit function.
Common:
No matter how the process terminates, the kernel closes all file descriptors opened by the process and releases the memory used by the process!
A more detailed introduction:
Calling exit () The exit () function causes normal program termination.
The exit () function performs the following functions:
1. All functions registered by the Standard C atexit () function are called in the reverse order of registration. If any of these functions calls exit (), the results are not portable. 2. All open output streams are flushed (data written out) and the streams are closed.
3. All files created by tmpfile () are deleted.
4. The _ exit () function is called. Calling _ exit () The _ exit () function performs operating system-specific program termination functions. These include: 1. All open file descriptors and directory streams are closed.
If the parent process is executing a wait () or waitpid (), the parent wakes up and status is made available.
3. If the parent is not executing a wait () or waitpid (), the status is saved for return to the parent on a subsequent wait () or waitpid () 4. Children of the terminated process are assigned a new parent process ID. Note: the termination of a parent does not directly terminate its children. 5. If the implementation supports the SIGCHLD signal, a SIGCHLD is sent to the parent. 6. Several job control signals are sent.
Why use the _ exit function instead of the exit function in a child process branch of a fork? There are many differences between 'exit ()' and'_ exit () 'that stand out when using' fork ()', especially 'vfork ()'.
The basic difference between 'exit ()' and'_ exit ()'is that the previous call implements the clean-up related to the user state structure (user-mode constructs) in the call library, and calls the user-defined cleanup program (the custom cleanup program is defined by the atexit function, which can be defined multiple times and executed in reverse order). Correspondingly, the _ exit function only performs kernel cleanup for the process. In child process branches created by 'fork ()', it is incorrect to use 'exit ()' normally, because using it causes the buffer of standard input and output (stdio: Standard Input Output) to be emptied twice, and temporary files are unexpectedly deleted (temporary files are created by the tmpfile function in the system temporary directory, and the file names are randomly generated by the system). The situation is even worse in C++ programs because the destructor (destructors) of the static target (static objects) can be executed incorrectly. (there are also special cases, such as daemons, where the parent process needs to call'_ exit () 'instead of the child process; the basic rule that applies to most cases is that' exit ()'is called only once each time it enters the 'main' function.) In a child process branch created by 'vfork ()', the use of 'exit ()' is more dangerous because it affects the state of the parent process.
# include; # include int glob = 6; / * external variable in initialized data * / int main (void) {int var; / * automatic variable on the stack * / pid_t pid; var = 88; printf ("before vfork\ n"; / * we don't flush stdio * / if ((pid = vfork ()) < 0) printf ("vfork error\ n"; else if (pid = = 0) {/ * child * / glob++; / * modify parent's variables * / var++; exit (0) / * child terminates * / / it is better to use _ exit (0) in a child process. } / * parent * / printf ("pid =% d, glob =% d, var =% d\ n", getpid (), glob, var); exit (0);} running on the Linux system, the content output of the parent process printf: pid = 29650, glob = 7, var = 89
Child processes close their own, although they share standard input, standard output, standard errors and other "open files", when the child process exit, it is only an decrement of a reference count, it is impossible to close the parent process, so the parent process still has output.
On other UNIX systems, however, the parent process may have no output because the child process called e x i t, which refreshes and closes all standard I / O streams, including standard output. Although this is performed by the child process, it is done in the address space of the parent process, so all affected standard Imax O FILE objects are in the parent process. When the parent process calls p r i n t f, the standard output has been turned off, so p r i n t f returns-1.
In Linux's standard function library, there is a set of functions called "advanced buffered O". The well-known functions such as printf (), fopen (), fread () and fwrite () are all listed in this list. They are also known as "buffer Imax O". They are characterized in that there is a buffer in memory for each open file, and several more records are read each time the file is read. In this way, the next time you read a file, you can read it directly from the buffer in memory, and each time you write a file, you only write to the buffer in memory, and so on, when you meet certain conditions (reaching a certain number, or encounter specific characters, such as newline character and file Terminator EOF), and then write the contents of the buffer to the file at once, which greatly increases the speed of reading and writing the file. But it also brings us a little bit of trouble in programming. If there is some data, we think it has been written to the file, in fact, because the specific conditions are not met, they are just stored in the buffer, then we use the _ exit () function to shut down the process directly, the data in the buffer will be lost, on the contrary, if you want 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 function declaration for _ exit is in the unistdh header file.
The following example compares the difference between the two functions. The printf function uses the buffer Istroke O, which automatically reads the record out of the buffer when it encounters a newline character "\ n". The example makes use of this property for comparison.
Exit.c source code
# include # include int main (void) {printf ("Using exit...\ n"); printf ("This is the content in buffer"); exit (0);}
Output information:
Using exit...
This is the content in buffer
# include # include int main (void) {printf ("Using exit...\ n"); / / if "\ n" is not added here, this message may not be displayed on the terminal. Printf ("This is the content in buffer"); _ exit (0);}
Only output:
Using exit...
Explanation: after a process calls exit, the process does not disappear completely immediately, but leaves a data structure called a Zombie. Zombie process is a very special process, it has given up almost all memory space, does not have any executable code, and cannot be scheduled, and only keeps a place in the process list. record the exit status of the process and other information for other processes to collect, in addition, zombie processes no longer occupy any memory space.
# include
Int main () {printf ("% c",'c'); _ exit (0);}
At this point, the study on "what is the difference between exit and exit in Linux system commands" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.