1pthread_atfork(3C) Standard C Library Functions pthread_atfork(3C)
2
3
4
6 pthread_atfork - register fork handlers
7
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 int pthread_atfork(void (*prepare) (void), void (*parent) (void),
13 void (*child) (void));
14
15
17 The pthread_atfork() function declares fork handlers to be called prior
18 to and following fork(2), within the thread that called fork(). The
19 order of calls to pthread_atfork() is significant.
20
21
22 Before fork() processing begins, the prepare fork handler is called.
23 The prepare handler is not called if its address is NULL.
24
25
26 The parent fork handler is called after fork() processing finishes in
27 the parent process, and the child fork handler is called after fork()
28 processing finishes in the child process. If the address of parent or
29 child is NULL, then its handler is not called.
30
31
32 The prepare fork handler is called in LIFO (last-in first-out) order,
33 whereas the parent and child fork handlers are called in FIFO (first-
34 in first-out) order. This calling order allows applications to preserve
35 locking order.
36
38 Upon successful completion, pthread_atfork() returns 0. Otherwise, an
39 error number is returned.
40
42 The pthread_atfork() function will fail if:
43
44 ENOMEM Insufficient table space exists to record the fork handler
45 addresses.
46
47
49 Solaris threads do not offer pthread_atfork() functionality (there is
50 no thr_atfork() interface). However, a Solaris threads application can
51 call pthread_atfork() to ensure fork()-safety, since the two thread
52 APIs are interoperable. Seefork(2) for information relating to fork()
53 in a Solaris threads environment in Solaris 10 relative to previous
54 releases.
55
57 Example 1 Make a library safe with respect to fork().
58
59
60 All multithreaded applications that call fork() in a POSIX threads pro‐
61 gram and do more than simply call exec(2) in the child of the fork need
62 to ensure that the child is protected from deadlock.
63
64
65
66 Since the "fork-one" model results in duplicating only the thread that
67 called fork(), it is possible that at the time of the call another
68 thread in the parent owns a lock. This thread is not duplicated in the
69 child, so no thread will unlock this lock in the child. Deadlock
70 occurs if the single thread in the child needs this lock.
71
72
73
74 The problem is more serious with locks in libraries. Since a library
75 writer does not know if the application using the library calls fork(),
76 the library must protect itself from such a deadlock scenario. If the
77 application that links with this library calls fork() and does not call
78 exec() in the child, and if it needs a library lock that may be held by
79 some other thread in the parent that is inside the library at the time
80 of the fork, the application deadlocks inside the library.
81
82
83
84 The following describes how to make a library safe with respect to
85 fork() by using pthread_atfork().
86
87
88 1. Identify all locks used by the library (for example
89 {L1,...Ln}). Identify also the locking order for these locks
90 (for example {L1...Ln}, as well.)
91
92 2. Add a call to pthread_atfork(f1, f2, f3) in the library's
93 .init section. f1, f2, f3 are defined as follows:
94
95 f1()
96 {
97 /* ordered in lock order */
98 pthread_mutex_lock(L1);
99 pthread_mutex_lock(...);
100 pthread_mutex_lock(Ln);
101 }
102
103 f2()
104 {
105 pthread_mutex_unlock(L1);
106 pthread_mutex_unlock(...);
107 pthread_mutex_unlock(Ln);
108 }
109
110 f3()
111 {
112 pthread_mutex_unlock(L1);
113 pthread_mutex_unlock(...);
114 pthread_mutex_unlock(Ln);
115 }
116
117
119 See attributes(5) for descriptions of the following attributes:
120
121
122
123
124 ┌─────────────────────────────┬─────────────────────────────┐
125 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
126 ├─────────────────────────────┼─────────────────────────────┤
127 │Interface Stability │Standard │
128 ├─────────────────────────────┼─────────────────────────────┤
129 │MT-Level │MT-Safe │
130 └─────────────────────────────┴─────────────────────────────┘
131
133 exec(2), fork(2), atexit(3C), attributes(5), standards(5)
134
135
136
137SunOS 5.11 12 Dec 2003 pthread_atfork(3C)