Find the answer to your Linux question:
Results 1 to 2 of 2
Can any one explain what's the problem of the follwing simple program? Actually, this program seems to work well in RedHat7 (gcc 3.X.X), but not in my Debian (gcc 4.0.X). ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2006
    Posts
    1

    sbrk() ..? what's the problem ?


    Can any one explain what's the problem of the follwing simple program? Actually, this program seems to work well in RedHat7 (gcc 3.X.X), but not in my Debian (gcc 4.0.X).
    I would like to know about the memory consumption of some program.
    Thank a lot.

    -----------------------
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    void main(){

    int i;
    int size = 1000;
    int before, after;
    int msize;
    char *c;

    before = (int)sbrk(0)/1024;

    c = (char *)malloc(size * sizeof(char));
    for (i = 0; i < size; i++)
    c[i] = 'a';

    after = (int)sbrk(0)/1024;

    msize = after - before;
    printf("after-before = %d\n", msize);

    }

    RESULT:
    after-before = 0

    -----------------------

  2. #2
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    try getusage, it's meant for this kind of task.
    Code:
    /* this is the rsuage struct, linux supprts most of this */
    /*
    struct  rusage {
            struct timeval ru_utime;        /o user time used o/
            struct timeval ru_stime;        /o system time used o/
                                            /o actual values kept in proc struct o/
            long    ru_maxrss;              /o maximum resident working set size o/
            long    ru_ixrss;               /o integral shared memory size o/
            long    ru_idrss;               /o integral unshared data  o/
            long    ru_isrss;               /o integral unshared stack  o/
            long    ru_minflt;              /o page reclaims o/
            long    ru_majflt;              /o page faults o/
            long    ru_nswap;               /o swaps o/
            long    ru_inblock;             /o block input operations o/
            long    ru_oublock;             /o block output operations o/
            long    ru_ioch;                /o # of characters read/written o/
            long    ru_msgsnd;              /o messages sent o/
            long    ru_msgrcv;              /o messages received o/
            long    ru_nsignals;            /o signals received o/
            long    ru_nvcsw;               /o voluntary context switches o/
            long    ru_nivcsw;              /o involuntary  o/
    
    }; */
    
    
    
    
    #include <sys/resource.h>
    #include <stdlib.h>
    
    #define ok(x) \
    if( (x)==-1) {perror(""); exit(EXIT_FAILURE);}
    #define not_NULL(x) \
    if( (x)==NULL) {perror(""); exit(EXIT_FAILURE);}
    
    int main()
    {
        struct rusage before, after;
        char *buf=NULL;
        
        ok(getrusage(RUSAGE_SELF,&before));
        not_NULL(buf=malloc(1000000) );
        ok(getrusage(RUSAGE_SELF,&after));
        printf("maximum resident working set size  before    %d,  after    %d\n",before.ru_maxrss, after.ru_maxrss );
        printf("integral shared memory size        before    %d,  after    %d\n",before.ru_ixrss , after.ru_ixrss  );
        printf("integral unshared data             before    %d,  after    %d\n",before.ru_idrss , after.ru_idrss  );
        printf("integral unshared stack            before    %d,  after    %d\n",before.ru_isrss , after.ru_isrss  );
        free(buf);    
    	return 0;
    }

Posting Permissions

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