Creation of Orphan Process in Linux using Python
Hi all,
In this article we will learn about,
Orphan Process :-
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 its 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
Orphan Process :-
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 its 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.
This was huge information for all ,those who needed these type article. This was really good and of course knowledgeable. Thank you for sharing this much information with us.Import Data Philippines
ReplyDeleteThis article provided me with a wealth of information about Python Certification in Delhi. The article is both educational and helpful. Thank you for providing this information. Keep up the good work.
ReplyDelete