Let’s assume we have a process that allocates a socket listening on a specific port, does something with it and then terminates abnormaly. Now a second process starts and wants to allocate a socket listening on the same port that was previously held by the crahsed process. Is this socket available for re-allocation?
-
How does the Operating System recover resources that weren’t released properly? Does the OS track the process id along with each allocated resource?
-
Is this cleanup something I can expect every POSIX compliant system to do?
This is up to the operating system but generally an OS maintains a process control structure to, among other things, manage its resources. When a process allocates a resource from the system (such as opening a file or allocating memory), details of the allocation are placed in that structure. When the process terminates, anything left in it gets cleaned up – but it’s best to explicitly clean up as you go.