Linux Environment Programming process (i): main function call, process termination, command line arguments and environment table

Source: Internet
Author: User

(i) Main function call

The main function acts as the entry function when the program is run, how is it called? The first thing to be clear is that the main function is also a function that can only be executed if it is called. In fact, when executing an executable program, before calling the main function, the kernel invokes a special startup routine that is used as the starting address for the executable program. How does the startup routine act as the starting address for an executable program? This is set by the link compiler, and the link compiler is called by the C compiler (such as the GCC compiler). What does the startup routine do primarily as the starting address of an executable program? The startup routine takes command-line arguments and environment variable values from the kernel to prepare for the invocation of the main function.

(ii) command-line arguments

When executing a program, the process calling exec can pass command-line arguments to the program. Examples are as follows:

/* *file name:argdemo.c *author    : libing *mail      : [email protected] *function  : Show the arguments you input */#include <stdio.h> #include <stdlib.h>intmain (int argc, char *argv[]) {int i;for (i = 0; i < argc; i++) {Prin TF ("argv[i] =%s.\n", Argv[i]);} Exit (0);}
Compile test results:./a.out FDJ FDA li

Argv[i] =./a.out.argv[i] = fdj.argv[i] = fda.argv[i] = li.
(iii) Environment table
Each program receives an environment table. As with the command-line arguments above, the environment table is also an array of character pointers, where each pointer contains an address to a null-terminated C string. The global variable environ contains the address of the array of pointers.

As you can see from the table, there is a null character at the end of each string. The environ is called the environment pointer, and the pointer array is the environment table, where each pointer points to a string that is an environment string. You typically use the getenv and PUTENV functions to access specific environment variables instead of environ variables. However, if you want to view the entire environment, you must use the Environ pointer.

#include <stdlib.h>
Char *getenv (const char *name); Return value: A pointer to the value associated with name, or null if not found

int putenv (char *string); Return value: Returns 0 if successful, and returns a value other than 0 if an error occurs

int setenv (const char *name, const char *value, int overwrite);
int unsetenv (const char *name); //Setenv and unsetenv, returns 0 if successful, or 1 if an error occurs

The description is as follows:

1. Putenv takes the form of a string "Name=value" (do not leave spaces on either side of the equal sign) and places it in the environment table. If name already exists, the original definition is deleted first.

2. Set name to value setenv. If the name already exists in the environment, then a: if Rewite is not 0, the existing definition is first deleted, and B: If rewrite is 0, its existing definition is not deleted (name is not set to the new value, and there is no error).

3. unsetenv Delete the definition of name. There is no error even if there is no such definition.

The sample code is as follows:

/* *file name:envdemo.c *author    : libing *mail      : [email protected] *function  : Test some Function of Env */# Include "Apue.h" #include <stdlib.h>intmain (int argc, char *argv[]) {char *value;int i;value = getenv ("PWD");p rintf ("%s\n", value), if (!getenv ("libing")) {printf ("can ' t find env libing.\n");} Putenv ("[email protected]"), value = getenv ("libing");p rintf ("%s\n", value); Unsetenv ("libing"); if (getenv ("libing") = = NULL) printf ("Can ' t find env libing.\n"); exit (0);}
Program test Result: GCC envdemo.c

/work/tmp/apue/chapter7can ' t find env libing. [Email protected]can ' t find env libing.
(iv) Termination of the process

Since a program starts again, there must be a termination. There are 8 ways to terminate a process, 5 of which are normal termination: returning from Main, calling Exit, calling _exit or _exit, the last thread returning from its startup routine, and the last thread invoking the Pthread_exit;3 type of exception termination: Call Abort, Receives a signal and terminates, and the last thread responds to the cancellation request.

1, Exit class function. There are three functions for terminating a program normally: _exit and _exit immediately into the kernel, exit performs some cleanup (including calls to execute various termination handlers, shuts down all standard I/O streams, etc.) and then enters the kernel.

#include <stdlib.h>

void exit (int status);

void _exit (int status);

#include <unistd.h>

void _exit (int status);

2, atexit function. A process can enlist up to 32 functions, which are automatically called by exit. We call these functions termination handlers and call the Atexit function to register the functions.

#include <stdlib.h>

int atexit (void (*func) (void)); Returns 0 if successful, or non-0 if an error occurs

The atexit parameter is a function address that does not require any arguments to be transmitted when calling this function, nor does it expect it to return a value. The order in which exit calls these functions is the opposite of the order in which they are enlisted. If the same function is registered multiple times, it is also returned multiple times.


Example code:

/* *file name:atexit.c *author    : libing *mail      : [email protected] *function  : Test the function atexit */#in Clude <stdio.h> #include <stdlib.h> #include <unistd.h>static void my_exit1 (void), static void My_ Exit2 (void); int main () {if ((Atexit (my_exit2))! = 0) printf ("Can ' t register my_exit2"); if ((Atexit (my_exit1))! = 0) printf ("Can ' t register my_exit1"); if ((Atexit (my_exit1)) = 0) printf ("Can ' t register my_exit1");p rintf ("Main is done.\n"); return 0;}

Compile test results:

Main is Done.first exit Handlerfirst exit Handlersecond Exit Handler




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.