Linux exit system call

Source: Internet
Author: User

Address: http://blog.sina.com.cn/s/blog_4850a7880100hncm.html

Reference: http://lzjyjh.blog.sohu.com/137646727.html

-----------------------------------------------

In Unix OS, the system calls exit to terminate a process. Calls that can be displayed by the ProcessExit System Call to terminate 1Processes, you can alsoReturn at the end of the program.(Startup
Routine calls exit when a C program returns from the main function ).

The method for calling exit is as follows:

       Exit (Status );

Status is returnedBack to parent processOfTermination code. When a process exitsThe process is in zombie
State
.

The following events occur when the exit system calls the execution:

1.Clear current processAll signal processing functions.

2. If the current process is associated with the terminal,Process Group LeaderTo process in each group.Send hang-up
Signal
And set the process group of these Members to 0.

3. PassKernel internal AlgorithmDisable all open file descriptors of the current processAnd releaseCurrent DirectoryInode associated; If current exists
(Charged) Root, also released through the algorithm Iput.

4. ProcessReleaseAllRegion and associated memory.

5.Time when the sub-process of the computing process machine is executed(User
Mode and kernel mode), and write the record to a global accounting file.

6. SetThe Process status changes to zombie., AndSet all of your sub-ProcessesParent process ID is set to 1 (init);IfThe status of the child is zombie.,InitProcess sends sigchld SignalTo clear the process of the sub-process
Table slot
.

7. the exiting process sends a sigchld signal to its parent process.

8. Perform Context
Switch,Schedule other non-zombie Processes(This process is already zombie ).

 

Before discussing exit and _ exit, we should first discuss the issue of File Memory Cache areas.

  In Linux,Standard input/output (I/O) functions areProcess as a file. Corresponds toEach fileIn the memory.Corresponding CacheEach time you read a file, some records will be read to the cache, so that the next time you read the file, it will be read in the cache; similarly,When writing a file, it is also written in the corresponding cache of the file, not directly written into the file on the hard disk..Line Break \ n or end mark EOF. The advantage of doing so is to speed up reading and writing files! But this also brings about some problems: for example, some data, we think the file has been written, but in factThe cache that does not meet certain conditions and does not reside in the memory.In this way, if weDirectly use the _ exit () function to terminate the process.Data Loss!!

  The reader may have noticed that _ exit is used instead of exit. If it is changed to exit, there will be no data loss problem. This is the difference between them! To explain this problem, we need to involve their work steps.

  Exit (): When executing this function, the process checks the file opening status,Clear the I/O cache. If there is data in the cache, it will write them into the corresponding file.This prevents the loss of file data! Then terminate the process.

  _ Exit (): When executing this function, it does not clear the standard input/output cache, but directly clears the memory space. Of course, it destroys the data that has not been written to the file cache. It can be seen that using the exit () function is more secure.

  In addition, the differences between the two are as follows:Exit () -- stdlib. h _ Exit () -- unistd. h

  Finally, the general situation is as follows:Exit (0) indicates normal exit, exit (1), exit (-1) indicates abnormal exit, 0, 1,-1 is the return value, The specific meaning can be customized.

   Exit seems to be in stdio. H, so it must contain header files. 
 Return indicates a function call. If the main function is returned, the system exits the program. 
 Exit is to forcibly exit the program at the call. Once the program is run, it will end. 
 ------------------------------------------------------------------- 
 Return 
   
 Yes 
   
 Function return 
 While exit indicates exit.
  
 ------------------------------------------------------------------- 
 Exit (1) indicates an exception exits. This 1 isReturnReturned to the Operating SystemHowever, DOS does not seem to require this return value.  
 Exit (0) indicates normal exit 
 ------------------------------------------------------------------- 
 No matter where it is written, it is a program launched, DOS and Windows are nothing different, at most the system processing is different. 
  Numbers 0, 1,-1 will be writtenEnvironment Variable errorlevelAnd other programs can determine the end state of the program. 
 Generally, 0 is normal, and other numbers are abnormal. You can specify the corresponding error. 
 ------------------------------------------------------------------- 
 If it is returned to the operating system, 0 indicates normal exit, and other values indicate abnormal exit. Before exiting, You can provide some prompt information or check the cause of the error in the debugging program.

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.