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. Oracle 经典语法(五)

    1. 哪些部门的人数比20 号部门的人数多.SELECT DEPTNO,COUNT(*) FROM EMP     GROUP BY DEPTNO      HAVING COUNT(*) >  ...

  2. IE兼容性问题列表及解决办法

    目录 概述 2 第一章:HTML 3 第一节:IE7-IE8更新 31. 如果缺少结束标记的 P 元素后跟 TABLE.FORM.NOFRAMES 或 NOSCRIPT 元素,会自动添加结束标记. 3 ...

  3. 【WinAPI】User32.dll注释

    #region User32.dll 函数 /// <summary> /// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以在GDI函数中使用该句柄来在设备 ...

  4. 元数据metadata 对IO有多大影响

    日志文件系统(journaling file systems)可防止系统崩溃时导致的数据不一致问题.对文件系统元数据(metadata)的更改都被保存在一份单独的日志里,当发生 系统崩溃时可以根据日志 ...

  5. tmux使用

    1.配置文件的使用 在~/.tmux.conf中添加: setw -g mouse-resize-pane on setw -g mouse-select-pane on setw -g mouse- ...

  6. 学习Slim Framework for PHP v3 (六)--route怎么被匹配的?

    先标记觉得以后会用到的内容: // add route to the request's attributes in case a middleware or handler needs access ...

  7. Part 53 to 55 Talking about Reflection in C#

    Part 53 Reflection in C# Part 54 Reflection Example here is the code private void btnDiscover_Click( ...

  8. hexo搭建静态博客

    1. 环境环境 1.1 安装Git 请参考[1] 1.2 安装node.js 下载:http://nodejs.org/download/ 可以下载 node-v0.10.33-x64.msi 安装时 ...

  9. ORACLE-树状数据结构获取各层级节点信息

    平时工作中出报表时,要求分别列出员工的一级部门,二级部门....,在数据库中,部门表(unit)的设计一般为在表中维护每个部门的上级部门(pid字段),或者通过一个关联表(unit_link)维护层级 ...

  10. ajax请求简写

    <script type="text/javascript"> function changle() { $.post( "SendMail", / ...