Article Directory
main
function
From C program always main
begins execution functions. main
Function prototype is:
int main(int argc, char *argv[]);
argc
Is the number of command line arguments,argv
is an array of pointers each pointing parameters constituted.When the kernel execution C program, call
main
before the function to call a special startup routine. This executable file specified as the address of the startup routine procedure - this is the connection editor settings, connected by the C editor editor called. The startup routine to obtain command-line arguments and environment variables value from the kernel, then the manner described above to callmain
a function to make arrangements.
The process is terminated
There are 8 ways to make the process of termination, including five kinds of normal termination, they are:
- From
main
return - transfer
exit
- Call
_exit
or_Exit
- The last thread returns from its startup routine
- The last call from a thread
pthread_exit
Aborted three ways, they are:
- Call
abort
function - Receiving a signal
- The last thread to respond to the cancellation request
1. Quit function
3 functions for normal termination. _exit
And _Exit
functions immediately into the kernel exit
function to perform some cleanup process, then return to the kernel.
#include <stdlib.h>
void exit(int status);
void _Exit(int status);
#include <unistd.h>
void _exit(int status);
main
Function call returns an integer with the value exit
equivalent. Thus in the main
function exit(0);
equivalent toreturn (0);
Test Example:
#include <stdio.h>
main()
{
printf("hello, world\n");
}
2. Functionsatexit
In accordance with ISO C, a process can be registered to 32 functions, these functions will exit
automatically calls. These functions are called termination processing program, and calls the atexit
function to register these functions.
#include <stdlib.h>
int atexit(void (*func)(void));
返回值:若成功,返回 0;若出错,返回非 0。
atexit
Parameter of the function is a function address, when you call this function without having to pass any parameters to it, do not expect it to return a value.exit
The order of the function and call these functions when they are checked in the order opposite. If the same function is registered several times, it will be called multiple times.
Test Example:
#include "../../include/apue.h"
static void my_exit1(void);
static void my_exit2(void);
int main(void)
{
if(atexit(my_exit2) != 0)
err_sys("can't register my_exit2");
if(atexit(my_exit1) != 0)
err_sys("can't register my_exit1");
if(atexit(my_exit1) != 0)
err_sys("can't register my_exit1");
printf("main is done\n");
return 0;
}
static void my_exit1(void)
{
printf("first exit handler\n");
}
static void my_exit2(void)
{
printf("second exit handler\n");
}
The results are as follows:
From the above chart, termination function execution order is opposite to the order they are registered, and my_exit1
the function is registered twice, it is executed twice.
Command line parameters
When executing a program, calling exec
the process command line arguments can be passed to the new program. This is part of normal operation of the UNIX shell.
Test Example:
All command line arguments echoed to the standard output, however, the usual echo
program can not echo the argument number 0.
#include "../../include/apue.h"
int main(int argc, char *argv[])
{
int i;
//for(i = 0; i < argc; i++)
for(i = 0; argv[i] != NULL; i++)
printf("argv[%d]: %s\n", i, argv[i]);
return 0;
}
The results are as follows:
Environmental Table
Each program receives an environment table, the same table with the parameters, environment table is a character array of pointers, each of which contains a pointer to a null ( '\ 0') the address of the end of the C strings. Global variable environ
contains the address of the array of pointers:
extern char **environ;
We call environ
for the environment pointer, array of pointers to environment table, where each string is a pointer to the environment strings. Historically, most UNIX systems support the main function takes three parameters, the third parameter is the environment table address:
int main(int argc, char *argv[], char *envp[]);
Because the provisions of ISO C main
function only two parameters, and the first three parameters and global variables environ
compared to not bring more benefits, it also provides POSIX.1 should be used environ
instead of the first three parameters. Commonly used getenv
and putenv
functions to access environment variables, rather than environ
variables. However, if you want to see the entire environment, you must use environ
a pointer.