《Unix网络编程》这本书附带了许多短小精美的小程序,我在阅读此书的时候,将书上的代码按照自己的理解重写了一遍(大部分是抄书上的),加深一下自己的理解(纯看书太困了,呵呵)。此例子在Ubuntu10.04上测试通过。
PS:程序里使用了包裹函数(首字母是大写的函数)和常量(所有字母都是大写的常量)的声明在my_unp.h文件中,定义在unp_base.c和unp_thread.c中,地址:http://blog.csdn.net/aaa20090987/article/details/8096701
程序简介:这个程序演示了父子进程之间如果利用管道技术进行通信。
上代码:
- #include "my_unp.h"
- void client(int readfd, int writefd)
- {
- size_t len;
- ssize_t n;
- char buff[MAXLINE];
- //从终端读入一行数据(文件路径)
- Fgets(buff, MAXLINE, stdin);
- len = strlen(buff);
- if( '\n' == buff[len-1] )
- len--;
- //将其写入管道
- Write(writefd, buff, len);
- //然后从管道中读出数据
- while( (n=Read(readfd, buff, MAXLINE)) > 0 )
- Write(STDOUT_FILENO, buff, n);
- }
- void server(int readfd, int writefd)
- {
- int fd;
- ssize_t n;
- char buff[MAXLINE];
- //从管道中读出一行数据(文件路经)
- if( (n=Read(readfd, buff, MAXLINE)) == 0 )
- error_quit("end of file while reading pathname");
- buff[n] = '\0';
- //打开文件
- fd = open(buff, O_RDONLY);
- //如果打开失败,就将失败原因写入管道
- if( fd < 0 )
- {
- snprintf(buff+n, sizeof(buff)-n, ": can't open, %s\n",
- strerror(errno));
- n = strlen(buff);
- Write(writefd, buff, n);
- }
- //否则,将文件内容写入管道
- else
- {
- while( (n=Read(fd, buff, MAXLINE)) > 0 )
- Write(writefd, buff, n);
- Close(fd);
- }
- }
- int main(int argc, char **argv)
- {
- int pipe1[2], pipe2[2];
- pid_t childpid;
- Pipe(pipe1);
- pipe(pipe2);
- childpid = Fork();
- //父子进程分别关闭自己不需要的管道端口
- if( childpid == 0 )
- {
- Close(pipe1[1]);
- Close(pipe2[0]);
- server(pipe1[0], pipe2[1]);
- }
- else
- {
- Close(pipe1[0]);
- Close(pipe2[1]);
- client(pipe2[0], pipe1[1]);
- //父进程必须等子进程退出之后才能退出
- Waitpid(childpid, NULL, 0);
- }
- return 0;
- }
运行示例(红色字体的为输入)