1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
| #include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <errno.h>
char fn_c[] = "childq.out";
char fn_p[] = "parentside.out";
int fd_c;
int fd_p;
void
prep1 (void)
{
char buff[80] = "prep1\n";
write (4, buff, sizeof (buff));
}
void
prep2 (void)
{
char buff[80] = "prep2\n";
write (4, buff, sizeof (buff));
}
void
prep3 (void)
{
char buff[80] = "prep3\n";
write (4, buff, sizeof (buff));
}
void
parent1 (void)
{
char buff[80] = "parent1\n";
write (4, buff, sizeof (buff));
}
void
parent2 (void)
{
char buff[80] = "parent2\n";
write (4, buff, sizeof (buff));
}
void
parent3 (void)
{
char buff[80] = "parent3\n";
write (4, buff, sizeof (buff));
}
void
child1 (void)
{
char buff[80] = "child1\n";
write (3, buff, sizeof (buff));
}
void
child2 (void)
{
char buff[80] = "child2\n";
write (3, buff, sizeof (buff));
}
void
child3 (void)
{
char buff[80] = "child3\n";
write (3, buff, sizeof (buff));
}
void *
thread1 (void *arg)
{
printf ("Thread1: Hello from the thread.\n");
}
int
main (void)
{
pthread_t thid;
int rc, ret;
pid_t pid;
int status;
char header[30] = "Called Child Handlers\n";
if (pthread_create (&thid, NULL, thread1, NULL) != 0)
{
perror ("pthread_create() error");
exit (3);
}
if (pthread_join (thid, NULL) != 0)
{
perror ("pthread_join() error");
exit (5);
}
else
{
printf
("IPT: pthread_join success! Thread 1 should be finished now.\n");
printf ("IPT: Prepare to fork!!!\n");
}
/* Register call 1 */
rc = pthread_atfork (&prep1, &parent2, &child3);
if (rc != 0)
{
perror ("IPT: pthread_atfork() error [Call #1]");
}
/* Register call 2 */
rc = pthread_atfork (&prep2, &parent3, &child1);
if (rc != 0)
{
perror ("IPT: pthread_atfork() error [Call #2]");
}
/* Register call 3 */
rc = pthread_atfork (&prep3, &parent1, NULL);
if (rc != 0)
{
perror ("IPT: pthread_atfork() error [Call #3]");
}
/* Create output files to expose the execution of fork handlers. */
if ((fd_c = creat (fn_c, S_IWUSR)) < 0)
perror ("creat() error");
else
printf ("Created %s and assigned fd= %d\n", fn_c, fd_c);
if ((ret = write (fd_c, header, 30)) == -1)
perror ("write() error");
else
printf ("Write() wrote %d bytes in %s\n", ret, fn_c);
if ((fd_p = creat (fn_p, S_IWUSR)) < 0)
perror ("creat() error");
else
printf ("Created %s and assigned fd= %d\n", fn_p, fd_p);
if ((ret = write (fd_p, header, 30)) == -1)
perror ("write() error");
else
printf ("Write() wrote %d bytes in %s\n", ret, fn_p);
pid = fork ();
if (pid < 0)
perror ("IPT: fork() error");
else
{
if (pid == 0)
{
printf ("Child: I am the child!\n");
printf ("Child: My PID= %d, parent= %d\n", (int) getpid (),
(int) getppid ());
exit (0);
}
else
{
printf ("Parent: I am the parent!\n");
printf ("Parent: My PID= %d, child PID= %d\n", (int) getpid (),
(int) pid);
if (wait (&status) == -1)
perror ("Parent: wait() error");
else if (WIFEXITED (status))
printf ("Child exited with status: %d\n", WEXITSTATUS (status));
else
printf ("Child did not exit successfully\n");
close (fd_c);
close (fd_p);
}
}
}
[root@centosgpt threaddemo]# ./atfork
Thread1: Hello from the thread.
IPT: pthread_join success! Thread 1 should be finished now.
IPT: Prepare to fork!!!
Created childq.out and assigned fd= 3
Write() wrote 30 bytes in childq.out
Created parentside.out and assigned fd= 4
Write() wrote 30 bytes in parentside.out
Parent: I am the parent!
Parent: My PID= 62375, child PID= 62377
Child: I am the child!
Child: My PID= 62377, parent= 62375
Child exited with status: 0
[root@centosgpt threaddemo]# cat parentside.out
Called Child Handlers
prep3
prep2
prep1
parent2
parent3
parent1
[root@centosgpt threaddemo]# cat childq.out
Called Child Handlers
child3
child1
|