pthread_atfork 注册fork的函数
实例
#define _UNIX03_THREADS 1
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/wait.h>
9 #include <stdlib.h>
10 #include <errno.h>
11
12 char fn_c[] = "childq.out";
13 char fn_p[] = "parentside.out";
14 int fd_c;
15 int fd_p;
16
17 void prep1(void) {
18 char buff[80] = "prep1\n";
19 write(4,buff,sizeof(buff));
20 }
21
22 void prep2(void) {
23 char buff[80] = "prep2\n";
24 write(4,buff,sizeof(buff));
25 }
26
27 void prep3(void) {
28 char buff[80] = "prep3\n";
29 write(4,buff,sizeof(buff));
30 }
31
32
33 void parent1(void) {
34 char buff[80] = "parent1\n";
35 write(4,buff,sizeof(buff));
36 }
37
38 void parent2(void) {
39 char buff[80] = "parent2\n";
40 write(4,buff,sizeof(buff));
41 }
42
43 void parent3(void) {
44 char buff[80] = "parent3\n";
45 write(4,buff,sizeof(buff));
46 }
47
48
49 void child1(void) {
50 char buff[80] = "child1\n";
51 write(3,buff,sizeof(buff));
52 }
53
54 void child2(void) {
55 char buff[80] = "child2\n";
56 write(3,buff,sizeof(buff));
57 }
58
59 void child3(void) {
60 char buff[80] = "child3\n";
61 write(3,buff,sizeof(buff));
62 }
63
64 void *thread1(void *arg) {
65
66 printf("Thread1: Hello from the thread.\n");
67
68 }
69
70
71 int main(void)
72 {
73 pthread_t thid;
74 int rc, ret;
75 pid_t pid;
76 int status;
77 char header[30] = "Called Child Handlers\n";
78
79
80 if (pthread_create(&thid, NULL, thread1, NULL) != 0) {
81 perror("pthread_create() error");
82 exit(3);
83 }
84
85 if (pthread_join(thid, NULL) != 0) {
86 perror("pthread_join() error");
87 exit(5);
88 } else {
89 printf("IPT: pthread_join success! Thread 1 should be finished now.\n");
90 printf("IPT: Prepare to fork!!!\n");
91 }
92
93
94 /*-----------------------------------------*/
95 /*| Start atfork handler calls in parent */
96 /*-----------------------------------------*/
97 /* Register call 1 */
98 rc = pthread_atfork(&prep1, &parent2, &child3);
99 /* if (rc != 0) {
100 perror("IPT: pthread_atfork() error [Call #1]");
101 printf(" rc= %d, errno: %d, ejr: %08x\n", rc, errno, __errno2());
102 }*/
103
104
105 /* Register call 2 */
106 rc = pthread_atfork(&prep2, &parent3, &child1);
107 /*if (rc != 0) {
108 perror("IPT: pthread_atfork() error [Call #2]");
109 printf(" rc= %d, errno: %d, ejr: %08x\n", rc, errno, __errno2());
}*/
111 /* Register call 3 */
112 rc = pthread_atfork(&prep3, &parent1, NULL);
113 /*if (rc != 0) {
114 perror("IPT: pthread_atfork() error [Call #3]");
115 printf(" rc= %d, errno: %d, ejr: %08x\n", rc, errno, __errno2());
116 }*/
117 /* Create output files to expose the execution of fork handlers. */
118 if ((fd_c = creat(fn_c, S_IWUSR)) < 0)
119 perror("creat() error");
120 else
121 printf("Created %s and assigned fd= %d\n", fn_c, fd_c);
122 if ((ret = write(fd_c,header,30)) == -1)
123 perror("write() error");
124 else
125 printf("Write() wrote %d bytes in %s\n", ret, fn_c);
126 if ((fd_p = creat(fn_p, S_IWUSR)) < 0)
127 perror("creat() error");
128 else
129 printf("Created %s and assigned fd= %d\n", fn_p, fd_p);
130 if ((ret = write(fd_p,header,30)) == -1)
131 perror("write() error");
132 else
133 printf("Write() wrote %d bytes in %s\n", ret, fn_p);
134 pid = fork();
135 if (pid < 0)
136 perror("IPT: fork() error");
137 else {
138 if (pid == 0) {
139 printf("Child: I am the child!\n");
140 printf("Child: My PID= %d, parent= %d\n", (int)getpid(),
141 (int)getppid());
142 exit(0);
143 } else {
144 printf("Parent: I am the parent!\n");
145 printf("Parent: My PID= %d, child PID= %d\n", (int)getpid(), (int)pid);
146 if (wait(&status) == -1)
147 perror("Parent: wait() error");
148 else if (WIFEXITED(status))
149 printf("Child exited with status: %d\n",WEXITSTATUS(status));
150 else
151 printf("Child did not exit successfully\n");
152 close(fd_c);
153 close(fd_p);
154 }
155 }
156 }
编译 gcc -o pthread_atfork -pthread pthread_atfork.c
这里注意 -pthread 而不是-lpthread 若是后者则会报错 pthread_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
Child: I am the child!
Child: My PID= 2616, parent= 2614
Parent: I am the parent!
Parent: My PID= 2614, child PID= 2616
Child exited with status: 0