[英]How does python determine the PID seemingly without normal system calls in Linux?
When running the following command 运行以下命令时
strace -f python3 -c 'import os; print(os.getpid())'
I noticed that strace does not catch the call to the getpid
(2) system call. 我注意到strace不能捕获对
getpid
(2)系统调用的调用。 I first considered that this was due to glibc caching the pid, but there shouldn't be a pid for libc to cache without at least a single real system call. 我首先考虑到这是由于glibc缓存了pid,但是没有至少一个真实的系统调用,就不应有libc用于缓存的pid。 Then I considered that maybe vdso was the culprit, but running a C program that makes this system call through libc shows a
getpid
call when straced. 然后我认为可能是vdso的罪魁祸首,但是运行一个通过libc进行此系统调用的C程序时,显示了一个
getpid
调用。 I finally gave up and looked up the source of the os.getpid
python module, which apparently seems to be defined in Modules/posixmodule.c
. 我最终放弃了,并查找了
os.getpid
python模块的源代码,该模块显然似乎是在Modules/posixmodule.c
定义的。 To my surprise (and subsequent confusion), it makes a normal call to getpid
! 令我惊讶(以及随之而来的混乱)的是,它正常调用了
getpid
!
So my question is: How does python determine the result of os.getpid
? 所以我的问题是:python如何确定
os.getpid
的结果? and if such value is indeed obtained by a call to getpid
, how is that call actually being made? 如果确实通过调用
getpid
获得了该值,那么实际上是如何进行该调用的?
The way the vdso works is, among other things, mapping process-specific variables into userspace that the vdso functions know how to read. vdso的工作方式尤其是将特定于进程的变量映射到vdso函数知道如何读取的用户空间中。 One of them is the current process ID, so
gettimeofday
doesn't need to make a syscall to access that information. 其中之一是当前进程ID,因此
gettimeofday
无需进行syscall即可访问该信息。
Now, specifically for getpid
, it's not actually a VDSO call. 现在,专门针对
getpid
,它实际上不是VDSO调用。 In glibc before 2.25, the library would cache calls, and since part of the Python runtime calls getpid
, there wouldn't be calls to it after the first. 在2.25之前的glibc中,该库将缓存调用,并且由于部分Python运行时调用
getpid
,因此在第一个调用之后将不再对其进行调用。 From 2.25 onward, the library doesn't cache the process ID, and so every getpid
call results in a syscall. 从2.25开始,该库不会缓存进程ID,因此每个
getpid
调用都会导致一个syscall。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.