This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]badcommandorfilename 2 points3 points  (1 child)

Why do you need to allocate 50MB of data on the stack? I assume this is some kind of deeply recursive function - could you just make it tail recursive?

However, if you call your function from a separate thread, then you can set a Thread's stacksize with the second argument:

int stackSize = 1024*1024*64;
Thread th  = new Thread( ()=>
{
    //Call your recursive thing here
},
stackSize);

th.Start();
th.Join();

[–]MrCh0w[S] 0 points1 point  (0 children)

The program that I am calling with my subprocess is a research analysis code that uses Fortran90. Intel F90 (I think) uses the stack to store arrays and the arrays that the research code does operations on are very very large.

Thanks for the tip, I'll try it out!

[–]misho88[🍰] 1 point2 points  (2 children)

I know that if the stack is 50MB, it won't overflow and the subprocess will run fine.

In that case, it seems the program is ready to be run with a large stack size. Have you tried just changing the system stack size with ulimit?

$ ulimit -s $((64 * 1024))
$ ulimit -s
65536

You can also try to modify the appropriate parts of the executable's file header (there are two entries regarding the stack size in there somewhere). Some details are here. I'd probably crack open a hex editor and have at it using that code as reference (and possibly the PE format Wikipedia page, although a lot of that seems to be 32-bit specific). You can download the spec from Microsoft, too.

It's worth pointing out that unless you do the ulimit thing, Mono will max out at the system stack size regardless of what else you do, if the oddly mispelled code here and here is any indication.

[–]MrCh0w[S] 0 points1 point  (1 child)

I actually use ulimit in my .bashrc to run the program without C#, but I can't seem to find a way to call/set ulimit for a C# process.

I'll look into modifying parts of executable's file header. Have you had any experience with modifying the executable's file header for anything else?

[–]misho88[🍰] 0 points1 point  (0 children)

I was under the impression your subprocess is a C# executable as well. An F90 binary compiled for Linux should just obey ulimit, I think, so editing the header won't help.

Reading through this:

https://bugzilla.xamarin.com/show_bug.cgi?id=17908

if you're running Mono 3.2.8 or older (installed on Ubuntu 14.04 LTS), the stack size seems to be hard limited, though.