如何使用的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?

查看:2037
本文介绍了如何使用的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

须藤也没有帮助。有没有办法将它设置为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).

更新2:它实际上好像的ulimit -s<价值> 是每个外壳,并且如果您设置的第一个时间,它通常工作。问题是,当你把它第二次,那么它可能会返回一个错误)

(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上,从<一个拍摄的堆栈大小的硬性限制href=\"http://lists.apple.com/archives/scitech/2004/Oct/msg00124.html\">http://lists.apple.com/archives/scitech/2004/Oct/msg00124.html理所当然,这是很老了,林不知道如果仍然如此了,但设置它简单地调用的ulimit -s硬,其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
$

我也发现了这个<一个href=\"http://linuxtoosx.blogspot.com/2010/10/stack-overflow-increasing-stack-limit.html\">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天全站免登陆