pipe(管道)

  • 专用于父子进程通信, 函数原型 int pipe(int fd[2])
  • fd[0]表示输入, fd[1]表示输出
  • 如果父子进程要双向通信, 可以通过类似信号的功能进行控制, 也可以简单地打开两个pipe

以下例子, 打开两个pipe, 第一个pipe用于父进程向子进程发送信息, 第二个pipe用于子进程向父进程发送消息

子进程接收到消息后, 将消息转成大写然后发送给父进程

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h> char *strupr(char *str){
char *orign=str;
for(;*str != 0;++str)
*str=toupper(*str);
return orign;
}
void err_quit(const char *str){
perror(str);
exit(1);
}
int main(){
int n;
int fd1[2], fd2[2];
pid_t pid;
char line[1024]; if(pipe(fd1)<0 || pipe(fd2)<0)
err_quit("pipe error"); if((pid =fork())<0){
err_quit("pipe error");
}else if(pid >0 ){
close(fd1[0]);
close(fd2[1]);
write(fd1[1],"hello world\n",12);
n=read(fd2[0],line,1024);
write(STDOUT_FILENO,line,n);
}else{
close(fd1[1]);
close(fd2[0]);
n=read(fd1[0],line,1024);
write(STDOUT_FILENO,line,n);
line[n]='\0';
write(fd2[1],strupr(line),n);
}
exit(0);
}

popen和pclose

  • 函数原型FILE *popen(const char *cmdstring, const char *type) , type的参数为"r"或"w"
  • 用于父子进程通信, popen会自动fork子进程、创建pipe和关闭不需要的pipe端
  • popen的实现有可理解为execl("/bin/sh","sh","-c",cmdstring,NULL)
  • 当父进程向子进程发送信息时(type="w"), 实际就是向shell发送命令;

    当父进程从子进程获取信息时(type="r"), 实际就是读取shell的执行结果
  • 虽然popen的返回是FILE,但关闭是要用pclose(fp)
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> #define MAXLINE 1024
void err_quit(const char *str){
perror(str);
exit(1);
}
int main(){
char line[MAXLINE];
FILE *fpin; if((fpin=popen("ls -l","r")) == NULL)
err_quit("popen error");
while(fgets(line,MAXLINE,fpin) != NULL){
if(fputs(line,stdout) ==EOF)
err_quit("fputs error");
}
if(ferror(fpin))
err_quit("fpin error");
if(pclose(fpin)==-1)
err_quit("pclose error");
return 0;
}

fifo

  • int mkfifo(const char *pathname,mode_t mode), 创建一个通信文件, 参数同open
  • mkfifo后, 以open打开文件, 打开方式为只读或只写, 另外可以非阻塞方式打开
  • 以只读打开时, 函数会阻塞直到有进程以只写方式打开
  • 以只写打开时, 函数会阻塞直到有进程以只读方式打开
  • 可用于非父子进程通信, 双向通信时开需两个
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h> #define FILE_MODE S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IROTH
void err_quit(const char *str){
perror(str);
exit(1);
}
int main(){
char *pathname="./fifobuf";
pid_t pid; if(mkfifo(pathname,FILE_MODE)<0)
err_quit("mkfifo error"); if((pid=fork())<0){
err_quit("fork error");
}else if(pid >0){
int fd;
if((fd=open(pathname,O_RDONLY))<0)
err_quit("open error");
char buf[100];
int n;
if((n=read(fd,buf,100)) < 0)
err_quit("read error");
buf[n]=0;
puts(buf);
exit(0);
}else{
int fd;
if((fd=open(pathname,O_WRONLY))<0)
err_quit("open error");
char *str="hello world";
write(fd,str,strlen(str));
exit(0);
}

pipe/popen/fifo的更多相关文章

  1. 信号处理篇alarm ferror kill mkfifo pause pclose perror pipe popen sigaction sigaddset sigdelset sigemptyset signal sleep strerror

    alarm(设置信号传送闹钟) 相关函数 signal,sleep 表头文件 #include<unistd.h> 定义函数 unsigned int alarm(unsigned int ...

  2. 进程间通信和同步:pipe、FIFO、消息队列、信号量、共享内存、信号

    一.半双工管道(pipe) 关于管道详细介绍可参考http://www.cnblogs.com/nufangrensheng/p/3560130.html. 1.管道实现父子进程间通信实例: /* p ...

  3. 进程间通信之管道(pipe、fifo)

    我们先来说说进程间通信(IPC)的一般目的,大概有数据传输.共享数据.通知事件.资源共享和进程控制等.但是我们知道,对于每一个进程来说这个进程看到属于它的一块内存资源,这块资源是它所独占的,所以进程之 ...

  4. 进程间通信之管道--pipe和fifo使用

    匿名管道pipe 函数原型: #include <unistd.h> int pipe(int fildes[2]); 参数说明 fildes是我们传入的数组,也是一个传出参数.filde ...

  5. 【Linux 应用编程】进程管理 - 进程间通信IPC之管道 pipe 和 FIFO

    IPC(InterProcess Communication,进程间通信)是进程中的重要概念.Linux 进程之间常用的通信方式有: 文件:简单,低效,需要代码控制同步 管道:使用简单,默认阻塞 匿名 ...

  6. linux多进/线程编程(4)——进程间通信之pipe和fifo

    前言: Linux环境下,进程地址空间相互独立,每个进程各自有不同的用户地址空间.任何一个进程的全局变量在另一个进程中都看不到,所以进程和进程之间不能相互访问,要交换数据必须通过内核,在内核中开辟一块 ...

  7. Linux中的pipe(管道)与named pipe(FIFO 命名管道)

    catalogue . pipe匿名管道 . named pipe(FIFO)有名管道 1. pipe匿名管道 管道是Linux中很重要的一种通信方式,是把一个程序的输出直接连接到另一个程序的输入,常 ...

  8. Linux进程间通信之管道(pipe)、命名管道(FIFO)与信号(Signal)

    整理自网络 Unix IPC包括:管道(pipe).命名管道(FIFO)与信号(Signal) 管道(pipe) 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道 ...

  9. 进程间通信IPC之--无名管道(pipe)和有名管道(fifo)(转)

     进程间通信IPC之--无名管道(pipe)和有名管道(fifo) 2012-01-17 22:41:20 分类: C/C++ 每个进程各自有不同的用户地址空间,任何一个进 程的全局变量在另一个进程中 ...

随机推荐

  1. Problem:Minesweeper Master

    Google code jam Qualification Round 2014 题目链接:https://code.google.com/codejam/contest/dashboard?c=29 ...

  2. hdu-5698 瞬间移动(数论+快速幂)

    题目链接: 瞬间移动 Problem Description   有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝 ...

  3. sharepoint 2013 "The module ... owssvr.dll could not be loaded due to a configuration problem"

    打开sharepoint站点可以看到这个503的错误, 在event viewer中查看如下: The Module DLL 'C:\Program Files\Common Files\Micros ...

  4. 浮出层的css写法,完美兼容IE6~10

    利用元素间的绝对定位差一像素,使用不同颜色做出浮出层小三角的效果,完美兼容各浏览器! html部分: <div class="poptip"> <span cla ...

  5. JDBC学习笔记(一)

    public static void main(String[] args) { ResultSet rs = null; Statement stmt = null; Connection conn ...

  6. SQL 把查出来的信息整合为一张表

    select name ,population from bbc where name='France' union all select name ,population from bbc wher ...

  7. Javascript加载速度慢解决办法

    通常我们的网站里面会加载一些js代码,统计啊,google广告啊,百度同盟啊,阿里妈妈广告代码啊,一堆,最后弄得页面加载速度很慢,很慢.解决办法:换一个js包含的方式,让javascript加载速度倍 ...

  8. 20141109--SQL 练习题-1

    create database xinxiku go use xinxiku go create table Student ( Sno ) primary key, Sname ) not null ...

  9. Js获取标签高度

    能力有限:问个问题,标签相对页面高度,是怎么写? 鼠标的横坐标,X轴: event.clientX; 鼠标的竖坐标,Y轴: event.clientY; 网页可见区域宽:    document.bo ...

  10. 【ASP.NET】获取网站目录的方法

         获取网站物理路径: HttpRuntime.AppDomainAppPath 获取网站虚拟路径: HttpRuntime.AppDomainAppVirtualPath