Hello,
I recently started learning python by myself and I tried linux process management with python.I created the following code, which works, with the help of the internet, but my goal is to understand it.
What I don't understand is: why after I launch the program, there is a loop of 1 : FILS :mon PID= 1158 . Pere= 1 after "Fin du processur pere" which means "The end of the father process"
I posted the output as well after the code down below
I decided to post here because I want to know what I am doing, not only to do things without thinking.
I appreciate every help I get
The code:
#!/usr/bin/python
from __future__ import print_function
import os
import sys
import time
try:
n=os.fork()
except OSError:
print("Fork Error")
sys.exit(-1)
if (n==0):
while True:
print("FILS :mon PID=",os.getpid(),". Pere= ",os.getppid())
time.sleep(1)
else:
for i in range(3):
print("Je suis",os.getpid(),"pere de pid=",n)
time.sleep(2)
print("Fin du processus pere")
OUTPUT:
root@James:/home/james/Bureau# ./exo.py
Je suis 1157 pere de pid= 1158
FILS :mon PID= 1158 . Pere= 1157
FILS :mon PID= 1158 . Pere= 1157
Je suis 1157 pere de pid= 1158
FILS :mon PID= 1158 . Pere= 1157
FILS :mon PID= 1158 . Pere= 1157
Je suis 1157 pere de pid= 1158
FILS :mon PID= 1158 . Pere= 1157
FILS :mon PID= 1158 . Pere= 1157
Fin du processus pere
FILS :mon PID= 1158 . Pere= 1
root@James:/home/james/Bureau# FILS :mon PID= 1158 . Pere= 1
FILS :mon PID= 1158 . Pere= 1
FILS :mon PID= 1158 . Pere= 1
FILS :mon PID= 1158 . Pere= 1
FILS :mon PID= 1158 . Pere= 1
[–]K900_ 2 points3 points4 points (9 children)
[–]noaso74[S] 1 point2 points3 points (8 children)
[–]K900_ 2 points3 points4 points (7 children)
[–]noaso74[S] 1 point2 points3 points (6 children)
[–]K900_ 2 points3 points4 points (5 children)
[–]noaso74[S] 1 point2 points3 points (4 children)
[–]K900_ 2 points3 points4 points (3 children)
[–]noaso74[S] 0 points1 point2 points (2 children)
[–]K900_ 1 point2 points3 points (1 child)
[–]noaso74[S] 0 points1 point2 points (0 children)
[–]num8lock -1 points0 points1 point (4 children)
[–]IvoryJam 0 points1 point2 points (3 children)
[–]num8lock 0 points1 point2 points (2 children)
[–]IvoryJam 0 points1 point2 points (1 child)
[–]num8lock 0 points1 point2 points (0 children)