fd and thread problem ===================== File descriptors are: 1. assigned by the kernel, and 2. shared by all threads in a process. This causes problems if combined with asynchrounous thread termination. Solutions: * Have a separate thread that creates all fd, and closes them whenever the owning thread dies. * Linux-specific: Use unshare() with CLONE_FILES to have per-thread fd tables. * OpenBSD/FreeBSD: Use rfork_thread() with RFPROC|RFFDG|RFMEM|RFSIGSHARE (or RFCDG instead of RFFDG for a clean fd table) (not sure about RFSIGSHARE. also, OpenBSD does not seem to have it) ... BUT rfork_thread() is deprecated in FreeBSD, (and rfork() seems to have been removed). pthread_create is meant to be an alternative, but does not seem to support having separate fd tables per thread. * NetBSD/any UNIX-like without rfork: NetBSD not seem to have rfork(), but it should be possible to fork() from a "template process" without any memory mappings besides the code (and perhaps a minimal stack/data segment? unless those can be munmap'ed)