有名管道(FIFO)
有名管道是持久稳定的。
它们存在于文件系统中。
FIFO比无名管道作用更大,因为他们能让无关联的进程之间交换数据。
管道文件一般用于交换数据。
shell命令创建管道
一个shell命令可以建立有名管道
--mkfifo [option] name
--mkfifo创建一个名为name的有名管道
--mkfifo fifo1 创建一个有名管道fifo1
--mkfifo -m fifo2 创建一个带权限的管道文件
--cat < fifo1 通过cat命令从fifo1中读取数据。
--ls > fifo1 将ls命令输出的结果写入fifo1中
shell命令删除管道
--"rm name"
代码创建管道fifo
mkfifo()函数是C语言库函数
int mkfifo(const char * pathname,mode_t mode);
参数pathname是文件名称,
参数mode是文件读写权限(读写权限是一位八进制数,例如0777(0表示八进制)表示当前用户,组用户,其他用户都拥有对该文件的可读,可写,可执行权限)
函数执行成功返回0,否则返回-,并设置变量errno。
代码删除管道
int unlink(const char *pathname);
unlink()函数是系统函数
函数执行成功返回0,否则返回-,并设置变量errno。
//代码创建有名管道
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h> int main(int arg, char * args[])
{
if(arg<)
{
printf("请输入一个参数!\n");
return -;
}
int no=;
//创建有名管道
no=mkfifo(args[],);
if(no==-)
{
printf("创建管道失败!\n");
return -;
}
return ;
}
//代码删除有名管道
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h> int main(int arg, char * args[])
{
if(arg<)
{
printf("请输入一个参数!\n");
return -;
}
int no=;
//删除有名管道
no=unlink(args[]);
if(no==-)
{
printf("删除管道失败!\n");
return -;
}
return ;
}
打开和关闭FIFO
int open(const char * pathname,int flags);
int close(int fd);
Linux的一切都是文件这一抽象概念的优势,打开和关闭FIFO和打开关闭一个普通文件操作是一样的。
FIFO的两端使用前都必须打开
open中如果参数flags为O_RDONLY将阻塞open调用,一直到另一个进程为写入数据打开FIFO为止。
相同的,O_WRONLY也导致阻塞一直到为读出数据打开FIFO为止。
//有名管道读文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> int main(int arg, char *args[])
{
if (arg < )
{
printf("请输入一个参数!\n");
return -;
}
int fd = ;
char buf[] = { };
//打开管道文件
fd = open(args[], O_RDONLY);
if (fd == -)
{
printf("open the file failed ! error message : %s\n", strerror(errno));
return -;
}
while (read(fd, buf, sizeof(buf)) > )
{
printf("%s", buf);
memset(buf, , sizeof(buf));
}
//close the file stream
close(fd);
return ;
}
//管道写文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> int main(int arg,char * args[])
{
if(arg<)
{
printf("请输入一个参数!\n");
return -;
}
int fd=;
char buf[]={};
fd=open(args[],O_WRONLY);
if(fd==-)
{
printf("open the file failed ! error message :%s\n",strerror(errno));
return -;
}
while()
{
//从键盘上读取数据
read(STDIN_FILENO,buf,sizeof(buf));
if(buf[]=='')
{
break;
}
//写入管道文件中
write(fd,buf,strlen(buf));
memset(buf,,sizeof(buf));
}
//close the file stream
close(fd);
return ;
}

Linux 进程通信(有名管道)的更多相关文章

  1. Linux进程通信----匿名管道

    Linux进程通信中最为简单的方式是匿名管道 匿名管道的创建需要用到pipe函数,pipe函数参数为一个数组表示的文件描述字.这个数组有两个文件描 述字,第一个是用于读数据的文件描述符第二个是用于写数 ...

  2. linux 进程通信之 管道和FIFO

    进程间通信:IPC概念 IPC:Interprocess Communication,通过内核提供的缓冲区进行数据交换的机制. IPC通信的方式: pipe:管道(最简单) fifo:有名管道 mma ...

  3. linux进程通信之管道

    1.介绍: 1)同一主机: unix进程通信方式:无名管道,有名管道,信号 system v方式:信号量,消息队列,共享内存 2)网络通信:Socket,RPC 2.管道: 无名管道(PIPE):使用 ...

  4. Linux 进程通信之管道

    管道是单向的.先进先出的,它把一个进程的输出和还有一个进程的输入连接在一起.一个进程(写进程)在管道的尾部写入数据,还有一个进程(读进程)从管道的头部读出数据.数据被一个进程读出后,将被从管道中删除, ...

  5. Linux进程通信——管道

    管道(pipe)本质上是一种文件,管道通信本质上是通过读写文件通信,但是管道解决了文件的两个问题:限制管道大小,解决read()调用文件结束问题. 管道一个环形的缓冲区,通过两个进程以生产者/消费者的 ...

  6. Linux下进程通信之管道

    每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲区,进程1把数据从用户空间拷到内核缓冲区,进程2再从内核缓冲区把 ...

  7. 进程通信类型 管道是Linux支持的最初Unix IPC形式之一

    管道 Linux环境进程间通信(一) https://www.ibm.com/developerworks/cn/linux/l-ipc/part1/index.html 管道及有名管道 郑彦兴200 ...

  8. 进程通信类型 管道是Linux支持的最初Unix IPC形式之一 命名管道 匿名管道

    管道 Linux环境进程间通信(一) https://www.ibm.com/developerworks/cn/linux/l-ipc/part1/index.html 管道及有名管道 郑彦兴200 ...

  9. Linux学习笔记(13)-进程通信|命名管道

    匿名管道只能在具有亲属关系的进程间通信,那么如果想要在不具有亲戚关系,想在陌生人之间通信,那又该怎么办呢? 别慌,Linux身为世界上*强大的操作系统,当然提供了这种机制,那便是命名管道-- 所谓命名 ...

随机推荐

  1. 应用程序代理AppDelegate解析

    应用程序UIApplication是通过代理和外部交互的,当应用程序生命周期中发生改变时UIApplication就会调用代理对应的方法. @implementation AppDelegate - ...

  2. 弃用的异步get和post方法之Block方法

    #import "ViewController.h" #import "Header.h" @interface ViewController () <N ...

  3. 安卓第十天笔记-fragment

    安卓第十天笔记-fragment Fragment(片段) 一.Fragment简介 *Fragment是3.0引入的API,主要为了解决平板,大屏幕手机显示问题 *Fragment代表了Activi ...

  4. iOS 验证邮箱手机号格式

    做登录界面时,用户在UITextfield中输入输入邮箱账号后,我们应该在本地验证格式是否正确,再将参数传给服务器验证. 最简单的就是利用系统的NSPredicate //利用正则表达式验证 -(BO ...

  5. du df 查看文件和文件夹大小

    http://www.cnblogs.com/benio/archive/2010/10/13/1849946.html du -h df -h du -h --max-depth=1 //  查看当 ...

  6. Effective Java 64 Strive for failure atomicity

    Principle Failure atomic - A failed method invocation should leave the object in the state that it w ...

  7. Java学习总结:飘逸的字符串

    Java学习:飘逸的字符串 前言 相信不管我们运用Java语言来开发项目还是进行数据分析处理,都要运用到和字符串相关的处理方法.这个社会处处有着和字符串相关的影子:日志.文档.书籍等.既然我们离不开字 ...

  8. 安装SQL Server2008,要重启机器,解决办法

    安装SQL Server2008时,总提示有挂起,要重启机器:重启之后还是有相应的提示,该怎么办呢? 其实只要删除一个注册表项就可以了: 1.  打开注册表编辑器 开始菜单—>运行->re ...

  9. CentOS6.5上golang环境配置

    CentOS6.5上golang环境配置 一.下载和解压go环境包 >>cd /usr/local/src/ >>wget -c http://golangtc.com/sta ...

  10. Super A^B mod C

    Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B ...