Thread: Process Resources and System Limits Problem

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    Process Resources and System Limits Problem

    Hello,

    i am trying to control system limits in my Debian - GNU,my maintarget is to control the memory that the proram can have, as many of you know, the kernel is giving some amount of memory to each process, now you can control it by setrlimit, so what i wanted to do, set the cur to 10mb and the max to 20mb, when i pass the cur i should get a signal, so i malloc 15mb, but on that one i got error ? whats wrong i mean ? i should be able to malloc 15, the problem is when i malloc 9mb that is less then 10 i get error too ? it seems i cant change the limit or somthing, when i call getrusage most of the values i get is 0, on maxrss and ixrss, please can anyone watch my program or test it, i made cpu test, it seems to work, i dont know if its working good too, but the memory is what i am looking for, malloc -> get a signal -> malloc the rest -> the program get killed.

    on rlimit function - http://www.huihoo.com/freebsd/freebs...html/ch07.html

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/time.h>
    #include <sys/resource.h>
    #include <signal.h>
    #include <math.h>
    
    
    //  RLIM_INFINITY - UNLIMITED
    
    // RLIMIT_CPU - CPU TIME / IN SEC
    // RLIMIT_AS - TOTAL VIRTUAL MEMORY
    // RLIMIT_RSS - TOTAL RESIDENT SIZE
    // RLIMIT_DATA - The RLIMIT_DATA limit specifies the maximum
    // amount of bytes the process data segment can occupy. 
    // The data segment for a process is the area in which dynamic memory is located
    // (that is, memory allocated by malloc() in C, 
    //or in C++, with new()).
    
    static void signal_beforedeath(int signo)
    {
        fprintf(stderr,"\nsignal recvied %d \n",signo);
     		sleep(5);  
        //exit(0);
    }
    
    int main (int argc, char *argv[])
    {
    	struct rlimit tp,hh,core;
    	struct rusage la;
    	int i,s1,s2;
    	char *s;
    	core.rlim_cur=0;
    	core.rlim_max=0;
    	setrlimit(RLIMIT_CORE,&core); // GOOD!! NO CORE FILES WILL BE CREATED.
    	s1=RLIMIT_CPU;
    	s2=RLIMIT_DATA;
    	if ((getrlimit(s1,&tp)==0)&&(getrlimit(s2,&hh)==0))
            {
       	printf("CPU LIMIT: %d %d\nMEMORY LIMIT %d %d\n",tp.rlim_cur,tp.rlim_max,hh.rlim_cur,hh.rlim_max);
       	tp.rlim_cur=1;
       	tp.rlim_max=2;
       	hh.rlim_cur=10000000; // 10mb
       	hh.rlim_max=20000000; // 20mb
       	if ((setrlimit(s1,&tp)==0)&&(setrlimit(s2,&hh)==0)&&(setrlimit(RLIMIT_AS,&hh)==0))
        	{
         		if ((getrlimit(s1,&tp)==0)&&(getrlimit(s2,&hh)==0))
         		 { 
           	  printf("CPU LIMIT UPDATED: %d %d (CPU/s)\nMEMORY LIMIT UPDATED %d %d MB/s\n",
           	         	  tp.rlim_cur,tp.rlim_max,hh.rlim_cur,hh.rlim_max);
           	  signal(SIGSEGV, signal_beforedeath);
               // CPU TEST
           	  signal(SIGXCPU, signal_beforedeath);
           	  for (i = 0; i < 50000000; i++)
           	  {
           	  if ((s=malloc(9000000)) == NULL)
           	  {
                printf("error on malloc 9MB\n"); 
               }
               if (s) free(s);
           	  if ((s=malloc(15000000)) == NULL)
           	  {
                printf("error on malloc 15MB\n"); 
               }
               if (s) free(s);
           	  if ((s=malloc(25000000)) == NULL)
           	  {
                printf("error on malloc 25MB\n"); 
               }
               if (s) free(s);
    
           	 getrusage(RUSAGE_SELF,&la);
           	 //printf("[%d][%d][%d]",ENOMEM,errno,s2);
           	 printf("resident: %d\n",la.ru_maxrss);
           	 printf("total amount of memory: %d\n",la.ru_ixrss);
           	 printf("CPU: %d\n",la.ru_idrss);
           	 printf("CPU: %d\n",la.ru_isrss);
           	 printf("CPU: %d\n",la.ru_minflt);
           	 printf("CPU: %d\n",la.ru_majflt);
           	 printf("CPU: %d\n",la.ru_nswap);
           	 printf("CPU: %d\n",la.ru_inblock);
           	 printf("CPU: %d\n",la.ru_oublock);
           	 printf("CPU: %d\n",la.ru_msgsnd);
           	 printf("CPU: %d\n",la.ru_msgrcv);
           	 printf("CPU: %d\n",la.ru_nsignals);
           	 printf("CPU: %d\n",la.ru_nvcsw);
           	        	 printf("CPU: %d\n",la.ru_nivcsw);
           	        	// sleep(2);
    
           	 }
             } 
         		else
          	{
           		printf("Call failed 2\n");
          	}
        	}
      	}
     	else
      	{
       	printf("Call failed 1\n");
      	}
     	return 0;
    }
    Last edited by Salem; 09-06-2005 at 05:50 AM. Reason: folding long lines

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. The indentation of your code sucks - just look at that column of closing braces.

    2. Check your units - I doubt that memory is managed in bytes in this interface.
    hh.rlim_cur=10000000; // 10mb
    My initial guess (that is, you go read the manual to find out) that memory is measured either in KB, or in pages. Lower level memory stuff usually works in integral numbers of pages.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    2
    LIMIT_AS
    The maximum size of the process's virtual memory (address space) in bytes.


    1024(byte) * 1000(kb) * 10 = 10mb

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  2. Getting at system Icon resources
    By Mastadex in forum Windows Programming
    Replies: 12
    Last Post: 06-29-2007, 09:19 AM
  3. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 05:35 PM
  4. ideas on how to make this not use 99% system resources?
    By aciarlillo in forum C++ Programming
    Replies: 3
    Last Post: 07-22-2005, 06:50 PM
  5. GlobalMemoryStatus to get "system resources free&
    By hanhao in forum Windows Programming
    Replies: 0
    Last Post: 03-20-2004, 02:30 AM