【随笔】Linux PID

一、ID task_struct结构体里面设计task id的有三个字段:

pid_t pid; pid_t tgid; struct task_struct *group_leader;

源码地址:https://elixir.bootlin.com/linux/latest/source/include/linux/sched.h#L804
【随笔】Linux PID
文章图片

其中pid_t就是一个int:
【随笔】Linux PID
文章图片

【随笔】Linux PID
文章图片

1. PID PID就是process ID。任何一个、不管是进程还是线程(Linux线程也是用的task_struct结构),其PID就是自己的进程ID。一个进程,如果只有主线程,那么PID是自己,TGID是自己等于PID,*group_leader指向自己。
2. TGID TGID就是thread group ID。如果一个进程创建了其他线程,那么被创建的线程有自己的PID,该线程的TGID就是主线程的PID,*group_leader指向主线程。getpid()系统调用,返回的是tgid而不是PID,所以在多线程应用中,所有线程都有相同的PID。
【随笔】Linux PID
文章图片

3. *group_leader 指向进程中主线程的指针。

二、pid_max 【随笔】Linux PID
文章图片

在32bit系统上,PID的最大值是32767;在64bit系统上,PID的最大值是4194303。要关注的有三个点:
  • /proc/sys/kernel/pid_max
    • the value in this file also acts as a system-wide limit on the total number of processes and threads.
  • /proc/sys/kernel/threads-max
    • This file specifies the system-wide limit on the number of threads (tasks) that can be created on the system.
    • The value written is checked against the available RAM pages. If the thread structures would occupy too much (more than 1/8th) of the available RAM pages, threads-max is reduced accordingly.
  • ulimit -u
    • 当前用户shell的环境变量,这个和ulimit -n一个姿势
    • Linux环境变量:https://blog.csdn.net/reliveit/article/details/45224575
    • ulimit -n:https://www.iteye.com/blog/jameswxx-2096461
What is the maximum value of the Process ID?:https://unix.stackexchange.com/questions/16883/what-is-the-maximum-value-of-the-process-id
man 5 proc
/proc/sys/kernel/pid_max (since Linux 2.5.34) This file specifies the value at which PIDs wrap around (i.e., the value in this file is one greater than the maximum PID). PIDs greater than this value are not allocated; thus, the value in this file also acts as a system-wide limit on the total number of processes and threads.The default value for this file, 32768, results in the same range of PIDs as on ear‐lier kernels.On 32-bit platforms, 32768 is the maximum value for pid_max.On 64-bit systems, pid_max can be set to any value up to 2^22 (PID_MAX_LIMIT, approximately 4 million).

man 5 proc
/proc/sys/kernel/threads-max (since Linux 2.3.11) This file specifies the system-wide limit on the number of threads (tasks) that can be created on the system. Since Linux 4.1, the value that can be written to threads-max is bounded.The minimum value that can be written is 20.The maximum value that can be written is given by the constant FUTEX_TID_MASK (0x3fffffff).If a value outside of this range is written to threads-max, the error EINVAL occurs. The value written is checked against the available RAM pages. If the thread structures would occupy too much (more than 1/8th) of the available RAM pages, threads-max is reduced accordingly.

ulimit参数说明:
选项 [options] 含义 例子
-H 设置硬资源限制,一旦设置不能增加。 ulimit – Hs 64;限制硬资源,线程栈大小为 64K。
-S 设置软资源限制,设置后可以增加,但是不能超过硬资源设置。 ulimit – Sn 32;限制软资源,32 个文件描述符。
-a 显示当前所有的 limit 信息。 ulimit – a;显示当前所有的 limit 信息。
-c 最大的 core 文件的大小, 以 blocks 为单位。 ulimit – c unlimited; 对生成的 core 文件的大小不进行限制。
-d 进程最大的数据段的大小,以 Kbytes 为单位。 ulimit -d unlimited;对进程的数据段大小不进行限制。
-f 进程可以创建文件的最大值,以 blocks 为单位。 ulimit – f 2048;限制进程可以创建的最大文件大小为 2048 blocks。
-l 最大可加锁内存大小,以 Kbytes 为单位。 ulimit – l 32;限制最大可加锁内存大小为 32 Kbytes。
-m 最大内存大小,以 Kbytes 为单位。 ulimit – m unlimited;对最大内存不进行限制。
-n 可以打开最大文件描述符的数量。 ulimit – n 128;限制最大可以使用 128 个文件描述符。
-p 管道缓冲区的大小,以 Kbytes 为单位。 ulimit – p 512;限制管道缓冲区的大小为 512 Kbytes。
-s 线程栈大小,以 Kbytes 为单位。 ulimit – s 512;限制线程栈的大小为 512 Kbytes。
-t 最大的 CPU 占用时间,以秒为单位。 ulimit – t unlimited;对最大的 CPU 占用时间不进行限制。
-u 用户最大可用的进程数。 ulimit – u 64;限制用户最多可以使用 64 个进程。
-v 进程最大可用的虚拟内存,以 Kbytes 为单位。 ulimit – v 200000;限制最大可用的虚拟内存为 200000 Kbytes。

补充:创建进程和线程的区别 图文摘抄自《趣谈Linux操作系统》第十九节:
创建进程的话,调用的系统调用是fork,在copy_process函数里面,会将五大结构files_struct、fs_struct、sighand_struct、signal_struct、mm_struct都复制一遍,从此父进程和子进程各用各的数据结构。而创建线程的话,调用的是系统调用clone,在copy_process函数里面, 五大结构仅仅是引用计数加一,也即线程共享进程的数据结构。
【【随笔】Linux PID】【随笔】Linux PID
文章图片


参考资料:
  • 《understanding Linux kernel 3rd》
  • 《趣谈Linux操作系统》
  • ulimit:https://www.ibm.com/developerworks/cn/linux/l-cn-ulimit/index.html
  • http://blog.chinaunix.net/uid-20662820-id-5690021.html

    推荐阅读