pipe.h
1 #ifndef _PIPE_H
2 #define _PIPE_H
3
4 int pipe_init(void); //初始化管道
5 int pipe_write_to_parent(void *buf, int len); //向父进程写数据
6 int pipe_write_to_child(void *buf, int len); //向子进程写数据
7 int pipe_read_from_parent(void *buf, int len); //读父进程数据
8 int pipe_read_from_child(void *buf, int len); //读子进程数据
9 void pipe_childend_close(void); //关闭子进程相应读、写端
10 void pipe_parentend_close(void); //关闭父进程相应读、写端
11 void pipe_free(void); //释放管道
12 #endif
pipe.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/ioctl.h>
5 #include <errno.h>
6 #include <sys/types.h>
7
8 #include "pipe.h"
9
10 #define PIPE_READ_END 0 //预定义读端
11 #define PIPE_WRITE_END 1 //预定义写端
12
13 static int pipe_fd1[2]; //父进程向子进程,即读父进程、写子进程
14 static int pipe_fd2[2]; //子进程向父进程,即读子进程、写父进程
15 //创建管道
16 int pipe_init(void)
17 {
18 if (pipe(pipe_fd1) < 0 || pipe(pipe_fd2) < 0) {
19 return -1;
20 }
21
22 return 0;
23 }
24 //子进程向父进程写数据
25 int pipe_write_to_parent(void *buf, int len)
26 {
27 if (pipe_fd2[PIPE_WRITE_END] < 0 || buf == NULL) {
28 return -1;
29 }
30
31 if (write(pipe_fd2[PIPE_WRITE_END], buf, len) != len) {
32 return -1;
33 }
34
35 return 0;
36 }
37 //父进程向子进程写数据
38 int pipe_write_to_child(void *buf, int len)
39 {
40 if (pipe_fd1[PIPE_WRITE_END] < 0 || buf == NULL) {
41 return -1;
42 }
43
44 if (write(pipe_fd1[PIPE_WRITE_END], buf, len) != len) {
45 return -1;
46 }
47
48 return 0;
49 }
50 //读父进程管道pipe_fd1[PIPE_READ_END],并判断是否为指定长度len
51 int pipe_read_from_parent(void *buf, int len)
52 {
53 int rlen;
54 int ret;
55
56 if (pipe_fd1[PIPE_READ_END] < 0 || buf == NULL) {
57 return -1;
58 }
59
60 ioctl(pipe_fd1[PIPE_READ_END], FIONREAD, &rlen);
61 if (rlen < len) {
62 return -1;
63 } else {
64 ret = read(pipe_fd1[PIPE_READ_END], buf, len);
65 if (ret < 0) {
66 return -1;
67 }
68 }
69
70 return 0;
71
72 }
73 //读子进程管道pipe_fd2[PIPE_READ_END],并判断是否为指定长度len
74 int pipe_read_from_child(void *buf, int len)
75 {
76 int rlen;
77 int ret;
78
79 if (pipe_fd2[PIPE_READ_END] < 0 || buf == NULL) {
80 return -1;
81 }
82
83 ioctl(pipe_fd2[PIPE_READ_END], FIONREAD, &rlen);
84 if (rlen < len) {
85 return -1;
86 } else {
87 ret = read(pipe_fd2[PIPE_READ_END], buf, len);
88 if (ret < 0) {
89 return -1;
90 }
91 }
92
93 return 0;
94
95 }
96 //关闭子进程写端pipe_fd1[PIPE_WRITE_END],子进程读端pipe_fd2[PIPE_READ_END].
97 void pipe_childend_close(void)
98 {
99 if (pipe_fd1[PIPE_WRITE_END] > 0) {
100 close(pipe_fd1[PIPE_WRITE_END]);
101 pipe_fd1[PIPE_WRITE_END] = -1;
102 }
103
104 if (pipe_fd2[PIPE_READ_END] > 0) {
105 close(pipe_fd2[PIPE_READ_END]);
106 pipe_fd2[PIPE_READ_END] = -1;
107 }
108
109 }
110 //关闭父进程读端pipe_fd1[PIPE_READ_END],父进程写端pipe_fd2[PIPE_WRITE_END]
111 void pipe_parentend_close(void)
112 {
113 if (pipe_fd1[PIPE_READ_END] > 0) {
114 close(pipe_fd1[PIPE_READ_END]);
115 pipe_fd1[PIPE_READ_END] = -1;
116 }
117
118 if (pipe_fd2[PIPE_WRITE_END] > 0) {
119 close(pipe_fd2[PIPE_WRITE_END]);
120 pipe_fd2[PIPE_WRITE_END] = -1;
121 }
122
123 }
124 //关闭父子进程管道pipe_fd1,pipe_fd2
125 void pipe_free(void)
126 {
127 if (pipe_fd1[PIPE_READ_END] > 0) {
128 close(pipe_fd1[PIPE_READ_END]);
129 pipe_fd1[PIPE_READ_END] = -1;
130 }
131
132 if (pipe_fd1[PIPE_WRITE_END] > 0) {
133 close(pipe_fd1[PIPE_WRITE_END]);
134 pipe_fd1[PIPE_WRITE_END] = -1;
135 }
136
137 if (pipe_fd2[PIPE_READ_END] > 0) {
138 close(pipe_fd2[PIPE_READ_END]);
139 pipe_fd2[PIPE_READ_END] = -1;
140 }
141
142 if (pipe_fd2[PIPE_WRITE_END] > 0) {
143 close(pipe_fd2[PIPE_WRITE_END]);
144 pipe_fd2[PIPE_WRITE_END] = -1;
145 }
146 }