Unix Programming - How to use setrlimit()?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2005 > How to use setrlimit()?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to use setrlimit()?
Kevin

2005-10-24, 3:48 pm

How do I use this function with the resource RLIMIT_DATA?

I set the rlimit structure field definitions, call setrlimit, and then
allocate memory which is larger than the limit. Is this the correct
implementation?

getrlimit returns the information that I previously set.

Thanks


Maxim Yegorushkin

2005-10-24, 3:48 pm


Kevin wrote:
> How do I use this function with the resource RLIMIT_DATA?
>
> I set the rlimit structure field definitions, call setrlimit, and then
> allocate memory which is larger than the limit. Is this the correct
> implementation?
>
> getrlimit returns the information that I previously set.


RLIMIT_DATA only affects sbrk() call as it is said in the man page.

If you allocate large chunks (usually larger than 128k) using glibc
malloc() it actually allocates them using memory obtained via mmap()
call rather than sbrk() and RLIMIT_DATA has nothing to do with mmap().

Kevin

2005-10-24, 3:48 pm

Thanks for the explanation. Is there a way to limit memory usage of a
process then?

"Maxim Yegorushkin" <maxim.yegorushkin@gmail.com> wrote in message
news:1129745888.717416.296510@f14g2000cwb.googlegroups.com...
>
> Kevin wrote:
>
> RLIMIT_DATA only affects sbrk() call as it is said in the man page.
>
> If you allocate large chunks (usually larger than 128k) using glibc
> malloc() it actually allocates them using memory obtained via mmap()
> call rather than sbrk() and RLIMIT_DATA has nothing to do with mmap().
>



David Schwartz

2005-10-24, 3:48 pm


"Kevin" <kmar@rogers.com> wrote in message
news:JOqdnTVLpI6tKcveRVn-sg@rogers.com...

> Thanks for the explanation. Is there a way to limit memory usage of a
> process then?


What does "memory usage of a process" mean? Do you mean physical memory?
Virtual memory?

DS


Kevin

2005-10-24, 3:48 pm

Yes, physical memory...

"David Schwartz" <davids@webmaster.com> wrote in message
news:dj6dc5$g5l$1@nntp.webmaster.com...
>
> "Kevin" <kmar@rogers.com> wrote in message
> news:JOqdnTVLpI6tKcveRVn-sg@rogers.com...
>
>
> What does "memory usage of a process" mean? Do you mean physical
> memory? Virtual memory?
>
> DS
>
>



David Schwartz

2005-10-24, 3:48 pm


"Kevin" <kmar@rogers.com> wrote in message
news:Jd-dnTTPVdkARcveRVn-hw@rogers.com...

> Yes, physical memory...


Then use setrlimit(RLIMIT_RSS), but note that limiting a process's use
of physical memory usually slows the whole system down. (Because it has to
do more disk I/O.)

DS


Kevin

2005-10-24, 3:48 pm

Thanks David for the help.

Is this the correct implementation? How come malloc worked?

int main()
{
char *a;
struct rlimit lim;

lim.rlim_cur = 0;
lim.rlim_max = 1000;

if(!setrlimit(RLIMIT)RSS, &lim)) {
printf("setrlimit failed\n");
}

a = (char *)malloc(3000);

if(a) {
printf("malloc successful\n");
}

free(a);
return 0;
}

"David Schwartz" <davids@webmaster.com> wrote in message
news:dj6mt5$jp3$1@nntp.webmaster.com...
>
> "Kevin" <kmar@rogers.com> wrote in message
> news:Jd-dnTTPVdkARcveRVn-hw@rogers.com...
>
>
> Then use setrlimit(RLIMIT_RSS), but note that limiting a process's use
> of physical memory usually slows the whole system down. (Because it has to
> do more disk I/O.)
>
> DS
>
>



David Schwartz

2005-10-24, 3:48 pm


"Kevin" <kmar@rogers.com> wrote in message
news:FLednbJ5l4biY8venZ2dnUVZ_s2dnZ2d@ro
gers.com...

> Thanks David for the help.
>
> Is this the correct implementation? How come malloc worked?


You said you wanted to limit *physical* memory, then you allocated a lot
of *virtual* memory. Why shouldn't it have worked?

> int main()
> {
> char *a;
> struct rlimit lim;
>
> lim.rlim_cur = 0;
> lim.rlim_max = 1000;
>
> if(!setrlimit(RLIMIT)RSS, &lim)) {
> printf("setrlimit failed\n");
> }
>
> a = (char *)malloc(3000);
>
> if(a) {
> printf("malloc successful\n");
> }
>
> free(a);
> return 0;
> }


What do you expect the OS to do? So long as it's possible to keep at
least one page resident in physical memory, there's no reason for the
'malloc' to fail.

You aren't telling us what you want, so you're getting bad anwers. Is
your question "how can I make 'malloc' fail?'

DS


James Antill

2005-10-24, 3:48 pm

On Wed, 19 Oct 2005 17:03:47 -0700, David Schwartz wrote:

>
> "Kevin" <kmar@rogers.com> wrote in message
> news:Jd-dnTTPVdkARcveRVn-hw@rogers.com...
>
>
> Then use setrlimit(RLIMIT_RSS), but note that limiting a process's use
> of physical memory usually slows the whole system down. (Because it has to
> do more disk I/O.)


man setrlimit says:

RLIMIT_RSS
Specifies the limit (in pages) of the process’s resident set
(the number of virtual pages resident in RAM). This limit only
has effect in Linux 2.4 onwards, and there only affects calls
to madvise() specifying MADVISE_WILLNEED.

....so that doesn't really work either.

--
James Antill -- james@and.org
http://www.and.org/and-httpd

Maxim Yegorushkin

2005-10-24, 3:48 pm


Kevin wrote:
> Thanks for the explanation. Is there a way to limit memory usage of a
> process then?


It depends on your connotation of "memory usage of a process".

If you just want to limit memory obtained via malloc() you can link to
your own malloc(), which will check the limits and forward the call to
the glibc malloc().

Maxim Yegorushkin

2005-10-24, 3:48 pm


Kevin wrote:
> Yes, physical memory...


Probably RLIMIT_AS is what you need.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com