How to view the number of threads in Linux and error exceeding the maximum system thread value
- By/proc pseudo file system
Description
The/proc directory on a Linux system is a file system, the proc file system. Unlike other common file systems,/proc is a pseudo-file system (also known as a virtual file system) that stores a series of special files that are currently running on the kernel, which users can use to view information about system hardware and the processes currently running. You can even change the running state of the kernel by changing some of these files.
Based on the particularity of the/proc file system as described above, its files are often referred to as virtual files and have some unique features. For example, some of these files will return a lot of information when viewed using the view command, but the size of the file itself will be displayed as 0 bytes. In addition, the time and date properties of most files in these special files are usually the current system time and date, which is related to their being refreshed (stored in RAM) at any time.
For ease of viewing and use, these files are typically stored in different directories or even subdirectories according to their relevance, such as the/PROC/SCSI directory where information about all SCSI devices on the current system is stored, and information about the processes currently running on the system is stored in/proc/n. , where n is a running process (as you can imagine, its associated directory disappears after a process has ended).
Most virtual files can be viewed using File view commands such as cat, more, or less, and some of the file information can be seen at a glance, but the information on the file is not very readable. However, these poorly readable files can perform well when using some commands such as APM, free, LSPCI, or top view.##查看线程数的方法cat /proc/{pid}/status其中Threads后面跟的就是线程数。或者:ls /proc/{pid}/task | wc -l##举例如下cat /proc/27587/statusThreads: 3或:ls /proc/27587/task |wc -l3
- Using the top command
##top命令后面跟-H,会打印出所有线程列表top -Htop -H -p {pid}##举例如下top -H -p 27587Tasks: 3 total, 0 running, 3 sleeping, 0 stopped, 0 zombiePID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND27587 siterec 20 0 1007m 79m 4772 S 1.7 0.2 203:34.71 python2753 siterec 20 0 1007m 79m 4772 S 0.0 0.2 2:19.32 python27590 siterec 20 0 1007m 79m 4772 S 0.0 0.2 0:00.00 python
- Using the PS command
# #ps followed by H, can print all threads of a process PS HH p {pid} | wc-l# #举例如下PID TTY STAT time COMMAND17721 pts/1 Sl 0:00/u sr/java/jdk1.8.0_60/bin/java-djava.util.logging.config.file=/mnt/deploy/apache-tomcat-8.0.35/conf/ logging.properties-djava.util.logging.manager=org.apache.juli.classloader17721 PTS/1 Sl 0:00/usr/java/jdk1.8.0_60 /bin/java-djava.util.logging.config.file=/mnt/deploy/apache-tomcat-8.0.35/conf/logging.properties- djava.util.logging.manager=org.apache.juli.classloader17721 PTS/1 Sl 0:00/usr/java/jdk1.8.0_60/bin/java-djava.uti L.logging.config.file=/mnt/deploy/apache-tomcat-8.0.35/conf/logging.properties-djava.util.logging.manager= Org.apache.juli.ClassLoader
When using the PS command to view the process, the process state corresponds to the following meanings:
D Non-disruptive sleep (usually in IO operation) received signal does not wake up and not run, the process must wait until there is an interrupt to occur
R is running or can be run (queued in the run queue)
S can interrupt sleep (dormant, blocked, waiting for a condition to form or receive a signal)
T stopped process received Sigstop, SIGSTP, Sigtin, Sigtou signal after stop operation
W is changing pages (2.6. Before the kernel is active)
X dead process (not turned on)
The Z-Zombie process process has terminated, but the process descriptor exists until the parent process calls WAIT4 () after the system call releases the BSD-style
< high priority (not good to other users)
N Low priority (Nice-to-other users)
L page locked in memory (real-time and custom IO)
s an information header
L Multithreading (using Clone_thread, like NPTL's pthreads)
+ in the foreground process group
- Using the Pstree command
##打印所有进程及其线程pstree -p ##打印某个进程的线程数pstree -p {pid} | wc -l
- System maximum number of threads exceeded
##报错信息:-bash: fork: retry: 没有子进程 -bash: fork: retry: 资源暂时不可用##解决方法:cat /etc/security/limits.d/20-nproc.conf或:cat /etc/security/limits.d/90-nproc.conf\* soft nproc 1024 // 将此处修改成unlimited或者其他数值root soft nproc unlimited
How to view the number of threads in Linux and error exceeding the maximum system thread value