Linux Process 4

Source: Internet
Author: User
Tags terminates

Exit () and _ exit () Functions

A process has its own life like a person. We use the fork () function to create a process. How can we stop the process.

Process exited

1. Any process in Linux that exits

Process exit indicates that the process is about to end. In Linux, process exit is divided into normal exit and abnormal exit.

1> exit normally

A. Execute return in the main () function.

B. Call the exit () function.

C. Call the _ exit () function.

2> exit unexpectedly

A. Call the about function.

B. The process receives a signal that terminates the program.

Tiger-John note:
Regardless of the exit method, the system will eventually execute the same code in the kernel. This code is used to close the opened file descriptor used by the process and release the memory and other resources it occupies.

 

 

3> compare the differences between the preceding exit Methods

(1) differences between exit and return:

A. Exit is a function with parameters. After exit is executed, the control is handed over to the system.

B. Return is the result after the function is executed. After urn is executed, the control is handed over to the calling function.

(2) differences between exit and abort:

A. Exit indicates that the process is terminated normally.

B. About indicates the termination of an exception.


Now we focus on the exit () and _ exit () functions.

 

2. Learning of exit () and _ exit ()

1> the exit and _ exit functions are used to terminate the process.

When the program executes exit or _ exit, the system unconditionally stops all remaining operations, clears various data structures including PCB, and terminates the operation of the process.

2> exit is declared in the header file stdlib. h, while the _ exit () Statement is declared in the header file unistd. h. If the value of exit_code in exit is 0, the process is terminated normally. If it is another value, an error occurs during program execution.

3> difference between exit () and _ exit:

A. _ exit () is returned to the kernel immediately after execution, while exit () must first perform some cleanup operations and then hand over control to the kernel.

B. when the _ exit function is called, it will close all file descriptors of the Process, clean up the memory and some other kernel cleanup functions, but will not refresh the stream (stdin, stdout, stderr ...). the exit function is an encapsulation of the _ exit function. It calls _ exit and refreshes the stream before calling it.

Tiger-John note:

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 File Buffer content back to the file. In Linux, there is an operation called "buffer I/O" in the standard function library, which features a buffer corresponding to each opened file and has a buffer in the memory. Each time a file is read, several records are read continuously, so that the next time the file is read, it can be directly read from the memory buffer. Similarly, each time a file is written, it is only the buffer in the memory, and meets certain conditions (such as reaching a certain number or encountering specific characters). Then, the content in the buffer is written to the file at one time. This technology greatly increases the speed of reading and writing files, but it also brings a little trouble to programming. For example, some data is considered to have been written into the file. In fact, because the specific conditions are not met, they are only stored in the buffer. In this case, the _ exit () function is used to directly close the process, the data in the buffer zone is lost. Therefore, to ensure data integrity, you must use the exit () function.

 

 

C. Use a function instance to see the differences between them:

Function Example 1: exit. c

1 # include <stdio. h>
2 # include <stdlib. h>
3
4 int main ()
5 {
6 printf ("using exit ----/N ");
7 printf ("this is the content in buffer/N ");
8 exit (0 );
9}

After the function is debugged

Think @ Ubuntu :~ /Work/process_thread/exit $ GCC exit. C-o exit
Think @ Ubuntu :~ /Work/process_thread/exit $./exit

The execution result is:

Using exit ----
This is the content in buffer

Function Example 2: _ exit. c

1 # include <stdio. h>
2 # include <unistd. h>
3
4 int main (void)
5 {
6 printf ("using _ exit --/N ");
7 printf ("this is the content in buffer ");
8 _ exit (0 );
9}
After the function is debugged

Think @ Ubuntu :~ /Work/process_thread/exit $ GCC _ exit. C-o _ exit
Think @ Ubuntu :~ /Work/process_thread/exit $./_ exit

The execution result is:

Using _ exit --

 

Tiger-John note:

1. the printf function uses the buffer I/O mode. This function automatically reads records from the buffer zone when a "/N" line break occurs. Therefore, exit () Exits after writing the data in the buffer zone, while the _ exit () function exits directly.

2. you can also change printf ("this is the content in buffer") in function instance 2 to printf ("this is the content in buffer/N ") (that is, add/n at the end of printf to view the running result. Why is this result ?)

 

 

Tiger-John added:

The termination sequence of parent and child processes varies with the result.

1> the parent process is terminated before the child process:

This is the orphan process we used earlier. When the parent process exits, the system connects the INIT process to the pipe process.

2> the child process is terminated before the parent process, and the parent process does not call the wait function.

In this case, the sub-process enters the zombie state and will continue until the system restarts. When the sub-process is in the dead state, the kernel only saves the necessary information of the process for the parent process. At this time, sub-processes always occupy resources and reduce the maximum number of processes that can be created by the system.

 

What is a dead state?

 

A process that has been terminated but has not been well processed by its parent process (obtain information about the final child process and release the resources it still occupies) is called a dead process (zombie ). The ps command prints the status of the zombie process to Z.

3> the child process is terminated before the parent process, and the parent process calls the wait function.

The parent process waits for the child process to end.

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.