UNIX用唯一的被稱為進程ID(process ID)的證書值來標識進程。每個進程還有一個父進程ID(parent process ID)利用os模塊可以返回這兩個id值。建立outputPID.py文件,內容如下:
#!/usr/bin/env python
import os
def main():
print "I am process %d\n" % os.getpid()
print "My parent is %d\n" % os.getppid()
if __name__=="__main__":
main()
輸出結果如下:
[root@localhost ~]# ./outputPID.py
I am process 3711
My parent is 2419
os.getpid()返回當前進程id值,os.getppid()返回當前進程的父進程PID值。
返回一個進程的組ID和用戶ID分別使用os.geteuid()與os.geteuid(),而os.getgid(),os.getegid()分別返回進程的真實組ID與有效組ID。建立outputIDs.py 文件,內容如下:
#!/usr/bin/env python
import os
def main():
print "My real user ID is %d\n" % os.getuid()
print "My effective user ID is %d\n" % os.geteuid()
print "My read group ID is %d \n" % os.getgid()
print "My effective group ID is %d \n" % os.getegid()
if __name__=="__main__":
main()
以下是運行結果:
[root@localhost ~]# ./outputIDs.py
My real user ID is 0
My effective user ID is 0
My read group ID is 0
My effective group ID is 0