Saturday, 5 December 2015

Python: Get process and parent process ids

‘os’ module provides getpid(), getppid() methods to get process id and parent process ids.

Method
Description
os.getpid()
Return current process id
os.getppid()
Return parent process id. On UNIX systems, if the parent process exits before child process, then the id returned is the one of the init process (1), on Windows it is still the same id, which may be already reused by another process.


import os

def getProcessId():
    print("Process Id : ",os.getpid())
    print("Parent process Id : ",os.getppid())

if(__name__=="__main__"):
    getProcessId()

Output
Process Id :  70024
Parent process Id :  57995




Previous                                                 Next                                                 Home

No comments:

Post a Comment