4.1 概述

管道只在亲缘进程间使用,FIFO在任意进程间使用

4.2 管道

#include <unistd.h>
int pipe(int fd[])

fd[0]用来读管道,fd[1]用来写管道

1)命令who | sort | lp中的管道:

2)管道实现文件服务器与客户端:

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <error.h> #define MAXLINE 1024 void client(int readfd,int writefd);
void server(int readfd,int writefd); int main()
{
int fd1[];
int fd2[];
pipe(fd1);
pipe(fd2);
int pid;
if( (pid = fork()) < )
{
fprintf(stderr,"fork error\n");
exit(-);
}
if(pid > )
{
close(fd1[]);
close(fd2[]);
server(fd1[],fd2[]);
exit();
}
close(fd1[]);
close(fd2[]);
client(fd2[],fd1[]);
waitpid(pid,NULL,);
exit();
} void client(int readfd,int writefd)
{
size_t len;
char buf[MAXLINE];
fgets(buf,MAXLINE,stdin);
len = strlen(buf);
if(buf[len-] == '\n')
--len;
write(writefd,buf,len);
while( (len = read(readfd,buf,MAXLINE)) > )
write(STDOUT_FILENO,buf,len);
} void server(int readfd,int writefd)
{
char buf[MAXLINE];
ssize_t n;
if( (n = read(readfd,buf,MAXLINE)) ==)
{
fprintf(stderr,"error\n");
exit(-);
}
buf[n] = '\0';
int fd;
if( (fd = open(buf,O_RDONLY)) < )
{
snprintf(buf+n,sizeof(buf)-n,"can't open: %s\n",strerror(errno));
n = strlen(buf);
write(writefd,buf,n);
}
else
while( (n = read(fd,buf,MAXLINE)) > )
write(writefd,buf,n);
close(fd);
}

4.3 popen和pclose函数

#include <stdio.h>
FILE *popen(char *cmd,char *type)
int pclose(FILE *fp)

popen函数创建另外一个进程执行cmd,并在调用进程与创建进程之间建立一个单向管道,管道的一端与返回的FILE对象绑定

type为"w",FILE对象与管道的写端绑定,cmd的标准输入为管道的读端

type为"r",FILE对象与管道的读端绑定,cmd的标准输出为管道的写端

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINE 1024
int main()
{
char buf[MAXLINE];
fgets(buf,MAXLINE,stdin);
int n = strlen(buf);
if(buf[n-] == '\n')
--n;
char cmd[MAXLINE];
snprintf(cmd,MAXLINE,"cat %s",buf);
FILE *fp = popen(cmd,"r");
while(fgets(buf,MAXLINE,fp) != NULL)
fputs(buf,stdout);
exit();
}

4.4 FIFO

FIFO又称命名管道

#include <sys/stat.h>
#include <sys/types.h>
int mkfifo(char *pathname,mode_t mode)

FIFO实现文件服务器和客户端

#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h> int main(int argc,char *argv[])
{
if(mkfifo("./fifo1",S_IRUSR|S_IWUSR) < )
{
fprintf(stderr,"mkfifo error\n");
exit(-);
}
if(mkfifo("./fifo2",S_IRUSR|S_IWUSR) < )
{
fprintf(stderr,"mkfifo error\n");
exit(-);
}
int pid;
if( (pid = fork()) < )
{
fprintf(stderr,"fork error\n");
exit(-);
}
if(pid == )
{
int fd1 = open("./fifo1",O_RDONLY);
int fd2 = open("./fifo2",O_WRONLY);
server(fd1,fd2);
exit();
}
int fd1 = open("./fifo1",O_WRONLY);
int fd2 = open("./fifo2",O_RDONLY);
client(fd2,fd1);
waitpid(pid,NULL,);
exit();
}

4.5 管道、FIFO的阻塞与非阻塞

第4章 管道与FIFO的更多相关文章

  1. 第4章 管道和FIFO

    4.1 管道 管道是由pipe函数创建的,提供一个单向数据流. 头文件 #include <unistd.h> 函数原型 int pipe(int fd[2]); 返回值 成功则为0,出错 ...

  2. 第四章:管道与FIFO

    4.1:概述 管道是最初的Unix IPC形式,可追溯到1973年的Unix第三版.尽管对于许多操作来说很有用,但它们的根本局限在于没有名字,从而只能由亲缘关系的进程使用.这一点随FIFO的加入得改正 ...

  3. 管道和FIFO 一

    管道和FIFO   管道(pipe)       管道在Unix及Linux进程间通信是最基础的,很容易理解.管道就像一个自来水管,一端注入水,一端放出水,水只能在一个方向上流动,而不能双向流动.管道 ...

  4. linux进程间通信-有名管道(FIFO)

    有名管道(FIFO) 命名管道也被称为FIFO文件,是一种特殊的文件.由于linux所有的事物都可以被视为文件,所以对命名管道的使用也就变得与文件操作非常统一. (1)创建命名管道 用如下两个函数中的 ...

  5. Linux系统编程——进程间通信:命名管道(FIFO)

    命名管道的概述 无名管道,因为没有名字,仅仅能用于亲缘关系的进程间通信(很多其它详情.请看<无名管道>).为了克服这个缺点.提出了命名管道(FIFO).也叫有名管道.FIFO 文件. 命名 ...

  6. [转] IPC之管道、FIFO、socketpair

    管道和FIFO作为最初的UNIX IPC形式,现在已用得较少.SocketPair可作为全双工版本的管道,较为常用,这里简单做个笔记 管道 * 只用于有亲缘关系的进程间通信 * 单向,即半双工 (双向 ...

  7. linux 有名管道(FIFO)

    http://blog.csdn.net/firefoxbug/article/details/8137762 linux 有名管道(FIFO) 管道的缓冲区是有限的(管道制存在于内存中,在管道创建时 ...

  8. UNIX环境高级编程——管道和FIFO限制

    系统加于管道和FIFO的唯一限制为: OPEN_MAX     一个进程在任意时刻打开的最大描述符数: PIPE_BUF       可原子的写往一个管道或FIFO的最大数据量. OPEN_MAX的值 ...

  9. UNIX环境高级编程——管道和FIFO的额外属性

    一个描述符能以两种方式设置成非阻塞. (1)调用open时可以指定O_NONBLOCK标志. writefd = open(FIFO1,O_WRONLY | O_NONBLOCK,0); (2)如果一 ...

随机推荐

  1. HW4.6

    public class Solution { public static void main(String[] args) { final double MILES_PER_KILOGRAM = 1 ...

  2. 计算机网络协议包头赏析-UDP

    之前我们已经针对以太网.IP.TCP协议,进行了包头赏析.本次,我们继续UDP协议包头赏析. 提到TCP,想必大家会有所了解,它早已是家喻户晓的一个网络协议了,而UDP远没有他的大哥那么的有名,所以, ...

  3. iOS开发中使用静态库 .a 文件

    ​​iOS开发中,在使用一些第三方库时,可能是一个静态库(比如GPUImage).这种情况下,需要编译出静态库文件(.a) ,然后配合响应的头文件(.h 文件)使用. 编译静态库,直接在Xcode中编 ...

  4. PowerDesigner实用技巧小结(2)

    PowerDesigner实用技巧小结 1.ORACLE数据库建模时,由于ORACLE的表名.字段名如果是小写会有一定的麻烦,需要将小写转化为大写? (1)在打开pdm的情况下,进入Tools-Mod ...

  5. bootstrap-table对前台页面表格的支持

    1.bootstrap-table是在bootstrap的基础上面做了一些封装,所以在使用bootstrap-table之前要导入的js和css有 1)基本的还是jQuery <script t ...

  6. BAPI

    MM模块 1. BAPI_MATERIAL_SAVEDATA 创建物料主数据 注意参数EXTENSIONIN的使用,可以创建自定义字段 例如:WA_BAPI_TE_MARA-MATERIAL = IT ...

  7. C# FileStream复制大文件

    即每次复制文件的一小段,以节省总内存开销.当然,本机复制也可以采用.NET内部的System.IO.File.Copy方法. 本文转载:http://www.cnblogs.com/wolf-sun/ ...

  8. 【17】以独立语句将newed对象置入智能指针

    1.为什么? 考虑下面的情况:方法声明为void processWidget(shared_ptr<Widget> pw,int priority). 调用方法 processWidget ...

  9. crm操作权限

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Xrm.Sdk.Query;     using System.Colle ...

  10. CameraTest

    from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage #device = MonkeyRunner.wa ...