日期:2014-05-16 浏览次数:20631 次
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