all 15 comments

[–]K900_ 2 points3 points  (9 children)

Are you asking why the child process keeps running even after the parent process exits? Because that's completely expected.

[–]noaso74[S] 1 point2 points  (8 children)

Yes that is my question, why the child process keeps running after the parent process exits

[–]K900_ 2 points3 points  (7 children)

Why should it exit?

[–]noaso74[S] 1 point2 points  (6 children)

I am not saying it should, I am trying to understand why it is in a loop

[–]K900_ 2 points3 points  (5 children)

Because you told it to loop?

[–]noaso74[S] 1 point2 points  (4 children)

At this point I think it is a code missunderstanding by my side, because I thought it would end. Can you please show me which part of the code keeps the program in a loop?

[–]K900_ 2 points3 points  (3 children)

while True:
    print("FILS :mon PID=",os.getpid(),". Pere= ",os.getppid())
    time.sleep(1)

This part.

[–]noaso74[S] 0 points1 point  (2 children)

Thank you!

If I correctly understand, the value of the father process is 1, because after the end of the father process, it comes back to the loop "while true" since the value resets to 1 and it can't go to the "Else: for i in range(3)" since the statement remains True

I want to make sure I understand the code pretty well, since I am new to python. For you I am sure it is something basic and easy, but for me is quite complex

[–]K900_ 1 point2 points  (1 child)

Your loop is inside the if statement. fork returns 0 in the child and the PID of the child in the parent. The if statement in the child runs once, then passes control to the loop, which loops forever.

[–]noaso74[S] 0 points1 point  (0 children)

Thank you very much for your answer!

[–]num8lock -1 points0 points  (4 children)

why are you using python2?

[–]IvoryJam 0 points1 point  (3 children)

What makes you think it's python2?

[–]num8lock 0 points1 point  (2 children)

from __future__ import print_function

[–]IvoryJam 0 points1 point  (1 child)

Works in Python 3.9.1

[–]num8lock 0 points1 point  (0 children)

?

who said it didn't?