Process environment and Process Control (1): Process initiation and Termination

Source: Internet
Author: User
Tags exit in

1. process start:
The C program is executed from the main function. The prototype is as follows:
Int main (INT argc, char * argv []);
Generally, the return value of main is int type, and 0 is returned correctly.
If the return value of main is void or none, Some compilers will give a warning. In this case, the return value of main is usually 0.

The main command line parameters are not explained too much. The following program shows:

# Include <stdio. h>

Int main (INT argc, char * argv [])
{
Int I;
For (I = 0; I <argc; I ++)
Printf ("argv [% d]: % s/n", I, argv [I]);
Return 0;
}

 

 

2. Process Termination:
There are two types of termination of C program: normal termination and exceptional termination.
Normal termination is divided into: return, exit, _ exit, _ exit, pthreade_exit
Exception: abort, signal, thread response canceled
It mainly refers to the first four types of normally terminated functions, namely exit functions.
# Include <stdlib. h>/* Iso c */
Void exit (INT status );
Void _ exit (INT status );
# Include <unistd. h>/* POSIX */
Void _ exit (INT status );
The differences between the above three functions are:
Exit () (or return 0) will call the standard I/O cleanup programs (such as fclose) that terminate the processing program and the user space ), _ exit and _ exit are cleared directly by the kernel instead of being called.

Management.
Therefore, exit (0) in the main function is equivalent to return 0.

3. atexit:
Iso c requires that a process can register up to 32 Termination handler functions, which are automatically called by exit in the reverse order of registration. If the same function is registered multiple times

Multiple calls.
The prototype is as follows:
# Include <stdlib. h>
Int atexit (void (* func) (void ));
The parameter is a function pointer pointing to the end processing function. This function has no parameters and no return value.

The following procedure is used as an example:

# Include <stdlib. h>

Static void myexit1 ()
{
Printf ("first exit handler/N ");
}

Static void myexit2 ()
{
Printf ("second exit handler/N ");
}

Int main ()
{
If (atexit (my_exit2 )! = 0)
Printf ("can't register my_exit2/N ");
If (atexit (my_exit1 )! = 0)
Printf ("can't register my_exit1/N ");
If (atexit (my_exit1 )! = 0)
Printf ("can't register my_exit1/N ");

Printf ("Main is done/N ");
Return 0;
}

 

Running result:
$./A. Out
Main is done
First exit Handler
First exit Handler
Second exit handler running result:
$./A. Out arg1 arg2 arg3
Argv [0]:./A. Out
Argv [1]: arg1
Argv [2]: arg2
Argv [3]: arg3

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.