Linux system limits

It is better to be boldly decisive and risk being wrong than to agnonize at length and be right too late .-------- rather boldly, decisively and risk making mistakes, rather hesitant, too late to correct

Linux each resource has an associated soft and hard limit, such as the number of child processes can create more than a single user has a limit, also a multi-process open file descriptors can also have a corresponding limit values, these restrictions will limit the server can provide the number of clients concurrent access.

1, how to modify the maximum number of file handles

linux default maximum number of file handles is 1024, in the linux server file is larger than the concurrent case, the system will report "too many open files" error.
Therefore, when the high concurrency linux server tuning often requires pre-tuning parameter Linux, Linux modify the maximum file handles.

method one

  1. ulimit -n <number of files that can be opened simultaneously>, the maximum number of handles for the current process of modifying the parameters specified
  • : This method is only valid for the current process, re-open or re-open a shell of a process, or the value of the parameter before

First with ulimit -a Linux-related query parameters, as follows:

Here Insert Picture Description

  • Which, open files is the maximum number of file handles, the default is 1024.
  • Modify Linux maximum number of file handles: ulimit -n 2048, will be modified to handle the maximum number of 2048.

Here Insert Picture Description

Method Two

All processes are effective ways to modify Linux system parameters

life /etc/security/limits.conf
添加
* soft nofile 65536
* hard nofile 65536

After the maximum number of handles 65536 instead save the changes, log off the current user, log in again, modified parameters will take effect

2, restriction modification system API function

In the Linux system, we can use the following two functions

#include <sys/resource.h>
int getrlimit(int resource, struct rlimit *rlim);      //获取系统限制信息
int setrlimit(int resource, const struct rlimit *rlim);  //修改系统限制

Set the number of open file descriptors (RLIMIT_NOFILE), create the number (RLIMIT_NPROC) process and other restrictions

  • Returns 0 on success, failure to return a non 0
  • 参数说明:
  • Parameter Description resource :

Large virtual memory space RLIMIT_AS // process, in bytes.
RLIMIT_CORE // great length the kernel dump file.
RLIMIT_CPU // permissible use of CPU time in seconds. When the process reaches the soft limit, the kernel will send SIGXCPU signal, the default behavior of this signal is to terminate the process of execution.
RLIMIT_DATA // large value of the process data segment.
Great length RLIMIT_FSIZE // process can create a file. If the process tries beyond this limit, the core will send SIGXFSZ signal, by default, will terminate the implementation process.
RLIMIT_LOCKS // process can lock and the establishment of a large value of the lease.
RLIMIT_MEMLOCK // process large amounts of data can be locked in memory, in bytes.
RLIMIT_MSGQUEUE // process a large number of bytes may be POSIX message queues assigned.
RLIMIT_NICE // process can be called perfect big values set by setpriority () or nice ().
RLIMIT_NOFILE // specified ratio process can open large files describing the value of the word freshman, beyond this value will be generated EMFILE error.
RLIMIT_NPROC // large number of processes a user can have.

RLIMIT_RTPRIO // process by large real-time priority sched_setscheduler and sched_setparam settings. RLIMIT_SIGPENDING // large number of pending signal user can have.
RLIMIT_STACK // large process stack, in bytes.

  • rlim: soft and hard limiting description of a resource structure
struct rlimit
{  
rlim_t rlim_cur;     //指定当前系统软限制
rlim_t rlim_max;    //指定的资源指定当前系统的硬限制
};    
  • 需要说明:软限制值必须小于硬限制值

Sample code is given below:

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/resource.h> 

//下面定义一个函数用来打印系统限制
void print_limits(char* name, int resource)
{   
    struct rlimit limit; 
     if(getrlimit(resource, &limit) < 0) 
     {
        printf("getrlimit  failure: %s\n", strerror(errno));             
        return ;      
     } 
    
    
    printf("%-15s    ",name);     

//判断软限制是否无限
    if(limit.rlim_cur == RLIM_INFINITY)       
    {         
        printf("    (infinite)      ");         
    }      
    else   
    {       
        printf("    %-15ld      ",limit.rlim_cur);     
    }
      
//判断硬限制是否无限
    if(limit.rlim_max == RLIM_INFINITY)    
    {       
        printf("    (infinite)     ");      
    }else     
    {        
        printf("    %-15ld  ",limit.rlim_max);    
    }     
    printf("\n"); 
}  


//主函数
int main(void)
{  
    struct rlimit limit = {0};
     
    printf("name                softlimit                hardlimit\n");
    
    print_limits("RLIMIT_NPROC", RLIMIT_NPROC);      //打印进程限制
    print_limits("RLIMIT_DATA", RLIMIT_DATA);    	//打印进程数据段的大值
    print_limits("RLIMIT_STACK", RLIMIT_STACK);     //打印大的进程堆栈
    print_limits("RLIMIT_NOFILE", RLIMIT_NOFILE);    //打印文件限制
     
    printf("\nAfter set RLIMIT_NOFILE:\n");       
    getrlimit(RLIMIT_NOFILE, &limit );
    
    limit.rlim_cur  = limit.rlim_max;    
    setrlimit(RLIMIT_NOFILE, &limit );
     
    print_limits("RLIMIT_NOFILE", RLIMIT_NOFILE);
     
    return 0;

}

Here Insert Picture Description

Published 29 original articles · won praise 65 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_46027505/article/details/105119108