i am trying to code socket server with fork in python. somehow a new fork will be created when a client is connected and this fork process will handle the connection including send/receive.
i ran this script on Linux centOS
and monitor resources with htop/top
to see how many forks (task) are shown. the
problem is when i kill some fork by using os._exit(0)
htop won't be changed (naturally it has to be decreased by killing forks) and when i close python script every thing will be back to normal ( Ram usage and tasks ).
so what i have to do that when i kill some fork by using os._exit(0)
, it effects on htop
in other hand releases all resources and do not wait until its own parent is killed ?
here it's the code to create forks:
def test(sock):
//handle socket then return
for i in range (1000):
sock,addr=socket.accept()
pid=os.fork()
if pid==0:
test(sock)
os._exit(0)
elif pid !=-1:
os.waitpid(-1, os.WNOHANG)