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. 2017 3 11 分治FFT

    考试一道题的递推式为$$f[i]=\sum_{j=1}^{i} j^k \times (i-1)! \times \frac{f[i-j]}{(i-j)!}$$这显然是一个卷积的形式,但$f$需要由自 ...

  2. R读取excel文件

    2017.09.05 我一个下午的成果啊啊啊啊,看看失败 不禁感叹一声,失败的路真是多啊!!!! 一.安装xlsx包 下面具体讲一讲怎么弄的(太笨了,所以学得慢,需要一步一步的来) 用R读取excel ...

  3. google analysis教程

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  4. 对faster rcnn 中rpn层的理解

    1.介绍 图为faster rcnn的rpn层,接自conv5-3 图为faster rcnn 论文中关于RPN层的结构示意图 2 关于anchor: 一般是在最末层的 feature map 上再用 ...

  5. 转:实现OC与JS的简易交互

    oc-->js stringByEvaluatingJavaScriptFromString,其参数是一NSString 字符串内容是js代码(这又可以是一个js函数.一句js代码或他们的组合) ...

  6. 使用object_box遇到的崩溃 java.lang.UnsatisfiedLinkError:

    java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/ ...

  7. CSS只改变背景透明度,不改变子元素透明度

    一般情况下,我们可以使用css的opcity属性改变某个元素的透明度,但是其元素下的子元素的透明度也会被改变,即使对子元素重新定义也没有用,例如: <div style="opacit ...

  8. [csp-201403-3]命令行选项

    #include<bits/stdc++.h> //#include <sstream> // if want to use stringstream using namesp ...

  9. Ubuntu: HDF5报错: HDF5 header version与HDF5 library不匹配

    今天在执行一个用到hdf5的python脚本时,遇到如下错误 Warning! ***HDF5 library version mismatched error*** The HDF5 header ...

  10. node.js 基础篇

    日志输出方式 node test.js 2>error.log 1>info.log 如果需要日志文件追加 node test.js 2>>error.log 1>> ...