bss和数据的最大大小 [英] maximum size of bss and data

查看:44
本文介绍了bss和数据的最大大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在编译时在C程序中声明所有变量,例如:

 字符缓存[CACHE_SIZE];char udp_ring [MAX_UDP_PACKET_SIZE * MAX_REQUESTS];int num_packets;char error_codes [NUM_ERRORS] [MAX_ERROR_STRING] = {{未知用户\ n"},{密码错误\ n"},....}; 

问题是,当C程序中的变量进入BSS或DATA段时,它们的大小是否受到限制?例如,如果我声明CACHE_SIZE为8GB RAM,它将起作用吗?32位或64位有什么区别吗?我计划在Linux上运行该程序,并且我的RLIMIT_DATA配置没有任何限制.

解决方案

您将能够管理内核允许进程处理的虚拟内存,这取决于体系结构.

例如,在x86体系结构(无x86-64长模式)上,Linux默认情况下将进程看到的虚拟内存拆分为3GB(对于进程)和1GB(对于内核)(即使启用了PAE):您的进程将无法处理超过3GB的虚拟内存(包括文本部分,数据,bss,堆,堆栈,共享对象等)

如果您静态分配所有缓冲区,而内核无法将其放入进程虚拟地址空间中,则它将在启动时被杀死:使用8GB缓冲区在32位体系结构上通常会导致这种行为./p>

如果您不想依赖glibc的内存管理功能( malloc ,...),则可以滚动自己的内存管理库并强制您的进程将其与 LD_PRELOAD 技巧,这样您就可以定义一个满足您自己需求的 malloc/calloc/realloc/free (使用 sbrk())实现.

I want to declare all the variables in my C program at compile time, like for example:

char cache[CACHE_SIZE];
char udp_ring[MAX_UDP_PACKET_SIZE*MAX_REQUESTS];
int  num_packets;
char error_codes[NUM_ERRORS][MAX_ERROR_STRING]= {
    {"Unknown user\n"},
    {"Wrong password\n"},
    ....
};

The question is, are there any limits on the size of the variables in a C program when they go in BSS or DATA segment? For example if I declare CACHE_SIZE of 8GB of RAM, will it work? Is there any difference for 32 bits or 64 bits? I plan to run the program on Linux and there will be no restriction in my RLIMIT_DATA configuration.

解决方案

You will be able to manage as much virtual memory as your kernel allows processes to handle : it will depend on the architecture.

For example, on a x86 architecture (without x86-64 long mode), Linux splits by default the virtual memory seen by a process in 3GB for the process and 1GB for the kernel (even if PAE is enabled) : your process won't be able to handle more than 3GB of virtual memory (include text sections, data, bss, heap, stack, shared objects, etc.)

If you allocate all your buffer statically and the kernel cannot fit it in the process virtual address space, it will be killed at start-up : using a 8GB buffer will mostly result in this behavior on a 32-bit architecture.

If you don't want to rely on glibc's memory management function (malloc, ...), you could roll your own memory management library and force your process to use it with a LD_PRELOAD trick, that way you could define a malloc/calloc/realloc/free (using sbrk()) implementation matching your own requirements.

这篇关于bss和数据的最大大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆