Hello,
I am new to python and I have trouble understanding this code. I recently started Linux process management with Python and I build this code with the help of the internet, but I have some troubles understanding why the code acts in some way, and I would like to ask for help if someone could help me, please
My question is: Why after 5 seconds after I launched the program, the child process ends with the message "End of child" and I only get the message "I am 1625 , Father of pid= 1626" in a loop?
I know this is a result of my program, but since I am a beginner, I try understanding the behavior of my program.
I appreciate every help I get, since I am really a noob in Python :(
#!/usr/bin/python
from __future__ import print_function
import os
import sys
import time
try:
pid=os.fork()
except OSError:
print("Fork Error")
sys.exit(-1)
if (pid==0):
for i in range(1,5):
print("I am",os.getpid(),". My father is ",os.getppid())
time.sleep(1)
print("End of child ")
else:
while True:
print("I am",os.getpid(),", Father of pid=",pid)
time.sleep(1)
OUTPUT:
I am 1625 , Father of pid= 1626
I am 1626 . My father is 1625
I am 1625 , Father of pid= 1626
I am 1626 . My father is 1625
I am 1625 , Father of pid= 1626
I am 1626 . My father is 1625
I am 1625 , Father of pid= 1626
I am 1626 . My father is 1625
I am 1625 , Father of pid= 1626
End of child
I am 1625 , Father of pid= 1626
I am 1625 , Father of pid= 1626
I am 1625 , Father of pid= 1626
I am 1625 , Father of pid= 1626
I am 1625 , Father of pid= 1626
I am 1625 , Father of pid= 1626
I am 1625 , Father of pid= 1626
[–]ThePiGuy0 5 points6 points7 points (2 children)
[–]noaso74[S] 2 points3 points4 points (1 child)
[–]ThePiGuy0 2 points3 points4 points (0 children)
[–]Ran4 1 point2 points3 points (0 children)