基础知识:

管道是最初的Unix IPC形式,可追溯到1973年的Unix第3版。使用其应注意两点:

1)没有名字;

2)用于共同祖先间的进程通信;

3)读写操作用read和write函数

#include <unistd.h>
int pipe(int
fildes[2]);

返回:成功返回1,否则返回-1.创建成功的两个文件描述符:f[0]和f[1],前者用来打开读,后者用来打开写。 #include <unistd.h>
ssize_t read(int fildes, void *buf, size_t nbyte);
ssize_t write(int fildes, const void *buf, size_t nbyte);

测试代码:

代码1

 #include<stdio.h>
#include<unistd.h> int main()
{
int n,fd[]; // 这里的fd是文件描述符的数组,用于创建管道做准备的
pid_t pid;
char line[];
if(pipe(fd)<) // 创建管道
printf("pipe create error/n"); if((pid=fork())<) //利用fork()创建新进程
printf("fork error/n");
else if(pid>)
{ //这里是父进程,先关闭管道的读出端,然后在管道的写端写入“hello world"
close(fd[]);
write(fd[],"hello word/n",);
}
else
{ //这里是子进程,先关闭管道的写入端,然后在管道的读出端读出数据
close(fd[]);
n= read(fd[],line,);
write(STDOUT_FILENO,line,n);
}
exit();
}

代码2

 #include <unistd.h>
#include <stdio.h> void client (int, int ) , server (int , int) ;
int main(int argc, char **argv)
{
int pipe1[] ,pipe2[];
pid_t childpid; pipe(pipe1);/* create two pipes */
pipe(pipe2); if ( (childpid = fork () ) == )
{ /* child */
close (pipe1[]) ;
close (pipe2[]) ;
server (pipe1[],pipe2[]) ;
exit () ;
}
/* parent */
close (pipe1[]) ;
close (pipe2[]) ;
client (pipe2[], pipe1[]) ; waitpid(childpid, NULL, ); /* wait for child to terminate */
exit() ; } void client(int readfd, int writefd)
{
size_t len;
ssize_t n;
char buff[MAXLINEI; /* read pathname */
fgets (buff, MAXLINE, stdin) ;
len = strlen(buf f) ; /* £gets() guarantees null byte at end */
if (buff [len - ] == '\n' )
len--; /* delete newline from £gets() */ /* write pathname to IPC channel */
write(writefd, buff, len); /* read from IPC, write to standard output */
while ( (n = Read(readfd, buff, MAXLINE)) > )
write(STD0UT_FILENO, buff, n):
}
void server(int readfd, int writefd)
{
int fd;
ssize_t n;
char buff [MAXLINE + ]; /* read pathname from IPC channel */
if ( (n = Read(readfd, buff, MAXLINE) ) == )
err-quit("end-of-file while reading pathname"):
buff[n] = '\0'; /* null terminate pathname */ if ( (fd = open(buff, O_RDONLY)) < )
{
/* error: must tell client */
snprintf(buff + n, sizeof(buff) - n, ": can't open, %s\",strerror(errn0));
n = strlen(buf f) ;
write(writefd, buff, n) ;
}
else
{
/* open succeeded: copy file to IPC channel */
while ( (n = Read(fd, buff, MAXLINE)) > )
write(writefd, buff, n);
close ( fd) ;
} }

参考资料

win32下的多线程命名管道通信的设计

linux管道

pipe fifo简介

Linux进程线程学习笔记:进程间通信之管道

Linux进程间通信IPC学习笔记之管道的更多相关文章

  1. Linux进程间通信IPC学习笔记之同步二(SVR4 信号量)

    Linux进程间通信IPC学习笔记之同步二(SVR4 信号量)

  2. Linux进程间通信IPC学习笔记之同步二(Posix 信号量)

    Linux进程间通信IPC学习笔记之同步二(Posix 信号量)

  3. Linux进程间通信IPC学习笔记之消息队列(SVR4)

    Linux进程间通信IPC学习笔记之消息队列(SVR4)

  4. Linux进程间通信IPC学习笔记之有名管道

    基础知识: 有名管道,FIFO先进先出,它是一个单向(半双工)的数据流,不同于管道的是:是最初的Unix IPC形式,可追溯到1973年的Unix第3版.使用其应注意两点: 1)有一个与路径名关联的名 ...

  5. Linux进程间通信IPC学习笔记

    linux下的进程通信手段基本上是从Unix平台上的进程通信手段继承而来的.而对Unix发展做出重大贡献的两大主力AT&T的贝尔实验室及BSD(加州大学伯克利分校的伯克利软件发布中心)在进程间 ...

  6. Linux进程间通信IPC学习笔记之同步一(线程、互斥锁和条件变量)

    基础知识: 测试代码: 参考资料: Posix 多线程程序设计

  7. Linux进程间通信IPC学习笔记之消息队列(Posix)

    基础知识: 消息队列可认为是一个消息链表,有足够写权限的线程可往队列中放置消息,有足够读权限的线程可以从队列中取走消息.在某个进程往一人队列写入消息之前,并不需要另外某个进程在该队列上等待消息的到达. ...

  8. linux的IPC进程通信方式-匿名管道(一)

    linux的IPC进程通信-匿名管道 什么是管道 如果你使用过Linux的命令,那么对于管道这个名词你一定不会感觉到陌生,因为我们通常通过符号"|"来使用管道,但是管道的真正定义是 ...

  9. Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存

    Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存 参考:<linux编程从入门到精通>,<Linux C程序设计大全>,<unix环境高级编程> ...

随机推荐

  1. Stupid Tower Defense

    Problem Description FSF is addicted to a stupid tower defense game. The goal of tower defense games ...

  2. How to solve Original Tango programmer”Hardware not Found”?

    Original Tango programmer is a new generation of transponder programmer which is developed to cover, ...

  3. android-partition分析

    转载请注明来源:cuixiaolei的技术博客 这里讲下android的分区.具体的使用在另一片文章中介绍,这里只是把它拿出来介绍. android的存储分为两种 一种叫做RAM,如emmc标准的dd ...

  4. 开发一个struts2的实例

    前面一篇博客(实现struts2框架)带大家对基于mvc业务流程熟悉了一下,现在我们就用对mvc实现最好的框架struts2来开发一个应用实例.虽然现在MyEclipse8.5以上版本已经开始支持St ...

  5. java+mysql实现保存图片到数据库,以及读取数据库存储的图片

    一:建表 二:获取数据库连接 1:导入mysql的驱动jar包,mysql-connector-java-5.1.8-bin.jar 2:写代码连接数据库,如下: /** * */ package c ...

  6. vb.net Linq 筛选(像 select distinct) DateTable 日期数据中的年份

    Private Sub initDDLByYear(ByVal dt As DataTable) ddlByYear.Items.Clear() ddlByYear.Items.Add(") ...

  7. CF Fox And Names (拓扑排序)

    Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  8. Scala中的If判断&While&For循环

    If 判断: object TestScalaIf { def main(args: Array[String]): Unit = { // val resutlt = judge1(-100) // ...

  9. 关于线程池ThreadPoolExecutor使用总结

    本文引用自: http://blog.chinaunix.net/uid-20577907-id-3519578.html 一.简介 线程池类为 java.util.concurrent.Thread ...

  10. php-fpm配置文件详解

    第一部分:FPM 配置 参数 | 说明 -p | 命令行中动态修改--prefix ;include=etc/fpm.d/*.conf | 用于包含一个或多个文件,如果glob(3)存在(glob() ...