pipe(建立管道)
表头文件 #include<unistd.h>
定义函数 int pipe(int filedes[2]);
函数说明
pipe()会建立管道,并将文件描述词由参数 filedes 数组返回。
filedes[0]为管道里的读取端,所以pipe用read调用的
filedes[1]则为管道的写入端。
返回值: 若成功则返回零,否则返回-1,错误原因存于 errno 中。
错误代码: 
EMFILE 进程已用完文件描述词最大量
ENFILE 系统已无文件描述词可用。
EFAULT 参数 filedes 数组地址不合法。
#include <unistd.h>
#include <stdio.h>
int main( void )
{
int filedes[2];
char buf[80];
pid_t pid;
pipe( filedes );
if ( (pid=fork()) > 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
{
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.
-----------------------------------------------------------------------------
#include<unistd.h>
#include<sys/types.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
int main(){
int pipe_fd[2];
pid_t pid;
char buf_r[100];
char *p_wbuf;
int r_num;
memset(buf_r,0,sizeof(buf_r));
//创建管道
if(pipe(pipe_fd)<0){
printf("pipe create error\n");
return -1;
}
if((pid=fork())==0){//表示在子进程中
printf("\n");
//关闭管道写描述符,进行管道读操作
close(pipe_fd[1]);
sleep(2);
//管道描述符中读取
if((r_num=read(pipe_fd[0],buf_r,100))>0){
printf("%d numbers read from the pipe is %s\n",r_num,buf_r);
}
close(pipe_fd[0]);
exit(0);
}
else if(pid>0){//表示在父进程中,父进程写
//关闭管道读描述符,进行管道写操作
close(pipe_fd[0]);
if(write(pipe_fd[1],"Hello",5)!=-1)
printf("parent write1 success!\n");
if(write(pipe_fd[1],"Pipe",5)!=1)
printf("parent write2 success!\n");
close(pipe_fd[1]);
sleep(3);
waitpid(pid,NULL,0);
exit(0);
}
}
管道读写注意事项:
1.必须在系统调用fork()中调用pipe(),否则子进程将不会继承文件描述符;
2.当使用半双工管道时,任何关联的进程都必须共享一个相关的祖先进程。
----------------------------------------------------------------------------------------
int in[2],out[2],err[2],child_fd[3];
#define close_pipe(pipe) close(pipe[0]); close(pipe[1])
int SendCmd(const char *Cmd)
{
int iRet = 0;
iRet = write(child_fd[0], Cmd, strlen(Cmd));
return iRet;
}
int ReadDate(const char *Buf,int Len)
{
int iRet = 0;
iRet = read(child_fd[1], (void *)Buf, Len);
return iRet;
}
int main(void)
{
//int filedes[2];
char buf[80];
pid_t pid;
pipe(in);
pipe(out);
pipe(err);
pid=fork();
if(pid == 0)
{
printf( "This is in the child process,here read a string from the pipe.\n" );
int err_fd = dup(2);
FILE* errf = fdopen(err_fd,"w");
// Bind the std fd to our pipes
dup2(in[0],0);
dup2(out[1],1);
dup2(err[1],2);
execl("/bin/sh","sh","-c","/usr/bin/mplayer -slave -idle -quiet -v -af",(void*)NULL);
fprintf(errf,"exec failed : %s\n",strerror(errno));
exit(1); 
}
else if(pid<0)
{
printf( "This is in the father process\n" );
stderr,"errno : %s\n",strerror(errno);
close_pipe(in);
close_pipe(out);
close_pipe(err);
return 0; 
}
child_fd[0] = in[1];
child_fd[1] = out[0];
child_fd[2] = err[0];
printf("Start...\n");
while(1)
{
switch(getchar())
{  
case 'r':
SendCmd("loadfile /mnt/Chinese/CH_1.avi\n");
break;
dafault:
break; 
}
//SendCmd("loadfile /mnt/Chinese/CH_1.avi\n");
//sleep(5);
}
return 0;
}

pipe()函数精解相关推荐

  1. python中map函数返回值类型_Python函数精解:map函数

    描述 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表. 语法 m ...

  2. python中len 函数_Python函数精解:len()函数

    描述 len函数返回序列类型对象(字符或字符串.元组.列表和字典等)的项目个数(长度). 语法 len(object) 函数返回一个大于0的int型整数,表示对象的项目个数. 实例 1. 当参数是序列 ...

  3. JavaScript 编程精解 中文第三版 三、函数

    三.函数 原文:Functions 译者:飞龙 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 部分参考了<JavaScript 编程精解(第 2 版)> 人们认为计算机科学是天 ...

  4. JavaScript 编程精解 中文第三版 二十、Node.js

    二十.Node.js 原文:Node.js 译者:飞龙 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 部分参考了<JavaScript 编程精解(第 2 版)> A stude ...

  5. JavaScript 编程精解 中文第三版 零、前言

    零.前言 原文:Introduction 译者:飞龙 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 部分参考了<JavaScript 编程精解(第 2 版)> We think ...

  6. 精解C++的switch语句

    入门书籍对switch语句的介绍相对较浅,我也因此而产生了很多想当然的误解.为解惑而写了以下一小篇精解switch语句,相信会对很多朋友有所帮助,同时顺便补充一些相关知识. 先抛出个题目,见下程序: ...

  7. C语言趣味程序百例精解

    1.绘制余弦曲线 在屏幕上用"*"显示0~360度的余弦函数cos(x)曲线 *问题分析与算法设计 如果在程序中使用数组,这个问题十分简单.但若规定不能使用数组,问题就变得不容易了 ...

  8. JavaScript 编程精解 中文第三版 二十一、项目:技能分享网站

    二十一.项目:技能分享网站 原文:Project: Skill-Sharing Website 译者:飞龙 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 部分参考了<JavaScri ...

  9. JavaScript 编程精解 中文第三版 十五、处理事件

    十五.处理事件 原文:Handling Events 译者:飞龙 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 部分参考了<JavaScript 编程精解(第 2 版)> 你对 ...

最新文章

  1. CTFshow 信息收集 web2
  2. ARM体系结构及内核回顾总结(一)
  3. 'mysql_attr_use_buffered_query'_php中mysql操作的buffer知识
  4. 登录代码,程序不是作文
  5. 米斯特白帽培训讲义 漏洞篇 文件包含
  6. 我们是如何认识这个世界的呢
  7. 彩虹php域名授权系统,彩虹云域名授权系统(正版源码+教程)
  8. 【学习笔记】seckill-秒杀项目--(11)项目总结
  9. 大学计算机课能旷课吗,计算机课旷课检讨书
  10. WIN10电脑热点无法共享
  11. 数据结构(c语言版) 计算机科学丛书,数据结构与算法分析--C语言描述(原书第2版)(计算机科学丛书)...
  12. 爵士之夜(Jazz Night)
  13. 最简洁的呼吸灯实验verilog
  14. 十一、总结一下今天在SpringAOP中遇到的一个坑,事务控制一直失败,自己感觉代码没错的感觉,往底层看,很明了了
  15. 苹果 M1 芯片首席设计师重回英特尔
  16. Android 自定义View 实例2_Clipping Canvas
  17. 数值计算方法上机c语言编程,数值计算方法上机实验报告.doc-资源下载在线文库www.lddoc.cn...
  18. 镇魂歌~Qt5字符串
  19. 4-4 jitsi meet sdk android 编译总结
  20. skynet源码赏析

热门文章

  1. Java高阶代码_Java高阶语法---Volatile
  2. oracle11g32位安装流程_Oracle 11g服务器安装详细步骤图文详解
  3. C++多继承(多重继承)详解(一)
  4. linux下使用 du查看某个文件或目录占用磁盘空间的大小
  5. tf.nn.embedding_lookup函数的用法
  6. 卸载 nvidia 显卡驱动
  7. 60. Leetcode 面试题 10.03. 搜索旋转数组 (二分查找-局部有序)
  8. Leetcode 面试题 01.01. 判定字符是否唯一 (每日一题 20211012)
  9. Leetcode 92 反转链表 II (每日一题 20210726)
  10. 机器学习应用方向(一)~英文姓名消歧(name disambiguation)