In Python3, I have essentially the following code:
server.py:
import os
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("127.0.0.1", 10000))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.listen(5)
while True:
print("waiting")
connection, client_address = sock.accept()
print("received")
child_pid = os.fork()
if child_pid == 0:
print("connection received")
received = connection.recv(1024)
connection.sendall("OK".encode('utf-8'))
os._exit(0)
client.py:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", 10000))
sock.close()
When I start the server and then the client, each time the client finishes a zombie process remains.
How to change the code so that no zombie process remains?