Verwenden von Fork und Exec in PythonPython

Python-Programme
Anonymous
 Verwenden von Fork und Exec in Python

Post by Anonymous »

Ich habe ein kleines Testprogramm geschrieben, um mit fork () und exec () in Python herumzuspielen, und ich habe einige rätselhafte Ergebnisse erzielt. Das Programm gibt nur einen neuen Prozess, um alles auszuführen, was als Befehlszeilenparameter in Argv angegeben wird. Hier ist mein Code: < /p>

Code: Select all

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()
< /code>
Ich führe dies mit dem folgenden Befehl aus: < /p>
python test.py python test.py ps
Mit anderen Worten, geben Sie einen Prozess aus, bei dem eine weitere Kopie des Testprogramms ausgeführt wird, bei dem ein weiterer Prozess ausgeht, der PS ausführt. Die Ausgabe, die ich bekomme, sieht so aus: < /p>

Code: Select all

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
Also, Prozess 999773 Erstellt Prozess 999777, der Prozess 999781 erstellt, der ps zeigt und Prozesse 999773, 9997777 AMD 999781 ausführt. Prozess 999781 terminiert terminiert. Prozess 999773 druckt dann eine Meldung aus, dass der Prozess 999774 beendet wurde. Woher stammt 999774?

Code: Select all

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):

Kann jemand erklären, was auf der Welt hier vor sich geht?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post