有名管道

无名管道和有名管道:

1. 管道是特殊类型的文件,在满足先入先出的原则写可以读写,不能定位读写位置。

2.管道是单向的。

3.无名管道阻塞于读写位置,而有名管道阻塞在创建位置

4.无名管道一般只用于亲缘关系进程间通信;有名管道以磁盘文件的方式存在,可以实现本机任意两进程间通信。

shell创建有名管道

mknod 管道名 p  //创建名为PIPETEST的有名管道 mknod为命令 p是参数,表示有名管道

指令 > 管道名 &   //将指令结果输入到到管道文件中

指令 < 管道名      //以指定管道名中的内容做为输入

编程实现有名管道

int mkfifo (__const char *__path, __mode_t __mode):建立的有名管道文件必须之前不存在, mode是该文件的权限。成功返回0,否则返回-1.

通过read(), write()实现读写操作。

其阻塞方式见下图(我懒得打字了,就上图吧....)

例子:

写进程

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/stat.h> #define FIFO_NAME "/tmp/my_fifo" //要创建的有名管道的路径
int main(int argc, char * argv[])
{
int pipe_fd;
int res;
char buffer[] = "hello world!";
if(access(FIFO_NAME, F_OK) == -) //文件是否存在
{
res = mkfifo(FIFO_NAME, ); //创建管道
if(res != )
{
fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);
exit(EXIT_FAILURE);
}
}
printf("Process %d opening FIFO O_WRONLY\n", getpid());
pipe_fd = open(FIFO_NAME, O_WRONLY); //打开有名管道
printf("the file's descriptor is %d\n", pipe_fd);
if(pipe_fd != -)
{
res = write(pipe_fd, buffer, sizeof(buffer)); //写数据
if(res == -)
{
fprintf(stderr, "Write error on pipe\n");
exit(EXIT_FAILURE);
}
printf("write data is %s, %d bytes is write\n", buffer, res);
close(pipe_fd);
}
else
exit(EXIT_FAILURE);
printf("Process %d finished\n", getpid());
exit(EXIT_SUCCESS);
}

读进程

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/stat.h> #define FIFO_NAME "/tmp/my_fifo" //要创建的有名管道的路径
int main(int argc, char * argv[])
{
int pipe_fd;
int res;
char buffer[];
int bytes_read = ;
memset(buffer, '\0', sizeof(buffer));
printf("Process %d opening FIFO O_RDONLY\n", getpid());
pipe_fd = open(FIFO_NAME, O_RDONLY); //打开有名管道
printf("the file's descriptor is %d\n", pipe_fd);
if(pipe_fd != -)
{
bytes_read = read(pipe_fd, buffer, sizeof(buffer));
printf("the read data is %s\n", buffer);
close(pipe_fd);
}
else
exit(EXIT_FAILURE);
printf("Process %d finished, %d bytes read\n", getpid(), bytes_read);
exit(EXIT_SUCCESS);
}

运行时,先在一个终端运行写进程,此时写进程会阻塞。

ctrl+alt+[F1-F6]切换终端,在新终端运行读进程。

读进程端结果为

再切换回原终端,写进程也执行完毕。

最新文章

  1. 关于dll
  2. HDU1532 网络流:最大流之福德福克森算法
  3. android Home键和返回键
  4. ubuntu初次安装后设置root用户密码
  5. (C#).NET 2.0 ~ 4.0 OS requirements.
  6. POJ2503——Babelfish(map映射+string字符串)
  7. 【原】Object 异常静态
  8. java查询手机号码归属地
  9. app/desktop/view/index.html 显示授权标识
  10. git变基--rebase
  11. mybatis中传入一个List集合作为查询条件的参数
  12. Dynamics CRM2016 New features in Microsoft Dynamics CRM Online 2015 Update 1 are now available
  13. 写XML
  14. 2018-2019-2 20165232《网络对抗技术》Exp1 缓冲区溢出实验
  15. Python——Tk控件说明
  16. SolidWorks基础-快速入门
  17. Beta冲刺 (5/7)
  18. 20145215《网络对抗》Exp5 MSF基础应用
  19. Ubuntu 配置vsftpd实现FTP服务器
  20. odoo开发笔记 -- odoo web机制浅析

热门文章

  1. 16.2,docker网络
  2. centso下如何解压RAR文件
  3. Pascal小游戏之奇葩的RPG
  4. 基于Xtrabackup备份集来恢复某个误删除的表(drop)
  5. selenium获取浏览器控制台日志
  6. Escape From The Earth 逃离地球
  7. CSU-1989 赶路的小X
  8. 聊聊、Spring WebApplicationInitializer
  9. Python的HttpClient实现
  10. 【python】用python爬取中科院院士简介信息