0% found this document useful (0 votes)
65 views6 pages

Os Program5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views6 pages

Os Program5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Program-5

AIM: Write a program to print the system resource limits. Use


getrlimit system call.

Theory:
Get/Set process resource limits in C
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

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


int getrlimit(int resource, struct rlimit *rlim);
int setrlimit(int resource, const struct rlimit *rlim);
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

 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 program to demonstrate working of getrlimit()
// and setrlimit()
#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= 1024 hard limit= 1048576


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

The Old limits values may vary depending upon the system.

Another Example for verification of getrlimit() system call


implementation
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 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= 1024 hard limit= 1048576

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

Too many open files

You might also like