5th. Process Environment (2) _ Process start and end

Source: Internet
Author: User
Tags strcmp terminates

2. process initiation and termination

2.1 C Program startup Process

(1) Start-up routines

① is a program code that is placed in the/lib/libc.so.***. at compile time, the compiler compiles the code for the startup routine into the executable file .

The ② executable program refers to the embedded startup example code as the starting address of the program .

③ when the kernel executes a C program (using the EXEC function), execute the boot routine code before calling main .

(2) The function of the start-up routine

 ① parameters of the collection command line are passed to the ARGC and argv in the main function

② Collecting environment information build environment table and pass it to main function

③ termination function of the registration process

2.2 Process Termination

Process termination

Main way

normal termination

① returns from the main function; ② call exit (Standard C library function);

③ call _exit or _exit (System call);

④ the last thread returned from its startup routine.

⑤ the last thread called Pthread_exit

abnormal termination

① call Abort;② to receive a signal and terminate;

③ the last thread responds to a cancellation request

Process returns

① normally the program runs successfully back to 0, otherwise it returns non-0. ② can see the process return value through echo $ in the shell.

2.3 related Functions

(1) Exit function

#include <stdlib.h>

functions

void exit (int status);  

void _exit (int status);

void _exit (int status);//#include <unistd.h>

functions

Terminate process

remarks

(1) _exit and _exit immediately enter the kernel .

(2) exit performs some cleanup (including calls to execute each termination handler, closing all standard i/ o Stream, etc.) before entering the kernel .

(2) atexit function

Header file

#include <stdlib.h>

Function

int atexit (void (*function) (void));

return value

0 if successful, 1 if there is an error

Function

Registering a termination handler with the kernel (exit handler)

Note

(1) Each initiated process is registered by default with a standard termination function.

(2) The terminating function releases some of the resources that the process consumes when the process terminates.

(3) The sequence of execution of multiple terminating functions is executed in the form of a stack , that is, after the registration of the first execution.

"Programming Experiment" terminating processing function

Process_term.c

#include <unistd.h>#include<stdio.h>#include<stdlib.h>#include<string.h>//defining the terminating function of a processvoidTERM_FUN1 (void) {printf ("First term function\n");}voidTerm_fun2 (void) {printf ("second term funciton\n");}voidTERM_FUN3 (void) {printf ("third term function\n");}intMainintargcChar*argv[]) {    if(ARGC <3) {fprintf (stderr,"Usage:%s file [exit|_exit|return]\n"); Exit (1); }        //Call the Atexit function to register the terminating function with the kernel//Note: The order of the registration termination functions is FUNC1->FUN2->FUN3, and the function is called in order fun3->fun2->fun1atexit (TERM_FUN1);    Atexit (TERM_FUN2);    Atexit (TERM_FUN3); FILE* fp = fopen (argv[1],"W"); fprintf (FP,"Hello world!\n");//fprintf with cache, calling exit or return will be//The stream will be closed and will be written to the specified file.     if(!STRCMP (argv[2],"Exit") ) {exit (0);//Library functions for standard C}Else if(!STRCMP (argv[2],"_exit") {_exit (0);//system Calls}Else if(!STRCMP (argv[2],"return")){        return 0; }Else{fprintf (stderr,"Usage:%s file [exit|_exit|return]\n", argv[0]); }    return 0;}/*output: [[email protected] 5.process]# bin/process_term term.txt return//Normal exit (call termination function and Refresh cache) third term Functionsecond Term Funcitonfirst term function[[email protected] 5.process]# cat Term.txthello world! [[email protected] 5.process]# bin/process_term term.txt exit//Normal exit (call termination function and Refresh cache) Third term Functionsecond term Funciton First term function[[email protected] 5.process]# cat Term.txthello world! [[email protected] 5.process]# bin/process_term term.txt _exit//Abnormal exit (do not call termination function and refresh cache) [[email protected] 5.process]# cat Term.txt[[email protected] 5.process]# bin/process_term term.txt quitusage:bin/process_term file [Exit|_exit|return] Third term Functionsecond term Funcitonfirst term function[[email protected] 5.process]# cat Term.txthello world! [Email protected] 5.process]#*/

2.4 C program start-up and termination

(1) Termination of the process

(2) The difference between the termination method

Return

Exit ()

_exit/_exit

Whether to refresh the standard I/O cache

Is

Is

Whether

Whether to call the terminating function automatically

Is

Is

Whether

(3) Description

Exit first calls each termination handler and then calls Fclose as many times as needed to close all open streams . The call to _exit or _exit will go directly into the kernel and will not invoke the terminate handler and close the opened stream.

The ②POSIX standard specifies that if a program calls any of the functions in the EXEC function family, all the terminating handlers that have been installed will be cleared.

The only way the ③ kernel makes the program execute is to call an exec function. The only party that the process voluntarily terminates failed to invoke _exit or _exit explicitly or implicitly (by calling Exit). The procedure can also be involuntarily terminated by a signal.

5th. Process Environment (2) _ Process start and 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.