There are certain situations where you may want to limit your process's use of system resources like CPU or may want to put certain limitations on things like number of open file descriptors, maximum memory your process can allocate for its data etc. You may use commands like ulimit, sysconf etc to control these resources from shell but through code there are two functions to achieve this : Code: #include <sys/time.h> #include <sys/resource.h> int getrlimit(int resource, struct rlimit *rlim); int setrlimit(int resource, const struct rlimit *rlim); The above two functions are used to get and set resource limits. Each resource has a corresponding soft and hard limit. A soft limit is something which any process can alter (from 0 to hard limit) but to play with hard limit a super user process is required. The soft and hard limit are defined by the structure : Code: struct rlimit { rlim_t rlim_cur; /* Soft limit */ rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */ }; In both the functions above, the parameter 'resource' tells the resource we want to limit and through the structure rlimit we pass the limit value. The below mentioned description of resources are very important. Please go through the description carefully before proceeding to examples. The resource that can be controlled are (From the man page) : RLIMIT_AS The maximum size of the process's virtual memory (address space) in bytes. This limit affects calls to brk(2), mmap(2) and mremap(2), which fail with the error ENOMEM upon exceeding this limit. Also automatic stack expansion will fail (and generate a SIGSEGV that kills the process if no alternate stack has been made available via sigaltstack(2)). Since the value is a long, on machines with a 32-bit longeither this limit is at most 2 GiB, or this resource is unlimited. RLIMIT_CORE Maximum size of core file. When 0 no core dump files are created. When non-zero, larger dumps are truncated to this size. RLIMIT_CPU CPU time limit in seconds. When the process reaches the soft limit, it is sent a SIGXCPU signal. The default action for this signal is to terminate the process. However, the signal can be caught, and the handler can return control to the main program. If the process continues to consume CPU time, it will be sent SIGXCPU once per second until the hard limit is reached, at which time it is sent SIGKILL. (This latter point describes Linux 2.2 through 2.6 behavior. Implementations vary in how they treat processes which continue to consume CPU time after reaching the soft limit. Portable applications that need to catch this signal should perform an orderly termination upon first receipt of SIGXCPU.) RLIMIT_DATA The maximum size of the process's data segment (initialized data, uninitialized data, and heap). This limit affects calls to brk(2) and sbrk(2), which fail with the error ENOMEM upon encountering the soft limit of this resource. RLIMIT_FSIZE The maximum size of files that the process may create. Attempts to extend a file beyond this limit result in delivery of a SIGXFSZ signal. By default, this signal terminates a process, but a process can catch this signal instead, in which case the relevant system call (e.g., write(2), truncate(2)) fails with the error EFBIG. RLIMIT_LOCKS (Early Linux 2.4 only) A limit on the combined number of flock(2) locks and fcntl(2) leases that this process may establish. RLIMIT_MEMLOCK The maximum number of bytes of memory that may be locked into RAM. In effect this limit is rounded down to the nearest multiple of the system page size. This limit affects mlock(2) and mlockall(2) and the mmap(2) MAP_LOCKED operation. Since Linux 2.6.9 it also affects the shmctl(2) SHM_LOCK operation, where it sets a maximum on the total bytes in shared memory segments (see shmget(2)) that may be locked by the real user ID of the calling process. The shmctl(2) SHM_LOCK locks are accounted for separately from the per-process memory locks established by mlock(2), mlockall(2), and mmap(2) MAP_LOCKED; a process can lock bytes up to this limit in each of these two categories. In Linux kernels before 2.6.9, this limit controlled the amount of memory that could be locked by a privileged process. Since Linux 2.6.9, no limits are placed on the amount of memory that a privileged process may lock, and this limit instead governs the amount of memory that an unprivileged process may lock. RLIMIT_MSGQUEUE (Since Linux 2.6.8) Specifies the limit on the number of bytes that can be allocated for POSIX message queues for the real user ID of the calling process. This limit is enforced for mq_open(3). Each message queue that the user creates counts (until it is removed) against this limit according to the formula: bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) + attr.mq_maxmsg * attr.mq_msgsize where attr is the mq_attr structure specified as the fourth argument to mq_open(3). The first addend in the formula, which includes sizeof(struct msg_msg *) (4 bytes on Linux/i386), ensures that the user cannot create an unlimited number of zero-length messages (such messages nevertheless each consume some system memory for bookkeeping overhead). RLIMIT_NICE (since Linux 2.6.12, but see BUGS below) Specifies a ceiling to which the process's nice value can be raised using setpriority(2) or nice(2). The actual ceiling for the nice value is calculated as 20 - rlim_cur. (This strangeness occurs because negative numbers cannot be specified as resource limit values, since they typically have special meanings. For example, RLIM_INFINITY typically is the same as -1.) RLIMIT_NOFILE Specifies a value one greater than the maximum file descriptor number that can be opened by this process. Attempts (open(2), pipe(2), dup(2), etc.) to exceed this limit yield the error EMFILE. (Historically, this limit was named RLIMIT_OFILE on BSD.) RLIMIT_NPROC The maximum number of processes (or, more precisely on Linux, threads) that can be created for the real user ID of the calling process. Upon encountering this limit, fork(2) fails with the error EAGAIN. 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.x, x < 30, and there only affects calls to madvise(2) specifying MADV_WILLNEED. RLIMIT_RTPRIO (Since Linux 2.6.12) Specifies a ceiling on the real-time priority that may be set for this process using sched_setscheduler(2) and sched_setparam(2). RLIMIT_RTTIME (Since Linux 2.6.25) Specifies a limit on the amount of CPU time that a process scheduled under a real-time scheduling policy may consume without making a blocking system call. For the purpose of this limit, each time a process makes a blocking system call, the count of its consumed CPU time is reset to zero. The CPU time count is not reset if the process continues trying to use the CPU but is preempted, its time slice expires, or it calls sched_yield(2). Upon reaching the soft limit, the process is sent a SIGXCPU signal. If the process catches or ignores this signal and continues consuming CPU time, then SIGXCPU will be generated once each second until the hard limit is reached, at which point the process is sent a SIGKILLsignal. The intended use of this limit is to stop a runaway real-time process from locking up the system. RLIMIT_SIGPENDING (Since Linux 2.6.8) Specifies the limit on the number of signals that may be queued for the real user ID of the calling process. Both standard and real-time signals are counted for the purpose of checking this limit. However, the limit is only enforced for sigqueue(2); it is always possible to use kill(2) to queue one instance of any of the signals that are not already queued to the process. RLIMIT_STACK The maximum size of the process stack, in bytes. Upon reaching this limit, a SIGSEGV signal is generated. To handle this signal, a process must employ an alternate signal stack (sigaltstack(2)). Since Linux 2.6.23, this limit also determines the amount of space used for the process's command-line arguments and environment variables; for details, see execve(2). An example (Changing RLIMIT_CPU) As we see above that there are various resources whose values we can change get and set through these two functions, lets try and use these functions to see how they work. Here is a sample code that tries to change RLIMIT_CPU: Code: #include <sys/resource.h> #include <sys/time.h> #include <unistd.h> #include<stdio.h> int main () { // Define and object of structure // rlimit. struct rlimit rl; // First get the time limit on CPU getrlimit (RLIMIT_CPU, &rl); printf("\n Default value is : %lld\n", (long long int)rl.rlim_cur); // Change the time limit rl.rlim_cur = 1; // Now call setrlimit() to set the // changed value. setrlimit (RLIMIT_CPU, &rl); // Again get the limit and check getrlimit (RLIMIT_CPU, &rl); printf("\n Default value now is : %lld\n", (long long int)rl.rlim_cur); // Simulate a long time consuming work while (1); return 0; } In the code above : We first get the current limit on CPU time (since there is no limit to start off so the time limit should be -1) Now after that we assign the value 1 as soft limit and call setrlimit() function to set this value as the new soft limit. Again we get the value to cross check whether the new value is now active or not. After that we get into an indefinite while loop to simulate a long work so that we can check whether or not our process is killed (Read RLIMIT_CPU above) Here is what happens when we run the program : Code: $ ./setlimit Default value is : -1 Default value now is : 1 CPU time limit exceeded As expected in the output above, the default value was -1, then we get our changed value 1 and after 1 second of processing the process is terminated by the signal SIGXCPU. Limiting the memory allocation for a Process In this section we will present a code that will restrict the memory usage for our process. Here is the code : Code: #include <sys/resource.h> #include <sys/time.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { // Define and object of structure // rlimit. struct rlimit rl; // First get the limit on memory getrlimit (RLIMIT_AS, &rl); printf("\n Default value is : %lld\n", (long long int)rl.rlim_cur); // Change the limit rl.rlim_cur = 100; // Now call setrlimit() to set the // changed value. setrlimit (RLIMIT_AS, &rl); // Again get the limit and check getrlimit (RLIMIT_AS, &rl); printf("\n Default value now is : %lld\n", (long long int)rl.rlim_cur); // Try to allocate more memory than the set limit char *ptr = NULL; ptr = (char*) malloc(1024); if(NULL == ptr) { printf("\n Memory allocation failed\n"); return -1; } free(ptr); return 0; } In the above code : We have put a limit to the maximum memory our program can allocate Next we try to allocate memory larger than the size we have set as limit. If memory allocation fails, we print a log. Here is the output : Code: $ ./setlimit Default value is : -1 Default value now is : 100 Memory allocation failed We see that memory allocation failed which was expected. Limiting the number of open file descriptors In this section we will present a code that will limit the number of open file descriptors. Here is the code : Code: #include <sys/resource.h> #include <sys/time.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { // Define and object of structure // rlimit. struct rlimit rl; // First get the limit on open files getrlimit (RLIMIT_NOFILE, &rl); printf("\n Default value is : %lld\n", (long long int)rl.rlim_cur); // Change the limit rl.rlim_cur = 4; // 3 are for stdin, stdout, stderr and one extra // Now call setrlimit() to set the // changed value. setrlimit (RLIMIT_NOFILE, &rl); // Again get the limit and check getrlimit (RLIMIT_NOFILE, &rl); printf("\n Default value now is : %lld\n", (long long int)rl.rlim_cur); // Try opening more than one file FILE *fp = NULL; int i=0; for (i=0; i<2; i++) { fp = NULL; fp = fopen("test.txt","r"); if(NULL == fp) { printf("\n fopen failed [%d]\n", i); return -1; } } return 0; } In the code above : We have changed to the value of number of open file descriptors from the default value to 1. Then we try to open 2 files in our logic Ideally the opening of second file should fail. Lets see the output : Code: $ ./setlimit Default value is : 1024 Default value now is : 4 fopen failed [1] We see that opening of the second file failed as we had only capacity of opening one file. Limiting the number of child processes Here is the code : Code: #include <sys/resource.h> #include <sys/time.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { // Define and object of structure // rlimit. struct rlimit rl; // First get the limit on number of child processes getrlimit (RLIMIT_NPROC, &rl); printf("\n Default value is : %lld\n", (long long int)rl.rlim_cur); // Change the limit rl.rlim_cur = 0; // Now we do not want this process to have any child process // Now call setrlimit() to set the // changed value. setrlimit (RLIMIT_NPROC, &rl); // Again get the limit and check getrlimit (RLIMIT_NPROC, &rl); printf("\n Default value now is : %lld\n", (long long int)rl.rlim_cur); // Try creating a process if( -1 == fork()) { printf("\n Creating a child process failed\n"); } return 0; } In the code above : We changed the limiting value for number of child process to zero so that no child process can be created. Next we try to create a process through fork() Ideally the function fork() should fail in this situation. Lets see the output : Code: $ ./setlimit Default value is : -1 Default value now is : 0 Creating a child process failed As seen clearly from the output, creation of a new child process failed. Conclusion To conclude, in this article we understood the usage (through practical examples) of the functions getrlimit and setrlimit which are used to get and set the value of various system resources. Stay tuned for more!!!