Table of contents
1.
Introduction
2.
getrlimit()
3.
setrlimit()
4.
C program 
5.
Frequently Asked Questions
5.1.
What is getrlimit?
5.2.
What is Rlim_t?
5.3.
What is the use of the setrlimit function?
6.
Conclusion
Last Updated: Mar 27, 2024

Get/Set Process Resource Limits In C

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Operating Systems

Introduction

As system resources are limited, each process is constrained by limitations imposed by the operating system. The “ulimit” command (a built-in command included in the Bourne shell [/bin/sh]) on the command line allows the user to view and adjust current system restrictions available to the shell and the processes it starts.

The C shell (/bin/csh) has a built-in command named limit that is similar.

The command “ulimit -Ha” displays the system's hard limits. Only the superuser can raise the rigid restrictions. 

Each resource has a soft and hard limit associated with it.

  • Soft limit: The kernel's actual limit for the related resource is the soft limit.
  • Hard limit: The soft limit has a hard limit that acts as a ceiling. 


Range of Soft limit: Between 0 and the hard limit.

The following structure defines the two limits:

struct rlimit {
    rlim_t rlim_cur; /* Soft limit */
    rlim_t rlim_max; /* Hard limit */
};
You can also try this code with Online C Compiler
Run Code

The signatures of system calls are as follows:

int getrlimit(int resource, struct rlimit *rlim);
int setrlimit(int resource, const struct rlimit *rlim);
You can also try this code with Online C Compiler
Run Code


Also see: Multiprogramming vs Multitasking, Open Source Operating System.

getrlimit()

The getrlimit() function returns the calling process's resource limits. A resource limit comprises two values: one that specifies the current (soft) limit and the other that specifies the maximum (hard) limit.

The RLIM_INFINITY value defined in sys/resource.h is more significant than any other limit value. If getrlimit() returns RLIM_INFINITY for a resource, it signifies that the implementation does not impose restrictions on it.

The resource argument indicates the resource for which hard and soft limits should be obtained. and maybe one of the following values:

  • RLIMIT_CORE

The maximum size of a memory dump allowed for the process (in bytes). 

  • RLIMIT_CPU

The maximum amount of CPU time (in seconds) that the process is permitted.

  • RLIMIT_DATA

In bytes, the maximum size of the break value for the process. This resource has a complex and soft limit value of RLIM_INFINITY in this implementation.

  • RLIMIT_FSIZE

The maximum file size (in bytes) that the process can handle.

  • RLIMIT_MEMLIMIT

The maximum number of usable storage above the 2-gigabyte bar can be allocated (in 1-megabyte chunks).

  • RLIMIT_NOFILE

The most significant number of open file descriptors that the process is authorized to have.

  • RLIMIT_STACK

The largest size of a process's stack in bytes.

  • RLIMIT_AS

The process's maximum address space size, in bytes.

The rlimit structure is referenced by the rlp parameter. The members of this structure are as follows:

rlim_cur: The current (soft) limit

rlim_max: The maximum (hard) limit

setrlimit()

The setrlimit() function restricts the number of resources available to the caller process. A resource limit comprises two values: one that specifies the current (soft) limit and the other that specifies the maximum (hard) limit.

The soft limit can be modified to any value between the hard and soft limitations. The soft limit cannot be adjusted lower than the current utilization for specific resource settings (RLIMIT CPU, RLIMIT NOFILE, RLIMIT AS).

Only a process with superuser authority can increase the hard limit. A single call to setrlimit can adjust both the soft and hard limits (). 

C program 

C program to demonstrate the 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;
	// getting 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));

	// setting new value
	lim.rlim_cur = 3;
	lim.rlim_max = 1024;
	// setting limits
	if(setrlimit(RLIMIT_NOFILE, &lim) == -1)
	fprintf(stderr, "%s\n", strerror(errno));

	// getting 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;
}
You can also try this code with Online C Compiler
Run Code

Output:

Old limits -> soft limit= 1048576 hard limit= 1048576 
New limits -> soft limit= 3 hard limit= 1024

 

You can also read about layered structure of operating system.

Frequently Asked Questions

What is getrlimit?

The resource limit for the provided resource is returned by the getrlimit() function. A resource limit is a technique for the operating system to impose a limit on a process's use of various resources. 

What is Rlim_t?

Rlim_t is an unsigned integer type used for limit values.

What is the use of the setrlimit function?

It sets the resource limit for the provided resource. A resource limit is a technique for the operating system to limit a process's use of various resources.

Conclusion

In this blog, we have learned that each resource has a soft and hard limit associated with it. The soft limit is the kernel's actual limit for the related resource and the hard limit acts as a ceiling. We learned how the getrlimit() and setrlimit() system calls could be used to get and set the resource limits such as files, CPU, memory, etc., associated with a process. The getrlimit() function returns the calling process's resource limits. The setrlimit() function restricts the number of resources available to the caller process.

Recommended Readings: 


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass