(This article from: http://www.cnblogs.com/wangkangluo1/archive/2012/06/06/2537677.html)
Linux under the default is not to produce core files, to use Ulimit-c unlimited release
Overview
System performance has always been a topic of concern, how to achieve the most effective tuning through the simplest settings, how to guarantee the operation of the program under the condition of limited resources, Ulimit is a simple means that we often use when dealing with these problems. Ulimit is an internal key feature of a Linux system that has a set of parameter sets that restrict the use of resources for the shell process and its child processes generated by it. This article will detail the functionality, use, and impact of Ulimit in a later section, and illustrate in detail how it affects the use of resources in specific cases.
function and usage of ulimit
Ulimit function Brief
Suppose there is a situation, when a Linux host on the same time 10 people, in the system resources unrestricted, the 10 users open 500 documents at the same time, assuming that each document size 10M, then the system's memory resources will be a huge challenge.
The actual application of the environment is more complex than this hypothesis, for example, in an embedded development environment, all aspects of resources are very scarce, for the number of open file descriptors, allocation stack size, CPU time, virtual memory size, and so on, have very strict requirements. The reasonable limitation and allocation of resources is not only the necessary condition to ensure the availability of the system, but also is closely related to the performance of the software running on the system. At this point, ulimit can play a big role, it is a simple and effective way to implement resource constraints.
Ulimit is used to limit the resources that the shell startup process consumes, and supports the following types of restrictions: the size of the kernel file created, the size of the process data block, the size of the shell process creation file, the size of the memory lock, the size of the resident memory set, the number of open file descriptors, The maximum size of the allocation stack, the CPU time, the maximum number of threads for a single user, and the maximum virtual memory that the Shell process can use. At the same time, it supports the constraints of hard and soft resources.
As a temporary limitation, ulimit can be used for shell sessions that are logged on by using its command, ending the limit at the end of the session and not affecting other shell sessions. For long-term fixed limits, the Ulimit command statement can also be added to a file read by the login shell, acting on a specific shell user.
Figure 1: Use of Ulimit
In the following sections, you will learn more about using Ulimit to make the appropriate resource restrictions.
How to use Ulimit
Ulimit manages different kinds of system resources with some parameter options. In this section, we will explain the use of these parameters.
The format of the Ulimit command is: ulimit [options] [limit]
You can refer to the following table for specific options meanings and simple examples.
Table 1. Ulimit parameter Description
options [Options] |
Meaning |
Example |
-H |
Set the hard resource limit once the setting cannot be increased. |
ULIMIT–HS 64; Limit hard resources, thread stack size is 64K. |
-S |
Setting a soft resource limit can be increased after setup, but cannot exceed hard resource settings. |
ULIMIT–SN 32; Restrict soft resources, 32 file descriptors. |
-A |
Displays all current limit information. |
Ulimit–a; Displays all current limit information. |
-C |
The size of the largest core file, in blocks. |
Ulimit–c Unlimited; the size of the resulting core file is not limited. |
-D |
The size of the largest data segment of the process, in Kbytes. |
Ulimit-d Unlimited; The data segment size of the process is not limited. |
-F |
The process can create a maximum value for the file, in blocks. |
ulimit–f 2048; Limit the maximum file size that a process can create to 2048 blocks. |
-L |
Maximum lockable memory size, in Kbytes. |
Ulimit–l 32; Limit maximum lockable memory size to Kbytes. |
-M |
The maximum memory size, in Kbytes. |
Ulimit–m Unlimited; No limit on maximum memory. |
-N |
The maximum number of file descriptors can be opened. |
Ulimit–n 128; Limit the maximum number of 128 file descriptors that can be used. |
-P |
The size of the pipe buffer, in Kbytes. |
Ulimit–p 512; Limit the size of the pipe buffer to Kbytes. |
-S |
The thread stack size, in Kbytes. |
Ulimit–s 512; The size of the limit line stacks is Kbytes. |
-T |
Maximum CPU elapsed time, in seconds. |
Ulimit–t Unlimited; There is no limit to the maximum CPU occupancy time. |
-U |
The maximum number of processes available to the user. |
Ulimit–u 64; Limit the maximum number of users to use 64 processes. |
-V |
The maximum available virtual memory for the process, in Kbytes. |
Ulimit–v 200000; Limit the maximum available virtual memory to 200000 Kbytes. |
We can use the Ulimit in several ways:
- In the user's startup script
If the user is using bash, you can include Ulimit–u 64 in the. bashrc file in the user's directory to limit the maximum number of 64 processes that users can use. In addition, you can add ULIMT to the startup script that is equivalent to the. BASHRC feature.
- In the application's startup script
If the user wants to limit the MyApp of an application, a simple script Startmyapp can be written.
As soon as you start the application through script Startmyapp, you can limit the thread stack size of the application MyApp to 512K.
- Input directly from the console
The buffer for the throttling pipeline is 256K.
Valid range of user processes
Ulimit, as a kind of work restricting the use of resources, has its scope. So, does it restrict the object to a single user, a single process, or the entire system? In fact, Ulimit restricts the current shell process and its derived child processes. For example, if a user runs two shell terminal processes at the same time and only executes ulimit–s 100 in one of the environments, the size of the file created in the shell process receives the appropriate restrictions, while the other shell terminal, including the subroutine on which it runs, is unaffected:
Shell Process 1
Shell Process 2
So, is there a way to limit the resources for a specific user? The answer is yes, by modifying the system's/etc/security/limits configuration file. This file not only restricts the use of resources for the specified user, but also restricts the use of resources for the specified group. Each line of the file is a description of the qualification, in the following format:
Domain represents the name of a user or group, and you can use * as a wildcard character. The Type can have two values, soft and hard. Item represents a resource that needs to be qualified and can have many candidate values, such as stack,cpu,nofile and so on, representing the maximum stack size, CPU time consumed, and number of open files. By adding a corresponding line description, you can create a corresponding restriction. For example:
The line configuration statement limits the maximum number of files that any user can create to 100.
Now the process and the user can be the resource constraints, seemingly enough, it is not. Many applications need to make a general limit to the resource usage of the entire system, and we need to modify the configuration file under/proc. The/proc directory contains many parameters for the current state of the system, such as/proc/sys/kernel/pid_max,/proc/sys/net/ipv4/ip_local_port_range, etc. The name of the file can be roughly guessed by the type of resource being restricted. Because of the large number of documents involved in this directory, not described here. Interested readers can open the relevant documents to read the instructions.
Examples of ulimit management system resources
Ulimit provides the ability to restrict system resources in a shell process. This chapter lists some examples of using Ulimit to restrict user processes, detailing these restrictive behaviors and their corresponding effects to illustrate how ulimit restricts system resources to achieve the ability to regulate system performance.
Using Ulimit to limit the shell's memory usage
This section shows readers how to use the –d,–m and –V options to limit the memory used by the shell.
First, let's look at the case where the LS command is called when the ulimit limit is not set:
Figure 2: ls command usage when ulimit is not set
You can see that the LS command is working properly at this time. Set Ulimit below:
Let's review the meanings of the three options described in the previous section:
-D: Sets the maximum value of the data segment. Units: KB.
-M: Sets the maximum number of resident memory that can be used. Units: KB.
-V: Sets the maximum value for virtual memory. Units: KB.
With the Ulimit setting above, we have limited the maximum memory available to the current shell to below 1000KB. Let's see what happens when we run the LS command:
As you can see from the above results, the LS operation fails at this time. According to the error message given by the system, we can see that there is an error in LS caused by memory allocation failure when calling LIBC library. So let's take a look at how big this libc library file is:
Figure 3. View libc File Size
As can be seen from the above information, the size of this libc library file is 1.5MB. And we use Ulimit to set the memory usage limit is 1000KB, less than 1.5MB, which also fully proves the ulimit to limit the use of shell memory functions.
Use Ulimit to limit the size of files created by the shell
Next, show the reader how to use the-f option to limit the size of the file that the shell can create.
First, let's look at the situation when Ulimit-f is not set:
Figure 4. Viewing a file
The existing file testfile size is 323669 bytes, now use the Cat command to create a copy of testfile:
Figure 5 Creating a replica when Ulimit is not set
As can be seen from the above output, we have successfully created a copy of the Testfile newFile.
Below we set ULIMT–F 100:
The-f option means to set the maximum number of files that the shell can create. The unit is blocks.
Now let's do the same copy command once to see what it will look like:
Figure 6: Create replica when setting Ulimit
This time the creation of the copy of Testfile failed, the system gave an error message when the file size exceeded the limit. The default size of the next block on the Linux system is bytes. So the meaning of the above ulimit is to limit the number of files that the shell can create to a maximum of x = 51200 bytes, less than 323669 bytes, so the creation of the file fails to meet our expectations. This example shows how to use Ulimit to control the maximum file that the shell can create.
Use Ulimit to limit the number of sockets that a program can create
Consider a real-world requirement. For a server program in a C/S model, it responds to multiple client program requests to create multiple socket ports. If there is a large number of clients making requests to the server at the same time, then server will need to create a large number of socket connections. However, in a system, it is often necessary to limit the maximum number of sockets that a single server program can use for use by other server programs. So how do we do that? The answer is that we can make it through Ulimit! The attentive reader may find that the ULIMIT option for the socket does not appear to be limited by the introduction of the previous chapters. Yes, Ulimit does not have an option to limit the number of sockets directly. However, we have the-n option, which is used to limit the maximum number of file descriptors that a process can open. Under Linux All resources are files, ordinary files are files, disk printers are files, sockets of course also files. Creating a new socket connection under Linux is essentially creating a new file descriptor. As shown (see the file descriptor information currently open by a process):
Figure 7. View process Open File descriptor
Therefore, we can limit the number of socket creation by using Ulimit–n to limit the maximum number of file descriptors that the program can open.
Use Ulimit to limit the size of the shell multithreaded stack (increase the number of available threads)
In the last example, we show you how to use-s (in kilobytes) to limit the stack size of a thread, thereby reducing the memory usage of the entire multithreaded application and increasing the number of threads available. This example is taken from a real case. The problem we are experiencing is that the system has the following limitations on our multi-threaded software:
Ulimit-v 200000
As described earlier in this article, this means that our programs can use up to 200MB of virtual memory. Since our program is a multithreaded program, the program will create new threads as needed at runtime, which will inevitably increase the overall memory requirement. At first we had a 1024 limit on the stack size (using the 1232来 instructions in this example):
When our program starts, through PMAP to see its memory usage, we can see a number of data segments that occupy 1232KB, which is the stack used by the thread created by the program:
Figure 8. The stack used by the program thread
Whenever a new thread is created, it needs a new allocation of 1232KB of memory space, and our total virtual memory limit is 200MB, so if we need to create more threads, one way to improve is to reduce the fixed stack size per thread, which can be done by ulimit–s To achieve:
We set the stack size to 512KB, and then pmap to see if our settings worked:
Figure 9: Stack size after setting Ulimit
As can be seen from the above information, we have successfully changed the stack size of the thread to 512KB, so that in the case of the total memory usage limit, we can increase the number of threads that can be created by using the method described in this section to improve the multithreaded performance of the program.
Summarize
In summary, the ulimit instruction in Linux system provides a convenient way for resource limitation and system performance optimization. From the user's shell startup script, the application startup script, and directly in the console, you can restrict the use of system resources, including the size of the kernel file created, the size of the process data block, the size of the shell process creation file, the size of the memory lock, the size of the resident memory set, The number of open file descriptors, the maximum size of the allocation stack, the CPU time, the maximum number of threads for a single user, the maximum virtual memory that the Shell process can use, and so on. The examples in this article are very intuitive to illustrate the use of ulimit and its effects, and it is clear that ulimit is very useful for our application and development work on the Linux platform.