Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
Get/Set process resource limits in C
Next article icon

Get/Set process resource limits in C

Last Updated : 17 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
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

Next Article
Get/Set process resource limits in C

S

Siddharth Nayak
Improve
Article Tags :
  • Operating Systems
  • Technical Scripter 2018

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
    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
    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
    Process Creation and Deletions in Operating Systems
    A process is an instance of a program running, and its lifecycle includes various stages such as creation, execution, and deletion. The operating system handles process creation by allocating necessary resources and assigning each process a unique identifier. Process deletion involves releasing reso
    6 min read
    Process Table and Process Control Block (PCB)
    While creating a process, the operating system performs several operations. To identify the processes, it assigns a process identification number (PID) to each process. As the operating system supports multi-programming, it needs to keep track of all the processes. For this task, the process control
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences