Process environment of Reading Notes in UNIX Advanced Programming

Source: Internet
Author: User

1. Process Termination

There are eight ways to terminate the process

(1) return from main

(2) Call exit;

(3) Call _ exit or _ exit;

(4) The last thread returns from its startup history

(5) The last thread calls pthread_exit;

There are three methods to terminate an exception:

(6) call abort ();

(7) receive a signal;

(8) The last thread makes a response to the cancellation request.


Three functions are used to terminate a program normally:

# Include <stdlib. h>

Void exit (INT status );

Void _ exit (INT status );

# Include <unistd. h>

Void _ exit (INT status );

The _ exit and _ exit functions immediately enter the kernel. Exit: execute some cleanup operations before entering the kernel.


Function atexit

# Include <stdlib. h>

Int atexit (void (* func) (void ));

Iso c requires that a process can register up to 32 functions through atexit. These functions are automatically executed when the process ends.


2. Environment table

Each process has an environment table, and the global variable environ contains a pointer to the Environment table.


Iso c defines a function getenv to take the value of the environment variable.

# Include <stdlib. h>

Char * getenv (const CAHR * Name );

In addition to obtaining environment variables, you also need to set environment variables.

# Include <stdlib. h>

Int putenv (char * Str); // string in the form of name = value, put it in the Environment table. If the name already exists, delete its definition first.

Int setenv (const char * Name, const char * value, int rewrite); // set name to value. If the name already exists in the environment, (a) If rewrite is not 0, Delete the existing definition first; (B) If rewrite is 0, do not Delete the existing definition.

Int unsetenv (const char * Name); // unsetenv deletes the name definition. No error even if it does not exist.


3. Storage space layout in C Language

The C program is always composed of the following parts:

(1) text section. That is, the machine Command executed by the CPU.

(2) initialize the data segment. It is usually called a data segment, which contains variables that need to be explicitly assigned values in the program.

For example, a statement other than any function in C:

Long max = 0;

(3) The data segment is not initialized. Before the program starts, the kernel initializes the data in this segment to 0 or a null pointer.

For example, declarations outside the function:

Long sum [1000];

(4) stack. The automatic variables and the information to be saved for each function call are stored in the stack. Each time a function is called, its return address and caller's environmental information (such as the values of some registers in the machine) are stored in the stack. The called function allocates storage space for automatic and temporary variables on the stack.

(5) heap. It is usually used for dynamic storage allocation in the heap.

Shows a typical storage space arrangement method:



4. Shared Library

The shared library (which can be understood as a dynamic link) eliminates the need to include common library functions in the executable file. When a program executes or calls a function library for the first time, it uses the Dynamic Link Method to link the program to the shared library function. This reduces the length of the executable file, but increases the time overhead.

Differences between dynamic and static links:

Static Connection Library is to directly link the function code used in the (LIB) file to the target program. Other library files are no longer needed when the program is running; dynamic Link is to link the file module (DLL) where the called function is located and the location of the called function in the file to the target program. When the program is running, find the corresponding function code from the DLL, therefore, support for the corresponding DLL files is required.

Static and dynamic libraries share code, all commands in lib are directly included in the final generated EXE file. However, if a DLL is used, the DLL does not need to be included in the final EXE file. During execution of the EXE file, the DLL file can be dynamically referenced and detached. Another difference between a static link library and a dynamic link library is that the static Link Library cannot contain any other dynamic or static Link Library, other dynamic or static link libraries can be included in the dynamic link library.


5. setjmp and longjmp Functions

In C, the Goto () statement cannot span functions. To jump over a function, use the setjum () and longjmp () functions.

# Include <setjmp. h>

Int setjmp (jum_buf env );

Void longjum (jmp_buf ENV, int Val );

Call setjmp at the location you want to return. Its Parameter env is of the jmp_buf type, which can restore all stack status information when longjmp is called. Call longjmp () when you want to return to the setjmp position.

Process environment of Reading Notes in UNIX Advanced Programming

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.