5. Process Resource Limitations
(1) Getrlimit, Setrlimit function
Header file |
#include <sys/resource.h> |
Function |
int* getrlimit (int resource, struct rlimit* rlptr); |
return value |
Successfully returned 0, error returned non 0 |
Function |
Access to resource limits, stored in rlptr-pointed structures |
Parameters |
(1) Rlimit structural body struct rlimit{ rlim_t rlim_cur; // Soft limit: current limit rlim_t Rlim_max; // Hard limit: Maximum value of Rlim_cur } (2) The value of resource: see the following table. |
|
Function |
int setrlimit (int resource, const struct rlimit* rlptr); |
return value |
Successfully returned 0, error returned non 0 |
Function |
Modify the resource limits specified by the resource by RLPTR the structure to which it is directed |
Note |
Resource constraints affect the calling process and are inherited by its child processes. |
(2) Resource value
Macro |
Role |
Rlimit_as |
Process Available Storage size |
Rlimit_core |
Maximum number of bytes in the core file |
Rlimit_cpu |
CPU Time Max |
Rlimit_data |
Maximum length of data segment |
Rlimit_fsize |
The maximum length of the file that can be created. When this soft limit is exceeded, a sigxfsz signal is sent to the process |
Rlimit_locks |
Maximum number of file locks |
Rlimit_memlock |
Maximum number of bytes that can be locked in memory using Mlock |
Rlimit_nofile |
Maximum number of files that can be opened |
Rlimit_nproc |
The maximum number of child processes that each user ID can have |
Rlimit_rss |
Byte length of the maximum resident memory set |
Rlimit_stact |
Maximum length of stack |
"Programming Experiment" shows process resource limitations
Process_limit.c
#include <sys/resource.h>#include<stdio.h>#include<stdlib.h>#defineRlimit (name) get_limit (#name, name);voidGet_limit (Char* Name,intResource) { structRlimit limit; if(Getrlimit (Resource, &limit) <0) {perror ("Getrlimit Error"); return; } printf ("%-15s", name);//left aligns the name of the print resource//rlmit_cur: Soft Resource limits if(Limit.rlim_cur = =rlim_infinity) {printf ("Infinity");//Unlimited}Else{printf ("%10ld", Limit.rlim_cur); } //Rlimit_max: Hard Resource limits if(Limit.rlim_max = =rlim_infinity) {printf ("Infinity");//Unlimited}Else{printf ("%10ld", Limit.rlim_max); } printf ("\ n");}intMainintargcChar*argv[]) {Rlimit (rlimit_as); //process available Storage sizeRlimit (Rlimit_core);//maximum number of bytes in the core fileRlimit (RLIMIT_CPU);//CPU Time MaxRlimit (Rlimit_data);//Maximum length of data segmentRlimit (rlimit_fsize);//maximum length of file that can be createdRlimit (rlimit_locks);//maximum number of file locksRlimit (Rlimit_memlock);//maximum number of bytes that can be locked in memory using MlockRlimit (Rlimit_nofile);//maximum number of files that can be openedRlimit (RLIMIT_NPROC);//the maximum number of child processes that each user ID can haveRlimit (RLIMIT_RSS);//byte length of the maximum resident memory setRlimit (Rlimit_stack);//maximum length of stack return 0;}
(3) configuration file for process resource limit
①/etc/security/limits.conf
Initialization of process resources in ②linux is established by process No. 0 and inherited by subsequent processes .
(4) modification rules for resource limitations
① Hard resource limit must be greater than or equal to soft resource limit
② any process can reduce or elevate its soft resource limit , but must be less than or equal to the hard resource limit .
③ any process can reduce its hard resource limit, but must be greater than its soft limit, ordinary households do not reversible this operation.
④ only super users can increase the hard limit .
"Programming Experiment" limits the maximum number of open files for a process
Limit_nofile.c
#include <unistd.h>#include<sys/resource.h>#include<stdio.h>#include<stdlib.h>#include<fcntl.h>#defineMODE S_irwxu | S_irwxg | S_irwxointOpen_file (Const Char*filename) { intFD = open (filename, O_RDWR | O_creat |O_trunc, MODE); if(FD <0) {perror ("Open Error"); Exit (1); } returnFD;}intMainintargcChar*argv[]) { structRlimit limit; Limit.rlim_cur=4;//set soft resource limitsLimit.rlim_max =1024x768;//set hard resource limits//set the maximum number of open files for a process//(note:0==> standard input 1== standard output 2==> standard error has been opened) if(Setrlimit (Rlimit_nofile, &limit) <0) {perror ("Setrlimit Error"); Exit (1); } //Open File intfd1, FD2; FD1= Open_file ("S1.txt");//Open normally if(Fd1 <0) {exit (1); } fd2= Open_file ("S2.txt");//if the 4 files specified by the soft resource limit are exceeded, an error is given. if(Fd2 <0) {exit (1); } close (FD1); Close (FD2); return 0;}
5th. Process Environment (4) _ Process resource limit