pipe创建函数:

#include <unistd.h>

/* Create a one-way communication channel (pipe).
If successful, two file descriptors are stored in PIPEDES;
bytes written on PIPEDES[1] can be read from PIPEDES[0].
Returns 0 if successful, -1 if not. */
int pipe (int __pipedes[]);

下面是一个测试实例,有些未说明函数,如Pipe,是pipe的一个包裹函数,二者参数及用法一致,仅仅是在包裹函数中添加了出错信息。

void client(int, int);
void server(int, int); int
main(int argc, char **argv)
{
int pipe1[], pipe2[];
pid_t childpid; Pipe(pipe1); /* create two pipes */
Pipe(pipe2); if ( (childpid = Fork()) == ) /* child */
{
Close(pipe1[]);
Close(pipe2[]); server(pipe1[], pipe2[]); // 子线程作为服务器端
exit();
}
/* 4parent */
Close(pipe1[]);
Close(pipe2[]); client(pipe2[], pipe1[]); Waitpid(childpid, NULL, ); /* wait for child to terminate */
exit();
}
/**
* 从客户端读取文件名,打开文件并将文件内容返回给客户端
* @param readfd 管道读描述符
* @param writefd 管道写描述符
*/
void server(int readfd, int writefd)
{
int fd;
ssize_t n;
char buff[MAXLINE + ]; /* 4read pathname from IPC channel */
if ( (n = Read(readfd, buff, MAXLINE)) == )
err_quit("end-of-file while reading pathname"); buff[n] = '\0'; /* null terminate pathname */ if ( (fd = open(buff, O_RDONLY)) < )
{
/* 4error: must tell client */
// 打开失败则直接将错误信息写入管道
snprintf(buff + n, sizeof(buff) - n, ": can't open, %s\n", strerror(errno));
n = strlen(buff);
Write(writefd, buff, n); }
else
{
/* 4open succeeded: copy file to IPC channel */
while ( (n = Read(fd, buff, MAXLINE)) > )
Write(writefd, buff, n);
Close(fd);
}
} /**
* 将键盘输入数据发送给服务器,并将服务器的回复信息显示在屏幕
* @param readfd 管道读描述符
* @param writefd 管道写描述符
*/
void client(int readfd, int writefd)
{
size_t len;
ssize_t n;
char buff[MAXLINE]; /* 4read pathname */
Fgets(buff, MAXLINE, stdin); // 从键盘获取输入信息
len = strlen(buff); /* fgets() guarantees null byte at end */
if (buff[len - ] == '\n')
len--; /* delete newline from fgets() */ /* 4write pathname to IPC channel */
Write(writefd, buff, len); // 写入管道 /* 4read from IPC, write to standard output */
while ( (n = Read(readfd, buff, MAXLINE)) > )
Write(STDOUT_FILENO, buff, n);
}

以上测试实例实现了一个如下的客户-服务器:

Unix IPC之pipe的更多相关文章

  1. 进程通信类型 管道是Linux支持的最初Unix IPC形式之一

    管道 Linux环境进程间通信(一) https://www.ibm.com/developerworks/cn/linux/l-ipc/part1/index.html 管道及有名管道 郑彦兴200 ...

  2. Linux 系统编程 学习:02-进程间通信1:Unix IPC(1)管道

    Linux 系统编程 学习:02-进程间通信1:Unix IPC(1)管道 背景 上一讲我们介绍了创建子进程的方式.我们都知道,创建子进程是为了与父进程协作(或者是为了执行新的程序,参考 Linux ...

  3. Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号

    Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号 背景 上一讲我们介绍了Unix IPC中的2种管道. 回顾一下上一讲的介绍,IPC的方式通常有: Unix IPC包括:管道 ...

  4. 进程通信类型 管道是Linux支持的最初Unix IPC形式之一 命名管道 匿名管道

    管道 Linux环境进程间通信(一) https://www.ibm.com/developerworks/cn/linux/l-ipc/part1/index.html 管道及有名管道 郑彦兴200 ...

  5. c/c++ unix ipc

    c/c++ unix ipc 一个例子 //c_unix.c #include <stdio.h> #include <sys/types.h> #include <sy ...

  6. Linux IPC BSD Pipe

    mkfifo() //创建有名管道(FIFO special file),创建完了就像普通文件一样open(),再读写,成功返回0,失败返回-1设errno.VS$man 3 mkfifo #incl ...

  7. Unix IPC之Posix消息队列(1)

    部分参考:http://www.cnblogs.com/Anker/archive/2013/01/04/2843832.html IPC对象的持续性:http://book.51cto.com/ar ...

  8. UNIX IPC: POSIX 消息队列 与 信号

    POSIX消息队列可以注册空队列有消息到达时所触发的信号,而信号触发对应的信号处理函数. 下面是一份基本的消息队列和信号处理结合的代码(修改自UNIX网络编程:进程间通信) #include < ...

  9. linux IPC的PIPE

    一.PIPE(无名管道) 函数原型: #include <unistd.h> ]); 通常,进程会先调用pipe,接着调用fork,从而创建从父进程到子进程的IPC通道. 父进程和子进程之 ...

随机推荐

  1. Jenkins(二)---jenkins之Git+maven+jdk+tomcat

    git+maven+jdk+tomcat  这四个软件可以百度在linux下的安装,不再赘述. server A --->   jenkins主机ip:192.168.100.119 serve ...

  2. BAYES和朴素BAYES

    0 前言  朴素贝叶斯算法仍然是流行的十大挖掘算法之一,该算法是有监督的学习算法,解决的是分类问题,如客户是否流失.是否值得投资.信用等级评定等多分类问题.该算法的优点在于简单易懂.学习效率高.在某些 ...

  3. RF parameter

    There are primarily 3 features which can be tuned to improve the predictive power of the model : 说明: ...

  4. HDU 3943 数位dp+二分

    K-th Nya Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) ...

  5. GO_03:GO语言基础语法

    1. Go项目的目录结构 一般的,一个Go项目在GOPATH下,会有如下三个目录: project   --- bin   --- pkg   --- src 其中,bin 存放编译后的可执行文件:p ...

  6. asp.net获取当前页面的url地址

    设当前页完整地址是:http://www.jb51.net/aaa/bbb.aspx?id=5&name=kelli "http://"是协议名 "www.jb5 ...

  7. python操作mongo脚本

    #!/usr/bin/python# -*- coding: utf-8 -*- import sysimport osimport jsonfrom pymongo import MongoClie ...

  8. c# lock的误解

    一直以为lock 一个实例就可以了,没想到实例的类型还是有区别的 static object lockObjStatic = new object(); object lockObj = new ob ...

  9. Docker查看映射卷报错

    问题描述: 当查看Docker容器的映射卷时出现报错信息,如下: [root@kazihuo ~]# docker inspect -f {{.Volumes}} volume   #volume指容 ...

  10. 漂亮!Javascript代码模仿淘宝宝贝搜索结果的分页显示效果

    分页按钮思想: 1.少于9页,全部显示 2.大于9页,1.2页显示,中间页码当前页为中心,前后各留两个页码 先看效果图: 01输入框焦点效果 02效果 模仿淘宝的分页按钮效果控件kkpager  JS ...