Pid的管理和分配 创建一个进程时, Pid位图 00010: do_fork→copy_process→alloc_pid struct pidmap 00011: atomic t nr_free; 00012: void *page; 00013: }; 00015:#define PIDMAP ENTRIES ((PID_MAX_LIMIT +8*PAGE_SIZE-1)/PAGE_SIZE/8) Pid名字空间 1958 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 pidcache 00023: unsigned int level; 00024: struct pid_namespace *parent: 00025: #ifdef CONFIG PROC FS 关于名字空间的更多信息,参见: 00026: struct vfsmount *proc_mnt;Nsproxy.cpid_namespace.ch 00027: #endif EMBEDDED SYSTEM LABORATORY 5uE料DUN0 ITUTE FOR AOVANCED5 UOY DF U百TC 00028:
2023/7/14 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 = 00071: .kref = 00072: refcount ATOMIC_INIT(2) 00073: 00074: .pidmap = 00075: [0...PIDMAP_ENTRIES-1]=ATOMIC_INIT(BITS_PER_PAGE),NULL 00076: 在start_kernel中,调用pidmap_init进行合理的初始化 00077: .last_pid =0, 00078: .level =0, 00079: child_reaper =&init_task 00080: }; 在kernel_init中,被修改为init进程 嵌入式系统实验室 2023/7/14 Linux操作系统分析 14/65 EMBEDDED SYSTEM LABORATORY 5uE料DUAN0 ITUTE FOR AOVANCED5 FUOY OF U百TC
2023/7/14 Linux操作系统分析 14/65 Init_pid_ns 在kernel_init中,被修改为init进程 在start_kernel中,调用pidmap_init进行合理的初始化
00526: void__init pidmap init(void) 00527: 00528: init_pid_ns.pidmap[o].page kzalloc(PAGE_SIZE,GFP_KERNEL); 00529: /*Reserve PID 0.We never call free_.pidmap(O)*/分配第一个位图页 00530: set_bit(o,init_pid_ns.pidmap[o].page); 00531: atomic_dec(&init_pid_ns.pidmap[o].nr_free); 00532: 00533: init_pid_ns.pid_cachep KMEM_CACHE(pid 00534: SLAB HWCACHE ALIGN I SLAB PANIC); 00535: 初始化struct pid的cache of Science and Technolo 嵌入式系统实验室 2023/7/14 Linux操作系统分析 15/65 EMBEDDED SYSTEM LABORATORY 百uE料DUAN0 ITUTE FOR AOVANCED5 FUOY DF U百TC
2023/7/14 Linux操作系统分析 15/65 分配第一个位图页 初始化struct pid的cache
阅读alloc pid、alloc pidmapi函数 掌握基于位图的pid分配方法 last pid+-l,或者下一个bit0,或者一个新的map的第 keme德或者从头开始(30以后) 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); pid_ns->last_pidpid; 2023/7/14 Linux return pid;
2023/7/14 Linux操作系统分析 16/65 ❖阅读alloc_pid、 alloc_pidmap函数 ❖掌握基于位图的pid分配方法 ❖last_pid+1,或者下一个bit0,或者一个新的map的第 一个bit或者从头开始(300以后) kernel/pid.c kernel/pid.c
用户如何获得一个进程的pd 女系统调用getpid 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 米 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 ence and Technolo 嵌入式系统实验室 2023/7/14 Linux操作系统分析 17/65 EMBEDDED SYSTEM LABORATORY 5uE料DUAN0 ITUTE FOR AOVANCED5 FUOY OF U百TC
2023/7/14 Linux操作系统分析 17/65 用户如何获得一个进程的pid ❖系统调用getpid ❖关于进程组 ➢使用组链表 ➢所有进程共享组内第一个进程的pid ⚫数据:tgid ❖单独一个进程可以看成只有一个进程的组 ❖getpid返回组pid