Unix IPC之pipe
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的更多相关文章
- 进程通信类型 管道是Linux支持的最初Unix IPC形式之一
管道 Linux环境进程间通信(一) https://www.ibm.com/developerworks/cn/linux/l-ipc/part1/index.html 管道及有名管道 郑彦兴200 ...
- Linux 系统编程 学习:02-进程间通信1:Unix IPC(1)管道
Linux 系统编程 学习:02-进程间通信1:Unix IPC(1)管道 背景 上一讲我们介绍了创建子进程的方式.我们都知道,创建子进程是为了与父进程协作(或者是为了执行新的程序,参考 Linux ...
- Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号
Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号 背景 上一讲我们介绍了Unix IPC中的2种管道. 回顾一下上一讲的介绍,IPC的方式通常有: Unix IPC包括:管道 ...
- 进程通信类型 管道是Linux支持的最初Unix IPC形式之一 命名管道 匿名管道
管道 Linux环境进程间通信(一) https://www.ibm.com/developerworks/cn/linux/l-ipc/part1/index.html 管道及有名管道 郑彦兴200 ...
- c/c++ unix ipc
c/c++ unix ipc 一个例子 //c_unix.c #include <stdio.h> #include <sys/types.h> #include <sy ...
- Linux IPC BSD Pipe
mkfifo() //创建有名管道(FIFO special file),创建完了就像普通文件一样open(),再读写,成功返回0,失败返回-1设errno.VS$man 3 mkfifo #incl ...
- Unix IPC之Posix消息队列(1)
部分参考:http://www.cnblogs.com/Anker/archive/2013/01/04/2843832.html IPC对象的持续性:http://book.51cto.com/ar ...
- UNIX IPC: POSIX 消息队列 与 信号
POSIX消息队列可以注册空队列有消息到达时所触发的信号,而信号触发对应的信号处理函数. 下面是一份基本的消息队列和信号处理结合的代码(修改自UNIX网络编程:进程间通信) #include < ...
- linux IPC的PIPE
一.PIPE(无名管道) 函数原型: #include <unistd.h> ]); 通常,进程会先调用pipe,接着调用fork,从而创建从父进程到子进程的IPC通道. 父进程和子进程之 ...
随机推荐
- Html 响应式 Web
网格视图 很多网页都是基于网格设计的,这说明网页是按列来布局的. 使用网格视图有助于我们设计网页.这让我们向网页添加元素变的更简单. 响应式网格视图通常是 12 列,宽度为100%,在浏览器窗口大小调 ...
- activiti教程之示例项目activiti-explorer运行_百度经验
https://jingyan.baidu.com/article/4e5b3e19107ad091901e249e.html
- 网络中,FIFO、LRU、OPT这三种置换算法的缺页次数
FIFO.LRU.OPT这三种置换算法的缺页次数 转载 由于要考计算机四级网络,这里遇到了问题,就搜了一些资料来解疑. 考虑下述页面走向: 1,2,3,4,2,1,5,6,2,1,2,3,7,6,3 ...
- 1044 Shopping in Mars
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...
- 编译geth报错的解决方法 make: *** [geth] 错误 1
在centos下安装了go1.9.1版本,编译go-ethereum时报错: [root@localhost go-ethereum]# make gethbuild/env.sh go run bu ...
- 2 Kafka Broker
Log的读写.删除流程---日志管理器(log manager)负责创建日志.获取日志.清理日志.所有的日志读写操作都交给具体的日志实例来完成. KafkaServer启动的时候,初始化三个类: Lo ...
- 快速搭建Spring Boot项目
Spring boot是Spring推出的一个轻量化web框架,主要解决了Spring对于小型项目饱受诟病的配置和开发速度问题. Spring Boot 包含的特性如下: 创建可以独立运行的 Spri ...
- Spring Resource 类图
插播个广告 老丈人家的粉皮儿,农产品,没有乱七八糟的添加剂,欢迎惠顾
- bzoj千题计划140:bzoj4519: [Cqoi2016]不同的最小割
http://www.lydsy.com/JudgeOnline/problem.php?id=4519 最小割树 #include<queue> #include<cstdio&g ...
- 洛谷 P3382 【模板】三分法
https://www.luogu.org/problem/show?pid=3382 题目描述 如题,给出一个N次函数,保证在范围[l,r]内存在一点x,使得[l,x]上单调增,[x,r]上单调减. ...