Pid的管理和分配 创建一个进程时 Pid位图 do fork>copyprocess >alloc_ _ pid 00010: struct pidmap 00011 atomic t nr free; 00012: void*page 00013: 00015: #define PIDMAP_ENTRIES(PID_MAX_LIMIT +8*PAGESIZE- 1)/PAGE_SIZE/8) 心Pid名字空间8 00017: struct pid_ namespace 00018 struct kref kref 00019 struct pidmap pidmap[ PIDMAP_ENTrIeS 00020 int last_pid 00021 struct task struct child_reaper 00022 struct kmem_cache pid_cachep' Struct pid cache 00023 unsigned int level 00024 struct pid namespace parent 00025: #ifdef CONFIG PROC Fs 关于名字空间的更多信息,参见 00026 struct vfsmount proc_mnt; Nsproxy cuPid namespace.ch 00027: #endif EMBEDDED SYSTEM LAF口RAT口RY 00028 SU:MDU INTHUTE OR AOVANCLD STUOY D USTt
2021/2/6 Linux操作系统分析 13/65 Pid的管理和分配 ❖创建一个进程时, ❖Pid名字空间 do_fork→copy_process→alloc_pid Struct pid的cache Pid位图 关于名字空间的更多信息,参见: Nsproxy.c以及pid_namespace.ch
Init pid ns 00064:/* 00065: PID-map pages start out as NULl, they get allocated upon 00066: first use and are never deallocated This way a low pid max 00067: value does not cause lots of bitmaps to be allocated, but 00068: the scheme scales to up to 4 million PIDs, runtime 00069: 00070: struct pid_namespace init_pid_ns=t 00071: kref=i 00072: refcount ATOMIC_INIT(2) 00073: 00074 pidmap =t 00075 [ O. PIDMAP_ENTRIES-1]= ATOMIC_INIT(BITS_PER_PAGE), NULL] 00076: 在 start kernel中,调用 pidmap_in进行合理的初始化 00077: last pid =0 00078 00079 child reaper= &init_task 00080: }; 在 kernel initr中,被修改为ini进程 2021/2/6 Linux操作系统分析 14/65 嵌入式系统实验室 EMBEDDED SYSTEM LAB口RAT口RY SU:MDU INTHUTE OR AOVANCLD STUOY D USTt
2021/2/6 Linux操作系统分析 14/65 Init_pid_ns 在kernel_init中,被修改为init进程 在start_kernel中,调用pidmap_init进行合理的初始化
00526:Vo id_init pidmap init(void) 00527: 00528 init_pid_ns. pidmaplo] page kzalloc(PAGE_SIZE, GFP_KERNEL) 00529: Reserve Pid o. We never call free pidmap(0)*/分配第一个位图页 00530 set_ bit(o, init_pid_ns. pidmaplol page); 00531: atomic_dec &init_ pid_ns. pidmaplol nr_ free 00532 00533: init_pid_ns pid_cache = KMEM_CACHE(pid 00534: SLAB HWCACHE ALIGN SLAB PANIC) 00535: 初始化 struct pid的 cache 2021/2/6 Linux操作系统分析 15/65 嵌入式系统实验室 EMBEDDED SYSTEM LAB口RAT口RY SU:MDU INTHUTE OR AOVANCLD STUOY D USTt
2021/2/6 Linux操作系统分析 15/65 分配第一个位图页 初始化struct pid的cache
☆阅读 alloc pid、 alloc pidmap函数 今掌握基于位图的p分配方法 ☆ last pid+1,或者下一个bit0,或者一个新的map的第 个bt或者从头开始(300以后) kernel/pid c 00129: pid last 1; 00130: if (pid > pid_max) 00131: pid RESERVED_ PIDS kernel/pid. C 00047: #define rESERvED Pids 300 if( test_and_set_bit(offset, map->page)) atomic_dec(&map->nr_free); 2021/2/6 id_ns->last_pid pid Linux扌 return pid
2021/2/6 Linux操作系统分析 16/65 ❖阅读alloc_pid、 alloc_pidmap函数 ❖掌握基于位图的pid分配方法 ❖last_pid+1,或者下一个bit0,或者一个新的map的第 一个bit或者从头开始(300以后) kernel/pid.c kernel/pid.c
用户如何获得一个进程的pd 冷系统调用epd k米 k sys getpid-return the thread group id of the current process Note, despite the name this returns the tgid not the pid the tgid and the pid are identical unless CLONE THREAD was specified on clone in 4 which case the tgid is the same in all threads of the same group. This is SMP safe as current->tgid does not change. asmlinkage long sys getpid( void) return task tgid vnr(current) ☆ getpid返回组pid 2021/2/6 Linux操作系统分析 17165 嵌入式系统实验室 EMBEDDED SYSTEM LAB口RAT口RY SU:MDU INTHUTE OR AOVANCLD STUOY D USTt
2021/2/6 Linux操作系统分析 17/65 用户如何获得一个进程的pid ❖系统调用getpid ❖关于进程组 ➢使用组链表 ➢所有进程共享组内第一个进程的pid ⚫数据:tgid ❖单独一个进程可以看成只有一个进程的组 ❖getpid返回组pid