1. 函数说明

pipe(建立管道):

1) 头文件 #include<unistd.h>

2) 定义函数: int pipe(int filedes[2]);

3) 函数说明: pipe()会建立管道,并将文件描写叙述词由參数filedes数组返回。

              filedes[0]为管道里的读取端

              filedes[1]则为管道的写入端。

4) 返回值:  若成功则返回零,否则返回-1,错误原因存于errno中。

错误代码:

         EMFILE 进程已用完文件描写叙述词最大量

         ENFILE 系统已无文件描写叙述词可用。

         EFAULT 參数 filedes 数组地址不合法。

2. 举例

#include <unistd.h>
#include <stdio.h> int main( void )
{
int filedes[2];
char buf[80];
pid_t pid; pipe( filedes );
pid=fork();
if (pid > 0)
{
printf( "This is in the father process,here write a string to the pipe.\n" );
char s[] = "Hello world , this is write by pipe.\n";
write( filedes[1], s, sizeof(s) );
close( filedes[0] );
close( filedes[1] );
}
else if(pid == 0)
{
printf( "This is in the child process,here read a string from the pipe.\n" );
read( filedes[0], buf, sizeof(buf) );
printf( "%s\n", buf );
close( filedes[0] );
close( filedes[1] );
} waitpid( pid, NULL, 0 ); return 0;
}

执行结果:

[root@localhost src]# gcc pipe.c

[root@localhost src]# ./a.out

This is in the child process,here read a string from the pipe.

This is in the father process,here write a string to the pipe.

Hello world , this is write by pipe.

当管道中的数据被读取后,管道为空。一个随后的read()调用将默认的被堵塞,等待某些数据写入。

若须要设置为非堵塞,则可做例如以下设置:

fcntl(filedes[0], F_SETFL, O_NONBLOCK);

        fcntl(filedes[1], F_SETFL, O_NONBLOCK);

Linux pipe函数的更多相关文章

  1. linux系统调用函数---12

    Linux应用编程学习笔记                                 周学伟 一.系统调用文件编程   1.文件打开函数 /*************************** ...

  2. 匿名管道和pipe函数

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

  3. Linux open函数

    Linux open函数 open 函数用于打开和创建文件.以下是 open 函数的简单描述 #include <fcntl.h> int open(const char *pathnam ...

  4. linux内核函数库文件的寻找

    linux内核函数的so库文件怎么找呢? 首先还是要产生一个进程的coredump文件的 linux有一个lib-gdb.so库,这个进程的coredump文件中所有load段的最后一个load段中, ...

  5. 体验usually.js的管道函数——pipe函数

    体验usually.js的管道函数——pipe函数 usually.js 是一个面向现代 Web 开发的 JavaScript 函数库,基于 ES6 开发.最新版本2.4.1,最新版本usually. ...

  6. linux select函数详解

    linux select函数详解 在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数的参数会告诉内核: •我们所关心的文件描述符 •对每个描述符,我们所关心的状 ...

  7. Linux mmap函数简介

    一.简介 Linux提供了内存映射函数mmap, 它把文件内容映射到一段内存上(准确说是虚拟内存上), 通过对这段内存的读取和修改, 实现对文件的读取和修改, 先来看一下mmap的函数声明: 头文件: ...

  8. 动态替换Linux核心函数的原理和实现

    转载:https://www.ibm.com/developerworks/cn/linux/l-knldebug/ 动态替换Linux核心函数的原理和实现 在调试Linux核心模块时,有时需要能够实 ...

  9. Linux getopt()函数 getopt_long()函数---转

    http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f Linux getopt()函数 getopt_long()函数 get_o ...

随机推荐

  1. C# Adding Hyperlink to Windows Form z

    When creating a Windows form in C#, we would like to create a hyperlink so that when the user click ...

  2. SQL Server 高性能写入的一些总结(转)

    1.1.1 摘要 在开发过程中,我们不时会遇到系统性能瓶颈问题,而引起这一问题原因可以很多,有可能是代码不够高效.有可能是硬件或网络问题,也有可能是数据库设计的问题. 本篇博文将针对一些常用的数据库性 ...

  3. (原创)LAMP教程3-下载centos6.4

    (原创)LAMP教程3-下载centos6.4 今天我要给大家讲的是安装CentOS-6.4-x86_64,是的没有错,就是64位的,因为我的机子是4G的内存,安装64位的centos是绰绰有余啊,但 ...

  4. 用Windows Live Writer发来

    文字     package com.myeclipseide.example.myblog.secure; import com.opensymphony.xwork2.ActionSupport; ...

  5. ZOJ 3911 Prime Query(线段树)

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  6. java操作redis之jedis篇

    首先来简单介绍一下jedis,其实一句话就可以概括的,就是java操作redis的一种api.我们知道redis提供了基本上所有常用编程语言的clients,大家可以到http://redis.io/ ...

  7. Java学习笔记(四):流程控制

    if语句 if判断语句比较简单,具体有下面的几种写法: int i = 0; // if 判断 if (i == 0) { // to do something } // if else 判断 if ...

  8. 关于 jquery cookie的用法

    东钿微信公众平台新版上线 需要一个引导用户操作步骤.设置一个cookie师傅偶第一次访问此页面 .如果是则跳出用户引导,如果不是,正常显示. 一开始在百度了一段jquery cookie插件,也没仔细 ...

  9. 决定如何开发你的WordPress主题框架

    在本系列教程的第一部分,我介绍了不同类型的主题框架并解释了它们是如何工作的. 在你开始建立你的主题框架之前,你需要考虑它是如何工作的,以及它将会被用来做什么,这样你才能从一开始就找到最合适的开发途径. ...

  10. 回顾JDBC

    最近因为公司的需要,一直在做我司商城的修改和维护,好几个月没有接触过java的代码了,有点手生,就算是开发java 的web项目,持久层习惯用hibernate,jdbc很少用.hibernate实现 ...