[Python] Using fork & exec in Python

Discussão em 'Python' iniciado por Stack, Abril 23, 2025.

  1. Stack

    Stack Membro Participativo

    I wrote a little test program to play around with fork() and exec() in Python, and I got some puzzling results. The program just forks a new process to run whatever is given as the command-line parameters in argv. Here is my code:

    import sys
    import os

    print("Start: " + str(os.getpid()))
    sys.stdout.flush()

    if len(sys.argv) > 1:
    pid = os.fork()
    if pid == 0:
    os.execvp(sys.argv[1], sys.argv[1:])
    elif pid > 0:
    print("Fork: " + str(pid))
    sys.stdout.flush()
    x = os.wait()
    if x[0] != pid: # NEEDED
    os.waitpid(pid,0) # NEEDED
    print("End: " + str(x[0]))
    else:
    print("Fork failed", file=sys.stderr)
    sys.exit(1)
    else:
    print("End")

    sys.stdout.flush()


    I run this with the following command:

    python test.py python test.py ps


    In other words, fork a process which runs another copy of the test program which forks yet another process which runs ps. The output I get looks like this:

    Start: 999773
    Fork: 999777
    Start: 999777
    Fork: 999781
    PID TTY TIME CMD
    999773 ? 00:00:00 python3
    999777 ? 00:00:00 python3
    999781 ? 00:00:00 ps
    End: 999781
    End: 999774


    So, process 999773 creates process 999777 which creates process 999781, which runs ps and shows processes 999773, 999777 amd 999781. Process 999781 terminates, so the parent process (999777) prints a message saying that 999781 terminated and then exits. Process 999773 then prints a message saying that process 999774 terminated. Where did 999774 come from?

    Then if I remove the two lines with the comment NEEDED, so that it waits for any process to terminate before terminating itself, I get the following output:

    Start: 1000822
    Fork: 1000830
    End: 1000824
    Python path configuration:
    PYTHONHOME = (not set)
    PYTHONPATH = (not set)
    program name = '/usr/bin/python3'
    isolated = 0
    environment = 1
    user site = 1
    import site = 1
    sys._base_executable = '/usr/bin/python3'
    sys.base_prefix = '/usr'
    sys.base_exec_prefix = '/usr'
    sys.executable = '/usr/bin/python3'
    sys.prefix = '/usr'
    sys.exec_prefix = '/usr'
    sys.path = [
    '/usr/lib/python38.zip',
    '/usr/lib/python3.8',
    '/usr/lib/python3.8/lib-dynload',
    ]
    Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
    Python runtime state: core initialized
    ModuleNotFoundError: No module named 'encodings'

    Current thread 0x00007f27b84f7740 (most recent call first):
    <no Python frame>


    Can anyone explain what on earth is going on here?

    Continue reading...

Compartilhe esta Página