linux中pipe和dup2详解
1、什么是管道
管道是半双工的,数据只能向一个方向流动;需要双方通信时,需要建立起两个管道; 只能用于父子进程或者兄弟进程之间(具有亲缘关系的进程); 单独构成一种独立的文件系统:管道对于管道两端的进程而言,就是一个文件,但它不是普通的文件,它不属于某种文件系统,而是自立门户,单独构成一种文件系统,并且只存在于内存中。数据的读出和写入:一个进程向管道中写的内容被管道另一端的进程读出。写入的内容每次都添加在管道缓冲区的末尾,并且每次都是从缓冲区的头部读出数据。
2、管道的创建
int pipe(int fd[2])
该函数创建的管道的两端处于一个中间进程,在实际应用中并没有太大意义,一般在pipe()创建管道后,再fork()一个子进程,然后通过管道实现父子进程之间的通信。
3、管道的读写规则
管道两端可分别用描述字fd[0]以及fd[1]来描述,需要注意的是,管道的两端是固定了任务的。即一端只能用于读,由描述字fd[0]表示,称其为管道读端;另一端则只能用于写,由描述字fd[1]来表示,称其为管道写端。
4、pipe函数
头文件:#include<unistd.h>
函数原型: int pipe(int fd[2])
函数参数:fd[2],管道的两个文件描述符,之后就是可以直接操作这两个文件描述符。其中fd[0]为读取端,fd[1]为写入端
返回值:成功返回0,否则返回-1
读fd[0]: close(fd[1]); read(fd[0], buf, BUF_SIZE);
写fd[1]: close(fd[0]); read(fd[1], buf, strlen(buf));
5、例子
#include <unistd.h>
#include <stdio.h> int main()
{
int fd[];
char buf[];
pid_t pid; if(pipe(fd) != )
{
printf("pipe error\n");
return ;
} pid = fork(); if(pid == -)
{
printf("fork error\n");
return ;
} else if(pid > )
{
printf("This is the father process, here write a string to the pipe\n");
char s[] = "Hello! write a string in the father process\n";
close(fd[]);
write(fd[], s, sizeof(s)); // 向管道中写入数据 }
else
{
printf("This is the child process, here read a string from the pipe\n");
close(fd[]);
read(fd[], buf, sizeof(buf)); //从管道中读取数据
printf("%s", buf);
}
waitpid(pid, NULL, );
return ;
}
输出结果为:
This is the father process, here write a string to the pipe
This is the child process, here read a string from the pipe
Hello! write a string in the father process
当管道中数据被读取后,管道为空。之后再read操作将默认的被阻塞,等待某些数据被写入。如需要设置为非阻塞,则可做如下设置:
fcntl(filedes[0], F_SETFL, O_NONBLOCK);
fcntl(filedes[1], F_SETFL, O_NONBLOCK);
6、dup2详解
用来复制一个现存的文件描述符,使两个文件描述符指向同一个file结构体。
其中头文件:#include <unistd.h>
函数原型:int dup2(int oldhandle, int newhandle);
例子还是使用上面那个例子,如下:
#include <unistd.h>
#include <stdio.h> int main()
{
int fd[];
char buf[];
pid_t pid;
int newfd;
if(pipe(fd) != )
{
printf("pipe error\n");
return ;
} pid = fork(); if(pid == -)
{
printf("fork error\n");
return ;
} else if(pid > )
{
printf("This is the father process, here write a string to the pipe\n");
char s[] = "Hello! write a string in the father process\n";
close(fd[]);
write(fd[], s, sizeof(s)); // 向管道中写入数据 }
else
{
printf("This is the child process, here read a string from the pipe\n");
close(fd[]);
dup2(fd[], newfd);
read(newfd, buf, sizeof(buf)); //从管道中读取数据
printf("%s", buf);
}
waitpid(pid, NULL, );
return ;
}
运行的结果还是一样。只是在36行代码中调用了dup2,把fd[0]复制给newfd。
linux中pipe和dup2详解的更多相关文章
- (转)linux 中特殊符号用法详解
linux 中特殊符号用法详解 原文:https://www.cnblogs.com/lidabo/p/4323979.html # 井号 (comments)#管理员 $普通用户 脚本中 #!/b ...
- Linux中mpstat命令参数详解
Linux中mpstat命令参数详解 mpstat 是 Multiprocessor Statistics的缩写,是实时系统监控工具.其报告与CPU的一些统计信息,这些信息存放在 /proc/stat ...
- linux中mv命令使用详解
mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] 源文件或目 ...
- 【转】linux 中dd命令使用详解
原文网址:http://xiaozhuang.blog.51cto.com/4396589/850657 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究 ...
- linux中Cron定时任务系统命令详解
分类:Linux VPS教程 作者:阿川 发布时间:October 13, 2011 有很多同学在购买VPS之后,需要用到计划任务.但是又对计划任务不太了解,所以.今天我们的帮助中心主要是给大家提供一 ...
- Linux中THIS_MODULE宏定义详解
一直都在耿耿于怀,这个THIS_MODULE到底是个什么玩意,linux内核中无处不在的东西.今天上网搜了一下,算是基本明白了.网上牛人写的已经比较详细,另外目前暂时没有时间往更深层次分析,所以直接贴 ...
- linux 中特殊符号用法详解
# 井号 (comments)#管理员 $普通用户 脚本中 #!/bin/bash #!/bin/sh井号也常出现在一行的开头,或者位于完整指令之后,这类情况表示符号后面的是注解文字,不会被执行 ...
- [fork]Linux中的fork函数详解
---------------------------------------------------------------------------------------------------- ...
- Linux中top命令参数详解
此文摘自(https://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316399.html) 简介 top命令是Linux下常用的性能分析工具,能够实 ...
随机推荐
- Golang学习系列:(一)介绍和安装
Golang学习系列:(一)介绍和安装 Java程序员带你来到Go的世界,让我们开始探索吧! Go是一种新的语言,一种并发的,带有垃圾回收的.快速编译的语言,它具有一下特点: 他可以在一台计算机上用几 ...
- Visual Studio2017 设置了vcpkg之后,编译其他程序出问题
博客参考:https://github.com/nodejs/node/issues/23909 错误如下 LNK2005 _SSL_CTX_check_private_key already def ...
- 转载:字符串hash总结(hash是一门优雅的暴力!)
转载自:远航休息栈 字符串Hash总结 Hash是什么意思呢?某度翻译告诉我们: hash 英[hæʃ] 美[hæʃ]n. 剁碎的食物; #号; 蔬菜肉丁;vt. 把…弄乱; 切碎; 反复推敲; 搞糟 ...
- [Training Video - 6] [File Reading] [Groovy] Reading Properties file
Reading Properties file : Properties prop = new Properties() def path = "D:\\SoapUIStudy\\appli ...
- Spring设置定时器配置
corn表达式生成:http://www.pppet.net/ 1.注解方式 添加命名空间 xmlns:task="http://www.springframework.org/schema ...
- MySQL性能调优与架构设计——第9章 MySQL数据库Schema设计的性能优化
第9章 MySQL数据库Schema设计的性能优化 前言: 很多人都认为性能是在通过编写代码(程序代码或者是数据库代码)的过程中优化出来的,其实这是一个非常大的误区.真正影响性能最大的部分是在设计中就 ...
- HDU1412:{A} + {B}
Problem Description 给你两个集合,要求{A} + {B}. 注:同一个集合中不会有两个相同的元素. Input 每组输入数据分为三行,第一行有两个数字n,m(0<n,m& ...
- 基数排序简单Java实现
基数排序(radix sort)又称“桶子法”,在对多个正整数进行排序时可以使用.它的灵感来自于队列(Queue),它最独特的地方在于利用了数字的有穷性(阿拉伯数字只有0到9的10个). 基数排序使用 ...
- Postgresql 9.6 搭建 异步流复制 和 同步流复制 详细教程
Basic Replication If you’re feeling overwhelmed, try setting up a slave to see how easy it is! We’ll ...
- 遍历datatable的几种方法(C# )
转载 遍历datatable的方法2009-09-08 10:02方法一: DataTable dt = dataSet.Tables[0]; for(int i = 0 ; i ...