Hi all,
In this article we will learn about,
An orphan process is a child process whose parent got finished or terminated. After the termination of parent process, init process will adopt it.If you look into details, the parent pid(ppid) of an orphan will be 1 which is the pid of init. Sometimes orphan process is intentionally created for certain needs, like if you run an application and you don't want the process created by your application to be killed, even after closing the application.
Creation of Orphan process using Python :-
Here, we are going to create an orphan process through python code.
The logic behind the code is,
Create a process using python multiprocessing module and target function will be parent.
In parent function another process is spawned , making it's target function as child. Using subprocess run a linux/unix command in child process.
In main method we create a process targeting parent(), which will spawn another process and finishes. So,the child process targeting child() will become orphan. You can verify by checking PIDs of the process.
The code below will give a clear idea about things mentioned.
Verify by running "ps -ef | grep gedit" and check for its ppid. Sample screenshot is attached below,
For any kind of corrections,suggestions or questions, please use comment section.
In this article we will learn about,
- Orphan process in Linux/Unix.
- How orphan process is created ?
- Creation of orphan process using python.
- Usage of python modules - multiprocessing , subprocess
An orphan process is a child process whose parent got finished or terminated. After the termination of parent process, init process will adopt it.If you look into details, the parent pid(ppid) of an orphan will be 1 which is the pid of init. Sometimes orphan process is intentionally created for certain needs, like if you run an application and you don't want the process created by your application to be killed, even after closing the application.
Creation of Orphan process using Python :-
Here, we are going to create an orphan process through python code.
The logic behind the code is,
Create a process using python multiprocessing module and target function will be parent.
In parent function another process is spawned , making it's target function as child. Using subprocess run a linux/unix command in child process.
In main method we create a process targeting parent(), which will spawn another process and finishes. So,the child process targeting child() will become orphan. You can verify by checking PIDs of the process.
The code below will give a clear idea about things mentioned.
import subprocess
import multiprocessing
import traceback
import time
processToStart = "gedit"
def child():
try:
linuxProcess = subprocess.Popen([processToStart], close_fds=True)
print("Process PID - ", linuxProcess.pid)
except Exception as e:
traceback.print_tb(e.__traceback__)
def parent():
try:
cc = multiprocessing.Process(name='child', target=child)
cc.daemon = False
cc.start()
except Exception as e:
traceback.print_tb(e.__traceback__)
def main():
try:
d = multiprocessing.Process(name='parent', target=parent)
d.daemon = False
d.start()
time.sleep(5)
d.terminate()
except Exception as e:
traceback.print_tb(e.__traceback__)
main()
Verify by running "ps -ef | grep gedit" and check for its ppid. Sample screenshot is attached below,
For any kind of corrections,suggestions or questions, please use comment section.
Comments
Post a Comment