The main function of the process environment, process terminates, command-line arguments and environment table

mainfunction

From C program always mainbegins execution functions. mainFunction prototype is:

int main(int argc, char *argv[]);

argcIs the number of command line arguments, argvis an array of pointers each pointing parameters constituted.

When the kernel execution C program, call mainbefore 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 call maina 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 mainreturn
  • transferexit
  • Call _exitor_Exit
  • The last thread returns from its startup routine
  • The last call from a threadpthread_exit

Aborted three ways, they are:

  • Call abortfunction
  • Receiving a signal
  • The last thread to respond to the cancellation request

1. Quit function

3 functions for normal termination. _exitAnd _Exitfunctions immediately into the kernel exitfunction 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);

mainFunction call returns an integer with the value exitequivalent. Thus in the mainfunction 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 exitautomatically calls. These functions are called termination processing program, and calls the atexitfunction to register these functions.

#include <stdlib.h>
int atexit(void (*func)(void));
返回值:若成功,返回 0;若出错,返回非 0

atexitParameter 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. exitThe 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_exit1the function is registered twice, it is executed twice.

Command line parameters

When executing a program, calling execthe 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 echoprogram 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 environcontains the address of the array of pointers:

extern char **environ;

We call environfor 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 mainfunction only two parameters, and the first three parameters and global variables environcompared to not bring more benefits, it also provides POSIX.1 should be used environinstead of the first three parameters. Commonly used getenvand putenvfunctions to access environment variables, rather than environvariables. However, if you want to see the entire environment, you must use environa pointer.

发布了229 篇原创文章 · 获赞 17 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qq_40073459/article/details/104419018