Skip to content Skip to sidebar Skip to footer

Different Processes Showed As Same Pid Within Netstat

I spawn few processes using the Python multiprocessing module. However when I call netstat -nptl, each ip:port listeners listed under the same PID. I'm using Python 2.7 on Ubuntu

Solution 1:

Using my Mac OS 10.9.5 (Python 2.7.3), I could reproduce the same behavior. After several try-and-error, it turned out that it's because the socket objects are shared among the processes. (lsof -p <pid> helps to identify listening sockets.)

When I made following change to Listener class, each process started to listen on its own port number by its own PID.

defget_name(self):
    # this method cannot refer to socket object any more# self.sockname should be initialized as "not-listening" at __init__return self.sockname

defrun(self):
    # Instantiate socket at run
    self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.socket.bind(('localhost', 0))
    self.sockname = self.socket.getsockname()
    # Listener knows socknameprint self.sockname
    self.socket.listen(1)
    time.sleep(self.ttl)

This behavior should be the same as Ubuntu's Python and netstat.

self.sockname remains "not-listening" at original process

To listen on port as independent process, sockets need to be created at run method of a Listener object (New process calls this method after creating copy of the object). However variables updated in this copied object in the new process are not reflected to the original objects in original process.

Post a Comment for "Different Processes Showed As Same Pid Within Netstat"