LINUX executable structure and process structure

Source: Internet
Author: User

one, the Linux executable file structure

Under Linux, the program is an ordinary executable file, the following is the basic situation of a binary executable file:



As you can see, this executable is stored (without being transferred into the content) into the code area (text), the data area, and the Uninitialized data area (BSS) 3 parts. The basic contents of each paragraph are described below:


Code Area :

The machine instruction that holds the CPU execution. Usually the code area is shareable (that is, another executor can call it), so that it can be shared for programs that are frequently executed, with only one copy of the code in memory . The code area is usually read-only, so that it is read-only because it prevents the program from accidentally modifying its instructions. In addition, the code area also plans information about the local variables.


The code area directives include opcode and operands (or object address references). If it is an immediate number (that is, a specific numeric value), it will be included directly in the code, if it is local data, will be allocated at runtime in the stack space, and then reference the address of the data, if it is uninitialized data area and data area, in the code will also refer to the address of the data.


Global Initialization data area/static data area (data segment):

The zone contains global variables that are explicitly initialized in the program, static variables that have been initialized (including global static variables and local static variables), and constant data (such as String constants).

For example, one is not declared within any function (global variable), as follows:

[CPP]View Plaincopy
    1. int  count = 100;


Causes the variable count to be stored in the initialization data area according to its initial value.


Define static variables in any location as follows:

[CPP]View Plaincopy
  1. static   int  num = 200;  

This declares a static data and initializes it, if declared outside of any function body, it is a static global variable, and if it is in the body of the function (local), it is represented as a local static variable. In addition, if you add static to a function name, it means that it can only be called in the current file.


Uninitialized data area (also called BSS area):

The global uninitialized variable and the uninitialized static variable are deposited. Data in the uninitialized data area is initialized to 0 or empty (null) by the kernel before the program begins execution.

For example, an uninitialized variable that is not declared within any function.

[CPP]View Plaincopy
    1. Long  sum[1000];

Save sum to uninitialized data


II. structure of the Linux process

Under a Linux system, if an executable is loaded into memory, it will become one or more processes (multiple processes because the process can then create a new process at run time, but only one process at load time). process is the basic unit of LINUX transaction management, all processes have their own independent processing environment and system resources . The environment of a process is determined and composed of the current system state and its parent process information.


A contrast chart for the executable file storage structure and the basic structure (partial) of the Linux process.


A process is a running program segment, a process that mainly includes the space requested in memory, code (loaded program, including code snippet, data segment, BSS), heap, stack, and kernel process information structure provided by core

Task_struct (position in/usr/include/linux/sched.h), open file, context (refers to a static description of the process execution activity), and a pending signal.


(1) code area (text Segment) . The executable code snippet is loaded, and the location loaded into memory is completed by the loader.
(2) Global initialize data area/static data area ("Database Segment") . The load is the executable data segment, stored in the data segment (global initialization, static initialization data) of the data life cycle for the entire program to run the process.
(3) uninitialized data area (BSS) . The executable file is loaded in BSS, the location can be separated or close to the data segment, the data stored in the data segment (global uninitialized, static uninitialized data) life cycle for the entire program running process.
(4) stack (stack) . The compiler automatically allocates the release, storing the function's parameter values, return values, local variables, and so on. It is loaded and released in real time during the program's run, so the lifetime of the local variable is applied to the free stack space.
(5) . Used for dynamic memory allocation. The heap is in memory between the BSS area and the stack area. The programmer is usually assigned and released, and if the programmer does not release it, it may be recycled by the OS at the end of the program.


The system is divided into so many areas, mainly based on the following considerations :

    • The code snippet and data segment are separate, the runtime is easy to load separately, and the processor in the Harvard architecture will get better pipeline efficiency.
    • Code is executed sequentially, the processor PC pointer is read in turn, and the code can be shared by multiple programs, the data can be called multiple times throughout the run, if mixing the code and data will result in wasted space.
    • Temporary data and code that needs to be reused are placed in the stack at run time, with a short life cycle, which facilitates resource utilization.
    • The heap area can be assigned and freed by programmers, allowing the user to freely allocate and improve the flexibility of the program.
C Each storage type comparison


Transferred from: http://blog.csdn.net/tennysonsky/article/details/45113863


LINUX executable structure and process structure

Related Article

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.