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. Java String 对 null 对象的容错处理

    前言 最近在读<Thinking in Java>,看到这样一段话: Primitives that are fields in a class are automatically ini ...

  2. 数据流模型、Storm数据流模型

  3. Tuple元组

    Tuple元组 Tuple 是 Storm 的主要数据结构,并且是 Storm 中使用的最基本单元.数据模型和元组. Tuple 描述 Tuple 就是一个值列表, Tuple 中的值可以是任何类型的 ...

  4. 第一章 Windows NT System Components

    Page 3. The focus(焦点) of this book is Windows NT file system and the interaction(交互) of the file sys ...

  5. nyoj 540 奇怪的排序

    奇怪的排序 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 最近,Dr. Kong 新设计一个机器人Bill.这台机器人很聪明,会做许多事情.惟独对自然数的理解与人类 ...

  6. hdoj 2612 Find a way【bfs+队列】

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. 如何将中国知网CNKI中的文献导入EndNote X6

    如何将中国知网CNKI中的文献导入EndNote X6 下面给出具体步骤: 1.在CNKI中检索目标文献,如检索<基于Qt的三维可视化技术研究> 我喜欢在CNKI(http://www.c ...

  8. 点击modal确定键后删除tr

    做第一个笔记,关于 “书单”.2016-09-03关于一个表格调用modal后,在点击表格中的删除按钮弹出modal,点击确定删除后,将一整行tr 删除的功能. 以下内容为table,表示为某班学生. ...

  9. 【转】使用VisualSVN Server搭建SVN服务器

    http://blog.csdn.net/han_yankun2009/article/details/7856992 使用 VisualSVN Server来实现主要的 SVN功能则要比使用原始的  ...

  10. stack计算表达式的值

    9.52 使用stack对象处理带圆括号的表达式.遇到左圆括号时,将其标记下来.当你在一个左括号之后遇到右圆括号时,弹出stack对象中这两边括号之间的元素,直到遇到左括号,将左括号也一起弹出栈. 接 ...