Find the answer to your Linux question:
Results 1 to 8 of 8
Following is the program executed, #include<sys/resource.h> #include<sys/time.h> #include<unistd.h> #include<malloc.h> #include<errno.h> int main() { struct rlimit r1,r2; char *p;int i=0; getrlimit(RLIMIT_DATA,&r1); printf("cur limit=%llu\n",r1.rlim_cur); printf("max limit=%llu\n",r1.rlim_max); r1.rlim_cur=100; r1.rlim_max=200; if(setrlimit(RLIMIT_DATA,&r1)!=0){ printf("callfailed and errno ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    7

    setrlimit isn't setting the memory limits. why?

    Following is the program executed,

    #include<sys/resource.h>
    #include<sys/time.h>
    #include<unistd.h>
    #include<malloc.h>
    #include<errno.h>

    int main()
    {
    struct rlimit r1,r2;
    char *p;int i=0;
    getrlimit(RLIMIT_DATA,&r1);
    printf("cur limit=%llu\n",r1.rlim_cur);
    printf("max limit=%llu\n",r1.rlim_max);

    r1.rlim_cur=100;
    r1.rlim_max=200;
    if(setrlimit(RLIMIT_DATA,&r1)!=0){
    printf("callfailed and errno is set to %d",errno);
    return -1;}

    getrlimit(RLIMIT_DATA,&r2);
    printf("cur limit=%llu\n",r2.rlim_cur);
    printf("max limit=%llu\n",r2.rlim_max);

    p=malloc(2000);
    if(p==NULL) {
    printf("allcation failed");
    return -1; }
    else {
    printf("malloc succesfull\n");}

    for(;i<250;i++)
    *(p+i)=(char)i;

    printf("done");
    free(p);
    return 0;
    }
    output
    ------
    cur limit=18446744073709551615
    max limit=18446744073709551615
    cur limit=100
    max limit=200
    malloc succesfull

    I undestand the malloc should fail and send out a signal to kill the process. But that doesnt happen. why?
    can someone please explain this behaviour?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    The first thing I did was to compile your program using the -Wall switch, which turns on warnings. I was warned that printf() was undeclared. I fixed that by including this in the source:
    Code:
    #include <stdio.h>
    That's always a good thing to do if you're going to use the normal printf(), as you seem to be doing here.

    I was also warned about a mismatch between a format for printing and the variable that was being printed. That would explain why my output was this:
    Code:
    cur limit=4612066196047527935
    max limit=4612066196047527935
    cur limit=4612066191752560740
    max limit=4612066191752560840
    malloc succesfull
    I fixed that by changing the %llu format to %lu in each of the four places it occurs.

    Are you sure you ran the code exactly as you posted it?

    At any rate, malloc() doesn't necessarily increase the total amount of memory the system has given you. It usually allocates to your program memory that the system has already given you. This memory is sitting in a data structure (maintained by the runtime library, including functions such as malloc()) called the heap.

    There are several reasons for this indirect approach to allocating memory; one reason is to avoid the CPU time expense of having to make a call to the operating system every time you need a chunk of memory.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    May 2008
    Posts
    7

    .

    Thanks for the reply.

    May be my question was not proper.let me reframe it--

    In the program setrlimit sets the momory limit to 200. Now when malloc allocates 300 bytes of memory it should raise a signal which whoud terminat the program.

    but the program executes fine. why?

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Now when malloc allocates 300 bytes of memory it should raise a signal which whoud terminat the program.

    but the program executes fine. why?
    malloc() is not getting 300 bytes of memory by requesting it from the operating system. It is getting 300 bytes of memory from available memory in the process's heap. The memory in the process's heap was allocated at the beginning of the process, before the call to setrlimit().
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    Jun 2008
    Posts
    34
    adevendra23,

    glibc malloc() use mmap() to satisfy your request if brk() failed.

    You can observe this by using the command "strace <your program".
    The output will look like:
    brk(0) = 0x804b000
    brk(0x806c000) = 0x804b000 <-return brk(0), not successful
    mmap2(NULL, 1048576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7d15000 <- pointer returned by malloc()

    If you print the errno after the malloc() statement, you will see it's set to 12 (ENOMEM), even though the malloc() is considered successful.

    This is a glibc behaviour.

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Fascinating.

    Steve Yau is correct, and I was wrong.

    Thanks! I learned something new today.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Just Joined!
    Join Date
    Jun 2008
    Posts
    34
    Fyi, there seems to be a patch fixing this behaviour:

    'Re: [PATCH] mm: include private data mappings in RLIMIT_DATA limit' - MARC

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    That's even more fascinating.

    glibc does the mmap() to get around the limit, and a kernel patch is introduced to neutralize that glibc workaround.

    This is sort of an arms race, isn't it?
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...