Get/Set process resource limits in C
Last Updated :
17 Dec, 2018
The
getrlimit() and
setrlimit() system calls can be used to get and set the resource limits such as files, CPU, memory etc. associated with a process.
Each resource has an associated soft and hard limit.
- soft limit: The soft limit is the actual limit enforced by the kernel for the corresponding resource.
- hard limit: The hard limit acts as a ceiling for the soft limit.
The soft limit ranges in between 0 and hard limit.
The two limits are defined by the following structure
C
struct rlimit {
rlim_t rlim_cur; /* Soft limit */
rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */
};
The signatures of the system calls are
C
int getrlimit(int resource, struct rlimit *rlim);
int setrlimit(int resource, const struct rlimit *rlim);
resource refers to the resource limits you want to retrieve or modify.
To
set both the limits,
set the values with the new values to the elements of rlimit structure.
To
get both the limits,
pass the address of rlim. Successful call to getrlimit(), sets the rlimit elements to the limits.
On
success, both return
0. On
error,
-1 is returned, and errno is set appropriately.
Here, is a program demonstrating the system calls by changing the value one greater than the maximum file descriptor number to 3.
C
// C program to demonstrate working of getrlimit()
// and setlimit()
#include <stdio.h>
#include <sys/resource.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
struct rlimit old_lim, lim, new_lim;
// Get old limits
if( getrlimit(RLIMIT_NOFILE, &old_lim) == 0)
printf("Old limits -> soft limit= %ld \t"
" hard limit= %ld \n", old_lim.rlim_cur,
old_lim.rlim_max);
else
fprintf(stderr, "%s\n", strerror(errno));
// Set new value
lim.rlim_cur = 3;
lim.rlim_max = 1024;
// Set limits
if(setrlimit(RLIMIT_NOFILE, &lim) == -1)
fprintf(stderr, "%s\n", strerror(errno));
// Get new limits
if( getrlimit(RLIMIT_NOFILE, &new_lim) == 0)
printf("New limits -> soft limit= %ld "
"\t hard limit= %ld \n", new_lim.rlim_cur,
new_lim.rlim_max);
else
fprintf(stderr, "%s\n", strerror(errno));
return 0;
}
Output:
Old limits -> soft limit= 1048576 hard limit= 1048576
New limits -> soft limit= 3 hard limit= 1024
The Old limits values may vary depending upon the system.
Now, If you try to open a new file, it will show run time error, because maximum 3 files can be opened and that are already being opened by the system(STDIN, STDOUT, STDERR).
C
// C program to demonstrate error when a
// process tries to access resources beyond
// limit.
#include <stdio.h>
#include <sys/resource.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
struct rlimit old_lim, lim, new_lim;
// Get old limits
if( getrlimit(RLIMIT_NOFILE, &old_lim) == 0)
printf("Old limits -> soft limit= %ld \t"
" hard limit= %ld \n", old_lim.rlim_cur,
old_lim.rlim_max);
else
fprintf(stderr, "%s\n", strerror(errno));
// Set new value
lim.rlim_cur = 3;
lim.rlim_max = 1024;
// Set limits
if(setrlimit(RLIMIT_NOFILE, &lim) == -1)
fprintf(stderr, "%s\n", strerror(errno));
// Get new limits
if( getrlimit(RLIMIT_NOFILE, &new_lim) == 0)
printf("New limits -> soft limit= %ld \t"
" hard limit= %ld \n", new_lim.rlim_cur,
new_lim.rlim_max);
else
fprintf(stderr, "%s\n", strerror(errno));
// Try to open a new file
if(open("foo.txt", O_WRONLY | O_CREAT, 0) == -1)
fprintf(stderr, "%s\n", strerror(errno));
else
printf("Opened successfully\n");
return 0;
}
Output:
Old limits -> soft limit= 1048576 hard limit= 1048576
New limits -> soft limit= 3 hard limit= 1024
Too many open files
There is another system call
prlimit() that combines both the system calls.
For more details, check manual by typing
man 2 prlimit
Similar Reads
Resource Allocation Techniques for Processes The Operating System allocates resources when a program need them. When the program terminates, the resources are de-allocated, and allocated to other programs that need them. Now the question is, what strategy does the operating system use to allocate these resources to user programs? There are two
2 min read
Operations on Processes Process operations refer to the actions or activities performed on processes in an operating system. These operations include creating, terminating, suspending, resuming, and communicating between processes. Operations on processes are crucial for managing and controlling the execution of programs i
5 min read
Process Schedulers in Operating System A process is the instance of a computer program in execution. Scheduling is important in operating systems with multiprogramming as multiple processes might be eligible for running at a time.One of the key responsibilities of an Operating System (OS) is to decide which programs will execute on the C
7 min read
Different types of Processes in Process Table The process table is a data structure used by the operating system to keep track of all processes. It is the collection of Program control Blocks (PCBs) which contains information about each process, such as its ID (PID), current state (e.g., running, ready, waiting), CPU usage, memory allocation, a
4 min read
Process in Operating System A process is a program in execution. For example, when we write a program in C or C++ and compile it, the compiler creates binary code. The original code and binary code are both programs. When we actually run the binary code, it becomes a process. A process is an 'active' entity instead of a progra
3 min read
Resource Management in Operating System Resource Management in Operating System is the process to manage all  the resources efficiently like CPU, memory, input/output devices, and other hardware resources among the various programs and processes running in the computer. Resource management is an important thing because resources of a comp
3 min read
5 State Process Model in Operating System In an operating system (OS), managing how programs run and interact with system resources is crucial for efficient performance. The 5-state process model is a fundamental framework used by OSs to categorize and control the behavior of processes, which are individual instances of programs running on
6 min read
Resource Allocation Graph (RAG) in Operating System A Resource Allocation Graph (RAG) is a visual way to understand how resources are assigned in an operating system. Instead of using only tables to show which resources are allocated, requested, or available, the RAG uses nodes and edges to clearly illustrate relationships between processes and their
7 min read
Inter-process Communication using a shared stack Inter Process Communication through shared memory is a concept where two or more process can access the common memory.the communication is done via this shared memory where changes made by one process can be viewed by another process. So, we can use one stack as a shared memory, where users(processe
8 min read
Process Control Block in OS A Process Control Block (PCB) is used by the operating system to manage information about a process. The process control keeps track of crucial data needed to manage processes efficiently. A process control block (PCB) contains information about the process, i.e. registers, quantum, priority, etc. T
7 min read