aboutsummaryrefslogtreecommitdiffhomepage
path: root/notes/fd_and_thread_problem.txt
blob: 2cb5487bbc9af03387b66e89a83f467a6a900b89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

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)