The Linux process terminates

Source: Internet
Author: User
Tags exit in terminates

Process exit in Linux is divided into normal exit and abnormal exit

1. Normal exit

(1) Execute return in the main () function

(2) Call the exit () function

(3) Call the _exit () function

2. Abnormal exit

(1) Call the About () function

(2) a process receives a signal that causes the process to terminate

Contrast

(1) Exit and return

Exit: a function with parameters. Leave control to the system after exit executes

Return: Returns after the function has finished executing. Return control to the calling function after execution

(2) Exit and about

Exit: Normal termination process

About: Abnormal termination process

(3) Exit and _exit

same : Both are used to terminate the process. When the program executes to exit or _exit, the system unconditionally stops all remaining operations, clearly includes the various data structures including the PCB, and terminates the operation of this process.

The parameter Exit_code in exit is 0 for normal termination of the process, and if other values indicate an error occurred during program execution

Similarities and differences : The header file of the exit () function-->stdlib.h

The header file of the _exit () function-->unistd.h

_exit () returns to the kernel immediately after execution, and exit () performs some cleanup work before handing over control to the kernel

When the _exit () function is called, it closes all file descriptors of the process, cleans up memory and some other kernel cleanup functions, but does not flush the stream (STDIN,STDOUT,STDERR, etc.)

The exit () function is a wrapper over the _exit () function, which invokes the _exit () function and flushes the stream before the call

eg

Exit.c

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/84/67/wKiom1ePkYOTe72yAAAhfTRBSXg148.png "title=" Image.png "alt=" Wkiom1epkyote72yaaahftrbsxg148.png "/>

Makefile

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/84/67/wKioL1ePkaSD0VKEAAAUu7gsbMs552.png "title=" Image.png "alt=" Wkiol1epkasd0vkeaaauu7gsbms552.png "/>

Execution Result:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/84/67/wKioL1ePkcDy120yAAAuP0h0Gg4867.png "title=" Image.png "alt=" Wkiol1epkcdy120yaaaup0h0gg4867.png "/>

_exit.c

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/84/67/wKiom1ePkeKxD5CVAAAhTqWCUaM061.png "title=" Image.png "alt=" Wkiom1epkekxd5cvaaahtqwcuam061.png "/>

Makefile

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/84/67/wKioL1ePkf_gKmyjAAAVcGozFIw017.png "title=" Image.png "alt=" Wkiol1epkf_gkmyjaaavcgozfiw017.png "/>

Execution results

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/84/67/wKiom1ePkhuRaIjHAAApBG9Njv0094.png "title=" Image.png "alt=" Wkiom1epkhuraijhaaapbg9njv0094.png "/>

The printf () function is a way to use buffered I/O, which automatically reads a record from a buffer when it encounters a "\ n" line break. Therefore, exit () exits when the buffer's data is finished, and the _exit () function exits directly

If you change the _exit.c in buffer to "this was the content in buffer \ n",

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/84/67/wKioL1ePkjThBaXaAAAhm3VqAM4245.png "title=" Image.png "alt=" Wkiol1epkjthbaxaaaahm3vqam4245.png "/>

The result is

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/84/67/wKioL1ePklvziqT1AAAWRP0sf2M139.png "title=" Image.png "alt=" Wkiol1epklvziqt1aaawrp0sf2m139.png "/>

If you change the "using _exit\n" to "Using _exit",

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/84/67/wKiom1ePknWR9EV0AAAghUYL49I701.png "title=" Image.png "alt=" Wkiom1epknwr9ev0aaaghuyl49i701.png "/>

The result is

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/84/67/wKiom1ePkoyRAeMSAAAXtjrAYTY296.png "title=" Image.png "alt=" Wkiom1epkoyraemsaaaxtjrayty296.png "/>

This article is from the "Voice of the Heart" blog, be sure to keep this source http://ljy789.blog.51cto.com/10697684/1828226

The Linux process terminates

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.