No Windows e no Unixen, quando um processo sai, o kernel fecha todas as alças abertas.
Windows NT
Terminating a Process – MSDN
Terminating a process has the following results:
- [...]
- Any resources allocated by the process are freed.
- All kernel objects are closed.
- [...]
While open handles to kernel objects are closed automatically when a process terminates, the objects themselves exist until all open handles to them are closed. Therefore, an object will remain valid after a process that is using it terminates if another process has an open handle to it.
ExitProcess
function – MSDNExiting a process causes the following:
- [...]
- All of the object handles opened by the process are closed.
- [...]
Linux
exit(3)
– Linux Programmer's Manual (libc functions)All open stdio(3) streams are flushed and closed.
_exit(2)
– Linux Programmer's Manual (kernel syscalls)The function
_exit()
terminates the calling process "immediately". Any open file descriptors belonging to the process are closed; any children of the process are inherited by process 1, init, and the process's parent is sent a SIGCHLD signal.
Observe que, nos dois sistemas operacionais,
-
Sockets são apenas um tipo de descritores de arquivos (fd's) / objetos do kernel, portanto, o acima se aplica igualmente a arquivos e sockets.
-
As descrições dos arquivos no Unix, bem como os objetos do kernel
de objetos gerenciadosno Windows, podem ser de propriedade de vários processos -elessuas alças podem ser herdadas por processos filhos e até transmitidas usando funções IPC especiais. -
Um arquivo ou socket só é fechado quando todos os fd's apontados para ele são destruídos.