3

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?

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
  • @Jeff: "Is this cleanup something I can expect every POSIX compliant system to do?" Why is a question like this off-topic? It is fundamentally important for programmers to know to what extent we need to ensure proper releasing of resources. I am facing a difficult IPC Problem at the moment, so this question is not out of general curiosity. (I will edit accordingly). – Johannes Rudolph Jul 17 '11 at 12:21
  • I don't believe the clean-up has anything to do with POSIX. You can expect this automatic cleanup to happen on every operating system that is worthy of any real use at all. If an operating system didn't do this, every abnormal termination of an application would remove resources from the system and it would become unusable as resources permanently (until the next reboot) disappeared. – mah Jul 25 '11 at 13:49
  • @mah you know win98, don't you? :-) But you are right, I would certainly expect this too. – Johannes Rudolph Jul 25 '11 at 14:02
  • @Johannes Rudolph -- I reassert my qualifier "every operating system that is worthy of any real use" ;) Sadly, I'm afraid I remember Windows ME also... – mah Jul 26 '11 at 16:33

2 Answers2

2

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.

mah
  • 39,056
  • 9
  • 76
  • 93
1

Specific details will depend upon the operating system, but generally speaking user-code is run in a virtual address space/sandbox where it does not have any direct access to hardware resources. Anything that the user process wants to access/allocate must be provided by calling the OS and asking it for the desired resource.

Thus the OS has a simple way of knowing who has been allocated which resources, and as long as it keeps track of this information, cleaning up resources in the event of a crashed process is as simple as fetching the list of resources allocated to that process, and marking them all as available again.

aroth
  • 54,026
  • 20
  • 135
  • 176