管道是单向的、先进先出的,它把一个进程的输出和还有一个进程的输入连接在一起。一个进程(写进程)在管道的尾部写入数据,还有一个进程(读进程)从管道的头部读出数据。数据被一个进程读出后,将被从管道中删除,其它读进程将不能再读到这些数据。管道提供了简单的流控制机制,进程试图读空管道时,进程将堵塞。相同,管道已经满时,进程再试图向管道写入数据。进程将堵塞。

管道包含无名管道和有名管道两种。无名管道仅仅能用于父进程和子进程间的通信,而有名管道能够用于同一系统中的随意两个进程间的通信。

  • 无名管道由pipe()函数创建
int pipe(int pipefd[2]);

#当一个无名管道建立时,它会创建两个文件描写叙述符:
#pipefd[0] 用于读管道,
#pipefd[1] 用于写管道。 #pipe()函数创建的管道默认是打开的.

演示样例程序:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h> int main(int argc, char** argv){
int pipe_fd[2];
pid_t pid;
if( pipe(pipe_fd) == -1){
fprintf(stderr, "%s\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);
exit(-1);
}
if(( pid = fork()) == 0){
close(pipe_fd[0]);
write(pipe_fd[1],"hello ",6);
write(pipe_fd[1]," world",7); printf("Child process write done!\n");
close(pipe_fd[1]);
exit(0);
}else if( pid > 0){
close(pipe_fd[1]);
sleep(3);
char buf[20];
memset(buf,0,20);
read(pipe_fd[0],buf,20);
printf("Parent process in : %s\n",buf);
close(pipe_fd[0]);
} return 0;
} #对于无名管道的读、写、关闭操作,与普通文件的操作是一样的。
  • 有名管道的创建有两种方式

    1、使用shell命令,格式例如以下:
mkfifo [OPTION] FILENAME

2、在程序中调用mkfifo()函数

int mkfifo(const char *pathname, mode_t mode);

mkfifo()函数会根据參数 pathname 建立特殊的FIFO文件(有名管道),该文件必须不存在,而參数mode为该文件的权限。

mkfifo()建立的FIFO文件其它进程都能够用读写一般的文件方式存取。mkfifo()建立的FIFO文件默认是关闭的,当使用open()函数打开文件时须要注意和普通文件的差别:

1、不能以O_RDWR模式打开FIFO文件进行读写操作。这样做的行为是没有定义的。由于管道是单向的,假设须要在程序之间双向传递数据,使用一对有名管道就可以。

2、对标志位的 O_NONBLOCK 标志的使用方法。

O_RDONLY、O_WRONLY和O_NONBLOCK标志共同拥有四种合法的组合方式:

flags=O_RDONLY:open将会调用堵塞。除非有另外一个进程以写的方式打开同一个FIFO。否则一直等待。

flags=O_WRONLY:open将会调用堵塞。除非有另外一个进程以读的方式打开同一个FIFO。否则一直等待。

flags=O_RDONLY|O_NONBLOCK:假设此时没有其它进程以写的方式打开FIFO,此时open也会成功返回,此时FIFO被读打开,而不会返回错误。

flags=O_WRONLY|O_NONBLOCK:马上返回。假设此时没有其它进程以读的方式打开,open会失败打开。此时FIFO没有被打开。返回-1。

演示样例程序:

#fifo_read.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h> #define FIFO_FILE "/tmp/myfifo" int main(int argc, char *argv[]){
int fd ;
char buf[128];
if(access(FIFO_FILE,F_OK) != 0){
if(mkfifo(FIFO_FILE, 0644) == -1){
fprintf(stderr,"%s\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);
return -1;
}
}
if( (fd = open(FIFO_FILE,O_RDONLY | O_NONBLOCK)) == -1){
fprintf(stderr,"%s\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);
return -1;
} while(1){
memset(buf,0,sizeof(buf));
if(read(fd,buf,sizeof(buf)) == -1){
fprintf(stderr,"%s\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);
return -1;
}
sleep(1);
printf("read is %s !\n",buf);
} unlink(FIFO_FILE);
return 0;
}
# fifo_write.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h> #define FIFO_FILE "/tmp/myfifo" int main(int argc, char *argv[]){
int fd ; if( (fd = open(FIFO_FILE,O_WRONLY | O_NONBLOCK)) == -1){
fprintf(stderr,"%s\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);
return -1;
} if (write(fd,"Hello world",12) == -1){
fprintf(stderr,"%s\t%s\t%d\n",strerror(errno), __FILE__,__LINE__);
return -1;
} return 0;
}

最新文章

  1. 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed
  2. java 对象输入输出流
  3. 转:Eclipse快捷键 10个最有用的快捷键
  4. Python 中的数据结构总结(一)
  5. Hello World!
  6. EF 知识点
  7. RDIFramework.NET开发实例━表约束条件权限的使用-Web
  8. HTML输出 二 控制行背景颜色
  9. C语言自动类型转换
  10. JavaScript闭包的一些理解
  11. 利用Node的chokidar 监听文件改变的文件。
  12. 深入浅出理解python 装饰器
  13. Testlink与MantisBT集成
  14. Alienware R8外星人台式机安装双系统(WIN10+Ubuntu)的总结
  15. Android联系人列表实现
  16. prufer序列学习笔记
  17. Python基础二字符串和变量
  18. 【float】与【position】汇总
  19. webrtc在ubuntu14.04上的编译过程(12.04亦可)
  20. javascript练习(二)

热门文章

  1. legend---一、如何实现js跳转到php页面
  2. WebServic调用天气预报服务
  3. WISP &gt; Client+AP &gt; WDS  的区别
  4. ios程序启动过程和UIWidnow介绍
  5. RHEL7.1安装VNC
  6. 自写的开发框架,胜于官方的clientAPP的实战开发。(已开源)
  7. js 图片轮转
  8. 配置Xcode版本控制SVN详细步骤内含解决Xcode/Mac OS10.8无法配置SVN的解决方法
  9. 迷茫了好一阵决定做WEB前端
  10. 漫漫人生路-学点Jakarta基础-Java8新特性 Stream/Lambda