process memory segment on linux

本文介绍了Linux内存的不同段,包括数据段、BSS段、只读数据段、堆段和栈段,并通过实验展示了这些段如何存储不同的变量类型,如全局变量、静态变量及常量字符串等。

There are several segments in linux memory, including data segment, BSS segment, Rodata segment, heap segment and stack segment.

 

The  Data Segment  contains global and static variables used by the program that are initialized

 

The  Bss Segment also known as uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code

 

Rodata (read-only data) segment starts at the end of data segment which contains all the constant variables and constant strings in the code

 

The Heap segment begins at the end of the BSS segment (if system has Rodata segment then it should be BSS segment) and grows to larger addresses from there.

 

The stack is a LIFO structure, typically located in the higher parts of memory.

 

Test.o is a file of Linux ELF format.

Objdump –h test.o #show all the sections in the ELF file.

Objdump –t test.o #show all symbol table in the ELF file.

Objdump –s test.o #show all the contents in the ELF file in hex.

Nm –a test.o #show symbol table in the ELF file.

 

Gcc –o test.i –E test.c   #produces preprocessed source code.

Gcc –o test.s –S test.i   #produces assembler code for each source code.

Gcc –o test.o –c test.s #compile source code to produce target object but do not link

Gcc –o test test.o         #link target object to produce executable file.

Ldd test                        #display static or dynamic linking library used by the executable file.

 

Dynamic linking library are compiled target objects. It stores symbol table of the target objects. In the linking stage, the executable will not link the dynamic linking library.  It will only record which library routines programs need and the index. When the executable calls the dynamic linking library it will be loaded into memory and mapped into process’s virtual address space. So it has a problem that dynamic linking library has its own data segment and if several processes access and modify the same global variable in the dynamic linking library the data will be polluted. So it is better not to allocate global variable in the dynamic linking library as a shared variable.

 

When an executable is executed, it is loaded into memory and operating system will give a 4G virtual address space (abbreviated VAS) to the process. All the objects used by process is stored in the memory, including text, data, BSS, heap, stack segments and those of static and dynamic linking library. Operating system is responsible for mapping the memory address of these objects to the process VAS so that process can use them. Process does not know the exact memory address of the resources it uses but it knows their addresses in the process VAS. This is the responsibility of memory management in the operation system.

 

example1:

From the experiment result, we can see that global and static variables that are initialized manually are located in the data segment and global and static variables that are not initialized are located in the bss segment and initialized with zero value by compiler. Local variables are stored in the stack so they are showed in the above result.

gcc compiler will treat global variable that is not initialized  as  a common symbol and when linking the common symbols are put into bss segment.

 

 

in the source above we declare a char * p that points to a constant string "123" and in the main function we call the function printf("%d",a) that contains a constant string "%d".

From the result above we can see that constant strings are stored in the rodata segment.

 

we declare a char array char tp[]="123vbcxv" but from the result above we can see that it is not stored in the rodata segment but in the data segment. This is because char array is not constant string but a sequence of characters stored in the data segment.

 

### Linux Shared Data Area Configuration and Usage In Linux systems, a shared data area typically refers to memory regions that multiple processes can access simultaneously. This concept plays an essential role in inter-process communication (IPC). The implementation of such areas involves several mechanisms including shared memory segments. To configure and use shared memory under Linux: #### Creating Shared Memory Segments A process creates or attaches to existing shared memory segments through system calls like `shmget` and `shmat`. Here's an example demonstrating creation and attachment of a shared memory segment[^1]: ```c #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> int main() { key_t key = ftok("testfile", 65); // Generate unique key int shmid = shmget(key, 1024, IPC_CREAT | 0666); if(shmid < 0){ printf("Error creating shared memory\n"); return -1; } char *str = (char*) shmat(shmid,(void*)0,0); strcpy(str,"Hello World"); printf("Data written in memory: %s\n", str); shmdt(str); // Detach from shared memory return 0; } ``` This code snippet illustrates how one might set up a simple string message within a shared memory space accessible by other programs running on the same machine. #### Access Control for Shared Memory Areas Permissions associated with each created object determine who has permission to read/write into this region. These permissions follow Unix file mode conventions when calling functions like `shmget`. For managing these resources effectively across different applications, developers often rely upon synchronization primitives provided either directly via POSIX APIs (`sem_init`, etc.) or indirectly through higher-level abstractions offered by programming languages themselves. Regarding specific tools mentioned earlier regarding network settings modification does not pertain directly to configuring shared data areas but rather pertains more closely towards administrative tasks involving hostname changes as noted previously .
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值