如何使用 ulimit 或 Mac OS X 上的每个进程为 C 或 Ruby 程序更改堆栈大小? [英] How to change the stack size using ulimit or per process on Mac OS X for a C or Ruby program?

查看:12
本文介绍了如何使用 ulimit 或 Mac OS X 上的每个进程为 C 或 Ruby 程序更改堆栈大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎为 C 程序或 Ruby 程序(使用 C 堆栈)设置堆栈大小的推荐方法是在 Bash shell 中使用 ulimit.但是

It seems that the recommended way to set stack size for a C program or Ruby program (which uses the C stack), is by using ulimit in the Bash shell. But

$ ulimit -s
8192

$ ulimit -s 16384
-bash: ulimit: stack size: cannot modify limit: Operation not permitted

sudo 也无济于事.有没有办法将其设置为 16MB、32MB 或 64MB?我认为应该有一种方法可以在每次程序调用时设置它,而不是设置系统范围的参数?

and sudo doesn't help either. Is there a way to set it to 16MB, 32MB, or 64MB? I thought there should be a way to set it per program invocation instead of setting a system wide parameter as well?

现在 8192 可能意味着 8MB,如果与一个进程可以使用的内存相比,这相当小,有时甚至高达 2GB 的 RAM.

Right now 8192 probably means 8MB which is quite small, if that is compared to how much a process can be using, sometimes as much as 2GB of RAM.

(更新说明: ulimit -a 可以显示其当前值).

(updated note: ulimit -a can show its current values).

(update 2: 实际上似乎 ulimit -s <value> 是每个 shell 的,如果你第一次设置它,它通常可以工作.问题是当你第二次设置它时,它可能会返回错误)

(update 2: it actually seems like ulimit -s <value> is per shell, and that if you set it the first time, it usually works. The problem is when you set it the second time, then it may return an error)

推荐答案

显然对于 mac os x 的堆栈大小有硬性限制,取自 http://lists.apple.com/archives/scitech/2004/Oct/msg00124.html 授予这是相当老的,我不确定它是否仍然正确,但设置它只需调用 ulimit -s hard,它的 65532.或大约 65 兆.

Apparently there is a hard limit on the stack size for mac os x, taken from http://lists.apple.com/archives/scitech/2004/Oct/msg00124.html granted this is quite old, and Im not sure if its still true anymore, but to set it simply call ulimit -s hard, its 65532. or about 65 megs.

我在雪豹上做了一些测试,10.6.8,它似乎是真的.

I did some tests on snow leopard, 10.6.8, and it does seem to be true.

$ ulimit -a
...
stack size              (kbytes, -s) 8192
...
$ ulimit -s 65533
-bash: ulimit: stack size: cannot modify limit: Operation not permitted
$ ulimit -s 65532
$

我还发现了这个 http://linuxtoosx.blogspot.com/2010/10/stack-overflow-increasing-stack-limit.html 虽然我没有测试过,所以不能说太多.

I also found this http://linuxtoosx.blogspot.com/2010/10/stack-overflow-increasing-stack-limit.html though I haven't test it, so can't really say much about it.

当应用程序消耗通常从堆中获取的大量内存时,堆栈通常为局部自动变量保留,这些变量存在的时间相对较短,相当于函数调用的生命周期,堆是大部分持久数据存在.

When applications consume gigs of memory thats usually taken from the heap, the stack is usually reserve for local automatic variables that exist for a relatively small amount of time equivalent to the lifespan of the function call, the heap is where most of the persistent data lives.

这里有一个快速教程:

#include <stdlib.h>

#define NUMBER_OF_BYTES 10000000 // about 10 megs
void test()
{
   char stack_data[NUMBER_OF_BYTES];          // allocating on the stack.
   char *heap_data = malloc(NUMBER_OF_BYTES); // pointer (heap_data) lives on the stack, the actual data lives on the heap.
}

int main()
{   
    test(); 
    // at this point stack_data[NUMBER_OF_BYTES] and *heap_data have being removed, but malloc(NUMBER_OF_BYTES) persists.
    // depending on the calling convention either main or test are responssible for resetting the stack.
    // on most compilers including gcc, the caller (main) is responssible.

    return 0;
}

$ ulimit -a
...
stack size              (kbytes, -s) 8192
...
$ gcc m.c
$ ./a.out
Segmentation fault
$ ulimit -s hard
$ ./a.out
$

ulimit 只是暂时的,您每次都必须更新它,或者更新相应的 bash 脚本以自动设置它.

ulimit is only temporary you would have to update it every time, or update your corresponding bash script to set it automatically.

一旦设置了 ulimit,它只能降低,不能提高.

这篇关于如何使用 ulimit 或 Mac OS X 上的每个进程为 C 或 Ruby 程序更改堆栈大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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