Linux executable file structure and process structure, linux Executable File

Source: Internet
Author: User

Linux executable file structure and process structure, linux Executable File
Linux executable file structure

In Linux, a program is a common executable file. The following lists the basic information about a binary executable file:



It can be seen that this executable file is stored in three parts: the code area (text), the data area (data), and the uninitialized data area (bss. The basic content of each section is described as follows:


Code Area:

The machine command that stores CPU execution. Generally, the code area is shareable (that is, another execution program can call it), so that it can be shared for the purposeFor programs that are frequently executed, you only need to have a copy of code in the memory.. The Code area is usually read-only, and the reason for its read-only is to prevent the program from accidentally modifying its commands. In addition, information about local variables is planned in the code area.


Commands in the code area include the operation code and operation object (or object address reference ). If it is an immediate number (that is, a specific value), it will be directly included in the Code. If it is local data, it will allocate space in the stack area during runtime, then reference the data address. If the data zone and data zone are not initialized, the data address will also be referenced in the code.


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

This area contains the global variables explicitly initialized in the program, initialized static variables (including Global static variables and local static variables), and constant data (such as string constants ).

For example, a global variable that is not declared in any function is as follows:

int count = 100;

The variable count is stored in the initialization data Zone according to its initial value.


You can define static variables at any location as follows:

static int num = 200;
This declares a static data and initializes it. If it is declared in any function in vitro, it indicates it is a static global variable. If it is in the function body (local ), it is a local static variable. In addition, if static is added before a function name, it indicates that the function can only be called in the current file.


Data zone not initialized (also called BSS zone):

The global uninitialized variables and static variables are saved. Data in the uninitialized data zone is initialized to 0 or NULL by the kernel before the program starts execution ).

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

long sum[1000];
Store sum in the uninitialized data zone.


Linux Process Structure

In Linux, if an executable file is loaded into the memory for running, it will evolve into one or more processes (the reason for multiple processes is that they can create new processes at run time, but there is only one process at load time ).Processes are the basic unit of Linux transaction management. All processes have their own independent processing environment and system resources.. The process environment is determined and composed of the current system status and its parent process information.


A comparison between the executable file storage structure and the Linux Process basic structure (part.


A process is a running program segment. A process mainly includes the space applied in the memory, code (loaded programs, including code segments, data segments, and BSS), heap, stack, and kernel process information structure provided by the kernel

Task_struct (in/usr/include/linux/sched. h), opened files, context (static description of the whole process of execution activity), and suspended signals.


(1)Code area (text segment). The executable code segment is loaded, and its location in the memory is completed by the loader.
(2)Global Initialization Data zone/static Data zone (Data Segment). The executable file data segment is loaded, and the data stored in the data segment (Global initialization, static initialization data) is stored throughout the program running process.
(3)Uninitialized data zone (BSS). The executable file BSS segment can be loaded separately or closely following the data segment. The lifecycle of data stored in the data segment (Global uninitialized, static uninitialized data) is the entire program running process.
(4)Stack). The compiler automatically allocates the release and stores the parameter values, return values, and local variables of the function. Therefore, the life cycle of local variables is applied to release the stack space.
(5)Heap). Used for dynamic memory allocation. The heap is located in the memory between the BSS zone and the stack zone. Generally, it is assigned and released by the programmer. If the programmer does not release the program, it may be recycled by the OS when the program ends.


The reason why the system is divided into so many regions is mainly based on the following considerations::

  • The code segments and data segments are separated to facilitate separate loading during runtime. The processors in the Harvard architecture will achieve better pipeline efficiency.
  • The code is read in sequence by the PC pointer of the processor, and the code can be shared by multiple programs. data may be called multiple times during the entire process, if code and data are mixed together, space is wasted.
  • Temporary data and the code that needs to be used again are put into the stack at runtime, and the life cycle is short, which facilitates improving resource utilization.
  • The heap area can be allocated and released by programmers, so that users can freely allocate and improve program flexibility.


C comparison of various storage types



References: Linux 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.