linux shell pgrep命令使用方法(pgrep指令)获取进程号、统计进程数量(学会区分Linux进程进程名)

article/2025/10/25 1:29:16

文章目录

    • 问题背景
    • pgrep指令
      • help文档
      • 使用示例
        • 1. 列出匹配进程的PID和进程名称(-l)(默认只能从进程名的子集字符串匹配,如果要使用完整进程名的子集字符串匹配,请加-f参数,下同)
        • 2. 列出匹配进程的PID和完整的命令行(-a)
        • 3. 统计匹配进程的数量(-c)
        • 4. 使用完整的进程名称进行匹配(-f)
        • 5. 不区分大小写进行匹配(-i)
        • 6. 选择最近启动的进程(n)
        • 7. 选择最早启动的进程(-o)
        • 8. 仅匹配给定父进程的子进程(-P)
        • 9. 通过控制终端进行匹配(-t)
          • 用途
          • 命令
          • 测试步骤
          • 示例
        • 10. 通过有效ID进行匹配(-u)
          • 用途
          • 命令
          • 测试步骤
          • 示例
        • 11. 通过真实ID进行匹配(-U)
          • 命令
          • 示例
        • 12. 精确匹配命令名称(-x)★★★
          • 命令
          • 示例

问题背景

按照我之前,在脚本中,获取除脚本自身进程之外与脚本同名进程号的方法:

ps -ef | grep "${SCRIPT_NAME}" | grep -v "grep" | awk '{print $2}' | grep -v "$PID"

这种方法有很大问题,莫名奇妙的,它无法正常过滤掉grep的进程(这里面还有点复杂,我一时半会也搞不明白咋回事,据说是grep会开子进程,并非grep那个子进程,而是开了一个与脚本相同的进程,导致出现问题,具体参考:linux shell脚本执行命令时创建子进程问题(特定的情况,例如后台运行、管道、分支或子shell等,脚本可能会创建子进程执行命令)grep)

后来我改用pgrep指令,用这个命令的好处是,不用使用grep命令了,它直接找出来就是进程号,而且不会带入额外的进程号,下面我们来看下pgrep指令的具体用法。

pgrep指令

pgrep命令用于根据进程的名称或其他属性来查找和列出匹配的进程ID(PID)。它可以根据不同的选项进行灵活的进程查找和过滤。

help文档

root@ubuntu:/ky/boot# pgrep --helpUsage:pgrep [options] <pattern>Options:-d, --delimiter <string>  specify output delimiter-l, --list-name           list PID and process name-a, --list-full           list PID and full command line-v, --inverse             negates the matching-w, --lightweight         list all TID-c, --count               count of matching processes-f, --full                use full process name to match-g, --pgroup <PGID,...>   match listed process group IDs-G, --group <GID,...>     match real group IDs-i, --ignore-case         match case insensitively-n, --newest              select most recently started-o, --oldest              select least recently started-P, --parent <PPID,...>   match only child processes of the given parent-s, --session <SID,...>   match session IDs-t, --terminal <tty,...>  match by controlling terminal-u, --euid <ID,...>       match by effective IDs-U, --uid <ID,...>        match by real IDs-x, --exact               match exactly with the command name-F, --pidfile <file>      read PIDs from file-L, --logpidfile          fail if PID file is not locked-r, --runstates <state>   match runstates [D,S,Z,...]--ns <PID>                match the processes that belong to the samenamespace as <pid>--nslist <ns,...>         list which namespaces will be considered forthe --ns option.Available namespaces: ipc, mnt, net, pid, user, uts-h, --help     display this help and exit-V, --version  output version information and exitFor more details see pgrep(1).

中文翻译:

root@ubuntu:/ky/boot# pgrep --help用法:pgrep [选项] <模式>选项:-d, --delimiter <字符串>  指定输出分隔符-l, --list-name           列出PID和进程名称-a, --list-full           列出PID和完整命令行-v, --inverse             反转匹配结果-w, --lightweight         列出所有TID-c, --count               统计匹配进程的数量-f, --full                使用完整进程名称进行匹配-g, --pgroup <PGID,...>   匹配指定的进程组ID-G, --group <GID,...>     匹配真实组ID-i, --ignore-case         不区分大小写进行匹配-n, --newest              选择最近启动的进程-o, --oldest              选择最早启动的进程-P, --parent <PPID,...>   仅匹配给定父进程的子进程-s, --session <SID,...>   匹配会话ID-t, --terminal <tty,...>  通过控制终端进行匹配-u, --euid <ID,...>       通过有效ID进行匹配-U, --uid <ID,...>        通过真实ID进行匹配-x, --exact               精确匹配命令名称-F, --pidfile <文件>      从文件中读取PID-L, --logpidfile          如果PID文件未锁定,则失败-r, --runstates <状态>    匹配运行状态 [D,S,Z,...]--ns <PID>                匹配与<pid>相同命名空间的进程--nslist <ns,...>         列出将用于--ns选项的命名空间可用的命名空间:ipc, mnt, net, pid, user, uts-h, --help     显示此帮助信息并退出-V, --version  输出版本信息并退出更多详细信息请参阅pgrep(1)

请注意,上面的<pattern>是要匹配的进程名称或其他属性的模式。可以根据实际情况替换为具体的值。

使用示例

以下是pgrep命令不同选项的使用示例:

我们在这个容器中测试:

注意:我们首先要学会区分什么是进程的进程名!
进程名就是那个进程的可执行文件,进程名只有一个,除去进程名,其他的都是路径和参数。
比如/bin/bash /usr/local/bin/entrypoint.sh,进程名是bash

root@5940438e0ee6:/build/libevent# echo "=====输出所有进程完整进程信息:"
=====输出所有进程完整进程信息:
root@5940438e0ee6:/build/libevent# ps -ef 
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 Jun27 pts/0    00:00:00 /bin/bash /usr/local/bin/entrypoint.sh
root           7       1  0 Jun27 pts/0    00:02:56 ./kyai_rest
root          43       0  0 23:38 pts/1    00:00:00 /bin/bash
root          56      43  0 23:40 pts/1    00:00:00 ps -ef
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# echo "输出所有进程进程名:"
=====输出所有进程进程名:
root@5940438e0ee6:/build/libevent# ps -e -o comm=
bash
kyai_rest
bash
ps
root@5940438e0ee6:/build/libevent# 

1. 列出匹配进程的PID和进程名称(-l)(默认只能从进程名的子集字符串匹配,如果要使用完整进程名的子集字符串匹配,请加-f参数,下同)

pgrep -l <pattern>
pgrep --list-name <pattern>

示例:

root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -l ky
7 kyai_rest
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -l ./
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -l ./ky
root@5940438e0ee6:/build/libevent#
root@5940438e0ee6:/build/libevent# pgrep -l -f ./ky
7 kyai_rest

2. 列出匹配进程的PID和完整的命令行(-a)

pgrep -a <pattern>
pgrep --list-full <pattern>

示例:

root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a ky
7 ./kyai_rest
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a ./ky
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a -f ./ky
7 ./kyai_rest
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -af ./ky
7 ./kyai_rest
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a bash
1 /bin/bash /usr/local/bin/entrypoint.sh
43 /bin/bash
root@5940438e0ee6:/build/libevent# 

3. 统计匹配进程的数量(-c)

pgrep -c <pattern>
pgrep --count <pattern>

示例:

root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -c ky
1
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -c bash
2
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -c ./ky
0
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -c -f ./ky
1
root@5940438e0ee6:/build/libevent# 

4. 使用完整的进程名称进行匹配(-f)

pgrep -f <pattern>
pgrep --full <pattern>

示例:

root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -f ky
7
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -f ./ky
7
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep ./ky
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep /usr/local/bin/entrypoint.sh
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -f /usr/local/bin/entrypoint.sh
1
root@5940438e0ee6:/build/libevent# 

5. 不区分大小写进行匹配(-i)

pgrep -i <pattern>
pgrep --ignore-case <pattern>

示例:

root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -i KyAi
7
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -i entry
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -i -f eNtry
1
root@5940438e0ee6:/build/libevent# 

6. 选择最近启动的进程(n)

pgrep -n <pattern>
pgrep --newest <pattern>

示例:

root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a bash
1 /bin/bash /usr/local/bin/entrypoint.sh
43 /bin/bash
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a -n bash
43 /bin/bash
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -n bash
43
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# 

7. 选择最早启动的进程(-o)

pgrep -o <pattern>
pgrep --oldest <pattern>

示例:

root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a bash
1 /bin/bash /usr/local/bin/entrypoint.sh
43 /bin/bash
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a -o bash
1 /bin/bash /usr/local/bin/entrypoint.sh
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -o bash
1
root@5940438e0ee6:/build/libevent# 

8. 仅匹配给定父进程的子进程(-P)

pgrep -P <PPID> <pattern>
pgrep --parent <PPID> <pattern>

示例:

比如0号进程有1号何2号两个子进程:

在这里插入图片描述

pgrep -P 0能将0号进程的子进程找出来:

root@ubuntu:~# 
root@ubuntu:~# pgrep -P 0
1
2
root@ubuntu:~# 

在查找到时对子进程筛选:

root@ubuntu:~# 
root@ubuntu:~# pgrep -P 0 "system"
1
root@ubuntu:~# 
root@ubuntu:~# pgrep -P 0 "thread"
2
root@ubuntu:~# 
root@ubuntu:~# 

注意:有时用-f参数从非进程名的字符串段匹配筛选时,注意双引号的使用,以及使用双引号后,仍应注意的问题:

下面测试中:pgrep -f -P 0 --systempgrep -f -P 0 "--system"都是报错的,只有pgrep -f -P 0 " --system"没出问题。

root@ubuntu:~# 
root@ubuntu:~# pgrep -f -P 0 --system
pgrep: unrecognized option '--system'Usage:pgrep [options] <pattern>Options:-d, --delimiter <string>  specify output delimiter-l, --list-name           list PID and process name-a, --list-full           list PID and full command line-v, --inverse             negates the matching-w, --lightweight         list all TID-c, --count               count of matching processes-f, --full                use full process name to match-g, --pgroup <PGID,...>   match listed process group IDs-G, --group <GID,...>     match real group IDs-i, --ignore-case         match case insensitively-n, --newest              select most recently started-o, --oldest              select least recently started-P, --parent <PPID,...>   match only child processes of the given parent-s, --session <SID,...>   match session IDs-t, --terminal <tty,...>  match by controlling terminal-u, --euid <ID,...>       match by effective IDs-U, --uid <ID,...>        match by real IDs-x, --exact               match exactly with the command name-F, --pidfile <file>      read PIDs from file-L, --logpidfile          fail if PID file is not locked-r, --runstates <state>   match runstates [D,S,Z,...]--ns <PID>                match the processes that belong to the samenamespace as <pid>--nslist <ns,...>         list which namespaces will be considered forthe --ns option.Available namespaces: ipc, mnt, net, pid, user, uts-h, --help     display this help and exit-V, --version  output version information and exitFor more details see pgrep(1).
root@ubuntu:~# 
root@ubuntu:~# pgrep -f -P 0 "--system"
pgrep: unrecognized option '--system'Usage:pgrep [options] <pattern>Options:-d, --delimiter <string>  specify output delimiter-l, --list-name           list PID and process name-a, --list-full           list PID and full command line-v, --inverse             negates the matching-w, --lightweight         list all TID-c, --count               count of matching processes-f, --full                use full process name to match-g, --pgroup <PGID,...>   match listed process group IDs-G, --group <GID,...>     match real group IDs-i, --ignore-case         match case insensitively-n, --newest              select most recently started-o, --oldest              select least recently started-P, --parent <PPID,...>   match only child processes of the given parent-s, --session <SID,...>   match session IDs-t, --terminal <tty,...>  match by controlling terminal-u, --euid <ID,...>       match by effective IDs-U, --uid <ID,...>        match by real IDs-x, --exact               match exactly with the command name-F, --pidfile <file>      read PIDs from file-L, --logpidfile          fail if PID file is not locked-r, --runstates <state>   match runstates [D,S,Z,...]--ns <PID>                match the processes that belong to the samenamespace as <pid>--nslist <ns,...>         list which namespaces will be considered forthe --ns option.Available namespaces: ipc, mnt, net, pid, user, uts-h, --help     display this help and exit-V, --version  output version information and exitFor more details see pgrep(1).
root@ubuntu:~# 
root@ubuntu:~# 
root@ubuntu:~# pgrep -f -P 0 " --system"
1
root@ubuntu:~# 
root@ubuntu:~# 

9. 通过控制终端进行匹配(-t)

用途

指定特定终端名称进行搜索的用途之一是限定搜索范围。在多用户或多终端环境中,可能会有多个终端同时运行着相同的进程。通过指定终端名称,你可以仅在特定终端中搜索匹配的进程,而不是在所有终端中搜索。

这对于需要在特定终端上执行操作或监控特定终端上的进程非常有用。例如,你可能希望在某个特定的终端上查找正在运行的某个应用程序的进程,或者在某个特定的终端上查找与某个用户相关的进程。

另外,指定终端名称还可以用于在脚本或自动化任务中进行进程管理。通过指定特定的终端,你可以确保只对特定终端上的进程进行操作,而不会影响其他终端上的进程。

总而言之,指定特定终端名称进行搜索可以提供更精确和有针对性的进程查找和管理。

命令
pgrep -t <tty> <pattern>
pgrep --terminal <tty> <pattern>
测试步骤

按照以下步骤进行测试:

  1. 打开终端。
  2. 运行 tty 命令,获取当前终端的名称。例如,如果终端名称是 /dev/tty1,则将 <tty> 替换为 tty1
  3. 运行 pgrep -t <tty> <pattern> 命令,将 <pattern> 替换为你想要查找的进程名称或关键字。例如,如果你想要查找所有运行着的 bash 进程,可以运行 pgrep -t tty1 bash
  4. 如果命令成功执行,它将输出匹配的进程 ID。
  5. 你也可以尝试运行 pgrep --terminal <tty> <pattern> 命令,它与上述命令的作用相同。

请注意,pgrep 命令用于在进程列表中查找匹配的进程,并输出它们的进程 ID。<tty> 是终端名称,<pattern> 是要查找的进程名称或关键字。你需要根据你的实际情况替换这些参数。

示例
root@ubuntu:~# 
root@ubuntu:~# tty
/dev/pts/0
root@ubuntu:~# 
root@ubuntu:~# 
root@ubuntu:~# pgrep -t /dev/pts/0 bash
root@ubuntu:~# 
root@ubuntu:~# pgrep -t /dev/pts/0 sy
root@ubuntu:~# 
root@ubuntu:~# pgrep -t /dev/pts/0
root@ubuntu:~# 
root@ubuntu:~# pgrep -t "/dev/pts/0"
root@ubuntu:~# 
root@ubuntu:~# pgrep -t "/dev/pts/0" bash
root@ubuntu:~# 
root@ubuntu:~# 

尴尬,我这啥也搜不出来

10. 通过有效ID进行匹配(-u)

用途

pgrep -u <ID> <pattern>pgrep --euid <ID> <pattern> 命令用于在指定用户 ID 或有效用户 ID 下查找匹配的进程。

  • <ID> 参数用于指定用户 ID 或有效用户 ID。你可以使用用户的用户名或用户 ID 来替换 <ID>
  • <pattern> 参数用于指定要查找的进程名称或关键字。
命令
pgrep -u <ID> <pattern>
pgrep --euid <ID> <pattern>
测试步骤
  1. 打开终端。
  2. 运行 id 命令,获取当前用户的用户 ID 和有效用户 ID。例如,如果用户 ID 是 1000,有效用户 ID 是 1000,则将 <ID> 替换为 1000
  3. 运行 pgrep -u <ID> <pattern> 命令,将 <pattern> 替换为你想要查找的进程名称或关键字。例如,如果你想要查找当前用户下所有运行着的 bash 进程,可以运行 pgrep -u 1000 bash
  4. 如果命令成功执行,它将输出匹配的进程 ID。
  5. 你也可以尝试运行 pgrep --euid <ID> <pattern> 命令,它与上述命令的作用相同。

请注意,pgrep 命令用于在进程列表中查找匹配的进程,并输出它们的进程 ID。-u--euid 选项用于指定用户 ID 或有效用户 ID。<ID> 是用户 ID 或有效用户 ID,<pattern> 是要查找的进程名称或关键字。你需要根据你的实际情况替换这些参数。

示例
root@ubuntu:~# 
root@ubuntu:~# id
uid=0(root) gid=0(root) groups=0(root)
root@ubuntu:~# 
root@ubuntu:~# 
root@ubuntu:~# pgrep -u 0000 bash
3058
3955
23187
root@ubuntu:~# 
root@ubuntu:~# pgrep -u 0 bash
3058
3955
23187
root@ubuntu:~# 
root@ubuntu:~# pgrep -u 1000 bash
root@ubuntu:~# 
root@ubuntu:~# ps -ef | grep bash
root        1061       1  0 19:27 ?        00:00:00 /bin/bash /etc/systemd/nvmemwarning.sh
root        1260       1  0 19:28 ?        00:00:00 /bin/bash /ky/boot/kyai_nv_server.sh
root        1276       1  0 19:28 ?        00:00:00 /bin/bash /etc/systemd/nvgetty.sh
root        3058    2914  0 19:28 pts/0    00:00:00 /bin/bash /usr/local/bin/entrypoint.sh
root        3955    3915  0 19:28 pts/0    00:00:00 /bin/bash /usr/local/bin/entrypoint.sh
root       23187   23106  0 21:46 pts/0    00:00:00 -bash
root       36607   23187  0 23:39 pts/0    00:00:00 grep --color=auto bash
root@ubuntu:~# 
root@ubuntu:~# 

在这里插入图片描述

11. 通过真实ID进行匹配(-U)

具体请参见有效id和真实id,我目前还不是很理解。

命令
pgrep -U <ID> <pattern>
pgrep --uid <ID> <pattern>
示例
root@ubuntu:~# 
root@ubuntu:~# pgrep -U 0000 bash
3058
3955
23187
root@ubuntu:~# 
root@ubuntu:~# 

12. 精确匹配命令名称(-x)★★★

命令
pgrep -x <pattern>
pgrep --exact <pattern>
示例
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# ps -ef 
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 19:28 pts/0    00:00:00 /bin/bash /usr/local/bin/entrypoint.sh
root           8       1  0 19:28 pts/0    00:00:39 ./kyai_rest
root          41       0  0 23:45 pts/1    00:00:00 /bin/bash
root          54      41  0 23:46 pts/1    00:00:00 ps -ef
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x kyai 
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x kyai_rest
8
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x -f kyai_rest
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x -f ./kyai_rest
8
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x bash          
1
41
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x -f bash
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x -f /bin/bash
41
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x -f /bin/bash /usr/local/bin/entrypoint.sh
pgrep: only one pattern can be provided
Try `pgrep --help' for more information.
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x -f "/bin/bash /usr/local/bin/entrypoint.sh"
1
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# 

注意:精准匹配,是包含参数的所有完整匹配

在这里插入图片描述


http://chatgpt.dhexx.cn/article/B0g0DLGt.shtml

相关文章

pgrep

Pgrep 用于根据进程的name查找进程的pid。例如&#xff1a;

Linux pgrep命令

1 pgrep pgrep是一个根据名称查找进程ID的命令&#xff0c;返回的是进程ID&#xff0c;若存在当个进程&#xff0c;则分为不同的行返回ID&#xff08;默认实现&#xff09;。 2 示例 查找java进程&#xff1a; pgrep java上图还显示了ps与pgrep的区别&#xff0c;简单来说&…

【笔记11】个人扫盲:内存与CPU中的核、线程、物理CPU、逻辑CPU

文章目录 CPU与内存CPU内存什么是通道常见问题 物理CPU和逻辑CPU常见问题LINUX查看WINDOW查看 CPU与内存 CPU CPU&#xff08;Central Processing Unit&#xff09;即中央处理器。CPU从内存&#xff08;Memory&#xff09;或缓存&#xff08;Cache&#xff09;中取出指令&…

Linux下区分物理CPU、逻辑CPU和CPU核数

一、概念 ① 物理CPU 实际Server中插槽上的CPU个数 物理cpu数量&#xff0c;可以数不重复的 physical id 有几个 ② 逻辑CPU Linux用户对 /proc/cpuinfo 这个文件肯定不陌生. 它是…

记录一个有意思的cpu逻辑cpu核数问题(已解决)

top命令查看逻辑cpu个数的时候发现只有20个 实际上服务器的的逻辑cpu个数是32个 物理cpu核数 PS&#xff1a;该问题也存在于部分新旧服务器上&#xff0c;网上没找到答案&#xff0c;特此记录。希望未来可以得到答案。 2021年09月27日更新 目前认为最近用到了多少cpu核数就显…

物理CPU,物理CPU内核,逻辑CPU概念详解

1.说明 CPU(Central Processing Unit)是中央处理单元&#xff0c; 本文介绍物理CPU&#xff0c;物理CPU内核&#xff0c;逻辑CPU&#xff0c; 以及他们三者之间的关系&#xff0c; 一个物理CPU可以有1个或者多个物理内核&#xff0c; 一个物理内核可以作为1个或者2个逻辑CPU。 …

Linux下区分物理CPU、逻辑CPU、CPU核数、线程数以及查看命令

概念 ① 物理CPU 实际服务器中插槽上的CPU个数 是指物理上&#xff0c;也就是硬件上存在着几颗物理cpu,指的是真实存在是cpu处理器的个数&#xff0c;1个代表一颗&#xff0c;2个代表2颗cpu处理器。 物理cpu数量&#xff0c;可以数不重复的 physical id 有几个 ② 逻辑CPU Lin…

linux服务器的物理CPU,CPU核数,逻辑CPU及Hadoop的Vcore

linux服务器的物理CPU&#xff0c;CPU核数&#xff0c;逻辑CPU及Hadoop的Vcore 1、Linux服务器的核数的概念 物理CPU&#xff1a; 服务器上真实存在的CPU&#xff0c;可以看到 CPU的核 (core)&#xff1a; 一个CPU上包含多少核(core)&#xff0c;真实存在但不能直接看到 总核数…

CPU 主频,核数 参数解读 物理CPU,逻辑CPU,物理核 概念辨析

Q1&#xff1a; CPU高主频好还是多核数好&#xff1f; 游戏需求 如果是主打游戏&#xff0c;由于游戏需要的是最简单粗暴的计算工作&#xff0c;这方面多核心有点无用武之地。因此&#xff0c;目前主流游戏都是双核心调用&#xff0c;四核或者更多核心的比较少。 也就是说&…

物理CPU-Core-逻辑CPU -超线程

原文地址&#xff1a; http://www.daniloaz.com/en/differences-between-physical-cpu-vs-logical-cpu-vs-core-vs-thread-vs-socket/ 水平所限&#xff0c;翻译不准确的地方望指正。 当我们使用像 nproc或者 lscpu等命令 来在CPU级别上获取计算机的架构和性能的时候&#xf…

【Pytorch】物理cpu、逻辑cpu、cpu核数、pytorch线程数设置

上周末写ddp&#xff0c;常常遇到中途退出的问题&#xff0c;解决中途遇到了很多CPU线程数和核心数的问题&#xff0c;记录如下 1. 物理cpu、逻辑cpu、cpu核数、超线程 这一部分主要来自什么是物理cpu&#xff0c;什么是逻辑cpu&#xff0c;什么cpu核数&#xff0c;什么是超线…

Linux下关于物理CPU、逻辑CPU和CPU核数、超线程

1、概念 &#xff08;1&#xff09; 【物理CPU数】 &#xff1a;实际Server中插槽上的CPU个数&#xff0c;物理cpu数量可以数不重复的 physical id 有几个 &#xff08;physical id&#xff09; &#xff08;2&#xff09; 【CPU核数】&#xff1a;单块CPU上面能处理数据的芯…

物理CPU,CPU核数,逻辑CPU

CPU概念 物理CPU 在Info中由physical id识别 物理CPU指的是实际主板上插槽上的CPU个数。physical id 就是每个物理CPU的ID&#xff0c;不重复的 physical id 有几个&#xff0c;物理cpu数量就是几个。 CPU核数&#xff08;物理核&#xff09; 在Info中由 cpu cores 识别 CPU核…

物理cpu数,逻辑cpu数,cpu核数

一、物理CPU数 机器的主板上实际插入插槽的CPU个数。 二、CPU核数 单块CPU上面集成一个或者多个处理器芯片(称为Core&#xff0c;核心)。 CPU最初发展的时候是一个CPU一个处理核心&#xff0c;CPU的性能主要靠提高核心工作频率来提高&#xff0c;但是仅仅提高单核芯片的速度…

物理cpu数和cpu核数和逻辑cpu数和vcpu区别

物理cpu数和cpu核数和逻辑cpu数和vcpu区别1.物理cpu数&#xff1a;主板上实际插入的cpu数量&#xff0c;可以数不重复的 physical id 有几个&#xff08;physical id&#xff09; 2.cpu核数&#xff1a;单块CPU上面能处理数据的芯片组的数量&#xff0c;如双核、四核等 &#…

CPU相关概念:物理cpu数、核数、逻辑cpu数,12核20线程实例分析

文章目录 0. 写此篇博客的原因1. 物理CPU&#xff0c;核&#xff0c;逻辑CPU概念1.1 内核工作方式1.2 超线程1.3 什么是大小核 2. CPU信息的查询方法2.1 Windows下查询CPU信息的方法2.2 Linux下查询CPU信息的方法方法一&#xff1a;lscpu 整体查看方法二&#xff1a;分别查看 3…

Linux系统下物理CPU和逻辑CPU的区别

01、问题 最近在搞Linux下性能评测&#xff0c;在做CPU评测时发现了个有意思的现象&#xff0c;因为uos系统是自带系统监视器的&#xff0c;在对输入法进程检测时&#xff0c;发现其CPU占用率为1%&#xff1a; 但是我用top命令查询时却发现它的CPU占用率为4%&#xff01; 这是…

理解物理 CPU 与逻辑 CPU

这是一篇有关物理和逻辑 CPU 实际含义以及它们之间有何差异的速写文章。 首先&#xff0c;CPU 这个术语在互联网上的使用非常的模糊和不严谨&#xff0c;常用来指处理器&#xff08;Processor&#xff09;、核心&#xff08;Cores&#xff09;或硬件线程&#xff08;Hardware …

【cpu概念】物理cpu个数、核数、逻辑cpu数的概念

首先要明确物理cpu个数、核数、逻辑cpu数的概念 1.物理cpu数&#xff1a;主板上实际插入的cpu数量&#xff0c;可以数不重复的 physical id 有几个&#xff08;physical id&#xff09; 2.cpu核数&#xff1a;单块CPU上面能处理数据的芯片组的数量&#xff0c;如双核、四核等 …

物理cpu、物理cpu内核、逻辑cpu

物理cpu&#xff1a;就是实在的可以看到的计算机硬件&#xff0c;插入计算机的cpu插槽里面。如下图所示 在Linux系统中想查看物理cpu个数命令&#xff1a;cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l 物理cpu内核&#xff1a;以前最早是一个cpu一个核心…