Welcome to MacBoardz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Why doesn't this program crash on a Mac ?

 
   Macintosh computer (Home) -> Programmer Help RSS
Next:  Mouse event handling (Carbon)  
Author Message
Thomas Deniau

External


Since: Jan 31, 2008
Posts: 1



(Msg. 1) Posted: Thu Jan 31, 2008 9:24 am
Post subject: Why doesn't this program crash on a Mac ?
Archived from groups: comp>sys>mac>programmer>help (more info?)

It should, as I'm using setrlimit() to limit memory usage...
On a GNU/Linux system it does crash.



#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
struct rlimit lim;
int i;

if(getrlimit(RLIMIT_AS, &lim) == -1) return 1;
printf("%d\n", lim.rlim_cur);

lim.rlim_cur = lim.rlim_max = 100 * 1024;
if(setrlimit(RLIMIT_AS, &lim) == -1) return 2;

if(getrlimit(RLIMIT_AS, &lim) == -1) return 1;
printf("%d\n", lim.rlim_cur);

char *v = malloc(100000000);
for (i=1;i<1000000;i++)
v[i]=(v[i-1]+3)%200; // pour pas que le malloc se fasse optimiser,
on sait jamais
printf("%d\n",42);

}


Thanks !

--
Thomas Deniau

 >> Stay informed about: Why doesn't this program crash on a Mac ? 
Back to top
Login to vote
Gregory Weston1

External


Since: Oct 03, 2004
Posts: 2243



(Msg. 2) Posted: Thu Jan 31, 2008 9:30 am
Post subject: Re: Why doesn't this program crash on a Mac ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article ,
thomas.RemoveThis@deniau.org (Thomas Deniau) wrote:

> It should, as I'm using setrlimit() to limit memory usage...
> On a GNU/Linux system it does crash.

I think any time you're making the statement that a piece of code
"should" crash you're into kind of weird space. I'd argue that
abstractly any crash is to be avoided if possible and point out that
absolutely nothing I can find in the documentation for setrlimit
requires a process exceeding its limits to crash (or respond in any
specific way).

The short answer is that it didn't crash because malloc (part of the C
standard library, not system call as setrlimit is) was - at the time it
was invoked - able to satisfy the request. Why it was able to do that is
an implementation detail; presumably it already had a pool reserved.
It's not guaranteed to succeed or to not succeed, which is why it's
important to check that malloc returned something other than NULL.

 >> Stay informed about: Why doesn't this program crash on a Mac ? 
Back to top
Login to vote
Ingo Paschke

External


Since: Jan 26, 2008
Posts: 2



(Msg. 3) Posted: Thu Jan 31, 2008 3:03 pm
Post subject: Re: Why doesn't this program crash on a Mac ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thomas Deniau wrote:

> It should, as I'm using setrlimit() to limit memory usage...
> On a GNU/Linux system it does crash.

$ man setrlimit

[...]
RLIMIT_RSS The maximum size (in bytes) to which a process's resident set size
may grow. This imposes a limit on the amount of physical memory to
be given to a process; if memory is tight, the system will prefer to
take memory from processes that are exceeding their declared resident
set size.
[...]

RLIMIT_AS is just an alias for RLIMIT_RSS on Darwin.

HTH,
Ingo.
 >> Stay informed about: Why doesn't this program crash on a Mac ? 
Back to top
Login to vote
Ben Artin

External


Since: Jun 20, 2005
Posts: 30



(Msg. 4) Posted: Thu Jan 31, 2008 5:31 pm
Post subject: Re: Why doesn't this program crash on a Mac ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article ,
thomas RemoveThis @deniau.org (Thomas Deniau) wrote:

> It should, as I'm using setrlimit() to limit memory usage...
> On a GNU/Linux system it does crash.

RLIMIT_AS limits the amount of physical memory, not virtual memory. What are you
trying to accomplish?

Ben

> int main(void)
> {
> struct rlimit lim;
> int i;
>
> if(getrlimit(RLIMIT_AS, &lim) == -1) return 1;
> printf("%d\n", lim.rlim_cur);
>
> lim.rlim_cur = lim.rlim_max = 100 * 1024;
> if(setrlimit(RLIMIT_AS, &lim) == -1) return 2;
>
> if(getrlimit(RLIMIT_AS, &lim) == -1) return 1;
> printf("%d\n", lim.rlim_cur);
>
> char *v = malloc(100000000);
> for (i=1;i<1000000;i++)
> v[i]=(v[i-1]+3)%200; // pour pas que le malloc se fasse optimiser,
> on sait jamais
> printf("%d\n",42);
>
> }

--
If this message helped you, consider buying an item
from my wish list: <http://artins.org/ben/wishlist>
 >> Stay informed about: Why doesn't this program crash on a Mac ? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Program crash - My program crashes under os x 10.2.8, and it works fine on 10.3.5., the place where it crashes is when my program calls function from certain file (i even made new function, that call different function from that file it still crashes when it calls from...

Help With Program - Hi-- I know this likely isn't the place to post a Help Wanted, but I don't know where else to go, so please excuse my rudeness. I'm looking for a programmer with UNIX and Codewarrior experience to do a rather spectacular program for Mac. If you're..

how to compile and run "c" program using MPW - hai how to compile and run "c" program using MPW. -- Direct access to this group with http://web2news.com http://web2news.com/?comp.sys.mac.programmer.help

Localizing a program - I'm localizing a Cocoa program and part of that is building a Localizable.strings file with an English version. This contains strings that are part of the program in the code like button labels that change and format strings for stringWithFormat methods...

Converting an IBM program to Mac, help please - My husband and I wrote a little program (94Kb) in Windows Visual Basic and donated it to the NeedleBar site, the best on the Net for antique sewing machine collectors. It consists of the .exe file where a sewing maker is chosen and a serial number is..
   Macintosh computer (Home) -> Programmer Help All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]