Advanced Programming for UNIX environments chapter seventh

Source: Internet
Author: User

The main points of knowledge in this chapter include: Program start and stop, process termination exit and _axit,atexit function. Storage space layout, deep nested error out function, resource query and change function and so on.

1. First pass the command line arguments and environment variable values from the kernel to the C boot routine, and then the C boot routine calls the main function, and main calls the other functions. Upon termination,

You can use the _exit () function or the _exit () function to directly invoke the kernel end. You can also call the exit () function to clean up the process first. (call at each termination

The standard I/O cleanup program) before entering the kernel.

The 2.exit function calls each termination handler before invoking the standard I/O stream cleanup shutdown work: Call the Fclose function for all open streams. This causes all buffered output data to be flushed.

3. Undefined termination Status: (1) Call the Exit class function without terminating state (2) main function return no return value (3) The main function does not declare the return type as Integer.

4. "Atexit function"

Registers the collection of termination handlers that are automatically called by exit. The order of the Atexit registration program is reversed from the order in which exit executes the call. void atexit (void)

Storage space layout for 5.c languages

Body segment: The part of the machine instruction that the CPU executes. Can be shared non-modifiable read-only;

Initialize data segment: A variable that needs to be explicitly assigned an initial value in the program.

Non-initialized data segment: BBS segment, the kernel initializes the data in this segment to 0 or null pointers.

Stacks: Automatic variables and the information you need to save each time the function is called are stored in this section.

Heap: Dynamic storage allocation.

6. Shared library: When the program first executes or when a library function is called for the first time, a dynamic link method is used to link the program to the shared library function.

7. Memory allocation function

(1) malloc (2) calloc. Allocate several storage spaces, each of which has a fixed size.

(3) void *realloc (void *ptr, size_t newsize);//newsize is the total length of the new and the old

Change the length of the previous allocation area. If there is enough new space allocated to the high level, the new space is added directly to the current position. If not,

Place the current content in a different location with the new assignment and delete the original location. So should not assign pointers to him

8.SETJMP and longjmp

This is a set of functions that appear in conjunction with each other.

The setjmp function determines the position in the main function returned after an error, LONGJMP is returned from the longjmp to the setjmp in the main function if an error occurs. The two associations determine that the same set of flags is the SETJMP parameter and the first parameter of the longjmp, need to be the same value, the second parameter of longjmp is used to distinguish between its setjmp and the corresponding longjmp in the different error location, so it needs to be set to different integer values to distinguish.

If you need to return setjmp to save the original value, you need to add the volatile attribute before the variable, and the optimization execution can implement its value without rollback.

(1) Volatile: As a keyword, ensure that this instruction does not need to be optimized by the compiler and ignored, but each direct read value. After optimization, volatile is taken from the register instead of the memory.

9.getrlimit and Setrlimit functions: Get and set resource limits.

Advanced Programming for UNIX environments chapter seventh

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.