SoFunction
Updated on 2025-04-07

A detailed explanation of file descriptors and process limits in Linux system

1. Basic concepts of file descriptors and process counts

File descriptor

File Descriptor (FD) is an abstract identifier used in Linux systems to access files or other I/O resources (such as pipelines, sockets, etc.). Each process has a file descriptor table that records files or I/O resources opened by the process.

Number of processes

A process is an instance of a running program in a Linux system. Each process has a unique process ID (PID). The maximum number of processes allowed by the system is kernel parameterskernel.pid_maxDecide, and the number of processes each user can create is subject toulimitlimit.

2. Limitations and adjustments of file descriptors

View current file descriptor restrictions

In Linux systems, you can view the current file descriptor limit by using the following command:

ulimit -n

Output example:

1024
  • explainulimit -nDisplays the maximum number of file descriptors that the current user can open.

Adjust file descriptor limits

If you need to temporarily adjust the file descriptor limit, you can use the following command:

ulimit -n 1048576
  • explain: Set the file descriptor limit for the current user to 1,048,576.

Permanently modify file descriptor restrictions

In order for the file descriptor limit to remain valid after system restart, editing is required/etc/security/File, add the following content:

* soft nofile 1048576
* hard nofile 1048576
  • explainsoftIndicates soft limit,hardIndicates hard limit.nofileIndicates the limitation of file descriptors.

Then edit/etc//common-sessionand/etc//common-session-noninteractiveFile, add the following content:

session required pam_limits.so

Finally, log in or restart the system to make the configuration take effect.

3. Limits and adjustments to the number of processes

Check the maximum number of processes at the system level

The maximum number of processes at the system level is based on kernel parameterskernel.pid_maxDecide. You can view it through the following command:

sysctl kernel.pid_max

Or read the kernel parameter file directly:

cat /proc/sys/kernel/pid_max

Output example:

32768
  • explainkernel.pid_maxIndicates the maximum number of process IDs allowed by the system.

Check the maximum number of processes at the user level

The number of processes that each user can create is subject toulimitlimit. You can view the current user's restrictions through the following command:

ulimit -u

Output example:

63293
  • explainulimit -uDisplays the maximum number of processes that the current user can create.

Check the current system's number of processes

If you want to see the number of processes that have been running in the current system, you can use the following command:

ps -eLf | wc -l

or:

ps aux | wc -l
  • explain: These commands will count the number of all processes in the current system (including threads).

View the total number of processes in the system

The total number of processes in the system is also subject to kernel parameters.-max, which represents the maximum number of threads allowed by the system. You can view it through the following command:

sysctl -max

or:

cat /proc/sys/kernel/threads-max

Output example:

126022
  • explain-maxis the maximum number of threads allowed by the system, usually more thankernel.pid_maxMuch bigger.

Permanently modify the maximum number of processes

If you need to modify the maximum number of processes at the system level, you can edit/etc/File, add or modify the following lines:

kernel.pid_max = 65536

Then run the following command to make the configuration take effect:

sysctl -p

4. Practical application scenarios and precautions

High load server

In high-load server environments, such as web servers, database servers, etc., it may be necessary to process a large number of files and connections simultaneously. Therefore, it is very important to reasonably adjust file descriptors and process limits.

System resources

Increasing file descriptors and process limits will occupy more system resources (such as memory), so it needs to be set reasonably based on the actual resource situation of the system.

User Limitations

In addition to system-level restrictions, each user also has restrictions on file descriptors and number of processes (byulimitView and set). Even if the system-level restrictions are set large, a single user may be subject toulimitLimitation.

5. Summary

In Linux systems, file descriptors and process count are two very important system resources. By reasonably adjusting the limitations of these resources, the performance and stability of the system can be significantly improved. This article details how to view and adjust file descriptors and process limits, and provides practical application scenarios and precautions. I hope these contents can help system administrators and developers better optimize system configuration.

Summary of key commands

  • File descriptor
    • View the current limit:ulimit -n
    • Temporary adjustment restrictions:ulimit -n 1048576
    • Permanent modification restrictions: Edit/etc/security/
  • Number of processes
    • View system level restrictions:sysctl kernel.pid_max
    • View user level restrictions:ulimit -u
    • Check the current number of processes:ps -eLf | wc -l
    • Permanent modification restrictions: Edit/etc/

By mastering these commands and configuration methods, you can better manage and optimize the resources of the Linux system to ensure that the system still operates stably under high load environments.

The above is a detailed explanation of the file descriptor and process limit in the Linux system. For more information about Linux file descriptor and process limit, please pay attention to my other related articles!