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
The signatures of the system calls are
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.
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).
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
- Drawback of Resource Preemption
- Resource Allocation Techniques for Processes
- Resource Allocation Graph (RAG) in Operating System
- Cross Origin Resource Sharing (CORS)
- Resource Reservation Protocol in Real-time Systems
- Create n-child process from same parent process using fork() in C
- Process Table and Process Control Block (PCB)
- Pass the value from child process to parent process
- Process states and Transitions in a UNIX Process
- Process Scheduler : Job and Process Status
- Process Synchronization | Set 2
- Formation Of Process from Program
- Difference between Process and Thread
- Communication between two process using signals in C
- Inter Process Communication (IPC)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.