Linux pipe功能
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功能的更多相关文章
- Linux pipe 源代码分析
Linux pipe 源代码分析 管道pipe作为Unix中历史最悠久的IPC机制,存在各个版本号的Unix中,主要用于父子进程之间的通信(使用fork,从而子进程会获得父进程的打开文件表) ...
- Remmina:一个 Linux 下功能丰富的远程桌面共享工具(转载)
Remmina:一个 Linux 下功能丰富的远程桌面共享工具 作者: Aaron Kili 译者: LCTT geekpi | 2017-05-10 09:05 评论: 2 收藏: 4 Remm ...
- 【转载】使用宝塔Linux面板功能查看服务器CPU使用率
运维过阿里云服务器或者腾讯云服务器的运维人员都知道,针对耗资源以及高并发的应用,很多时候我们需要关注云服务器的资源利用率情况,如最近一段时间内CPU的使用率.内存占用率等情况信息.阿里云和腾讯云官方后 ...
- 【转载】 使用宝塔Linux面板功能查看服务器内存使用情况
运维过阿里云服务器或者腾讯云服务器的运维人员都知道,针对占用内存比较高的应用或者服务等,我们需要时刻关注服务器的内存使用率,是否存在内存瓶颈等情况的出现.阿里云和腾讯云官方后台界面的监控数据页面也有相 ...
- how to using Linux pipe command output another command's help content to a file
how to using Linux pipe command output another command's help content to a file Linux tee > >& ...
- Linux inotify功能及实现原理
http://www.cnblogs.com/jiejnan/archive/2012/05/18/2507476.html 简介: 当需要对 Linux®文件系统进行高效率.细粒度.异步地监控时,可 ...
- 【linux】linux查找功能
linux系统中我们经常会需要查找某些文件,当有时候我们不确定一个文件的位置,比如某服务配置文件具体路径,自己没有头绪去寻找的话会很难找,也会耽误时间.linux就提供了很多命令,find,locat ...
- Linux inotify功能及实现原理【转】
转自:http://blog.csdn.net/myarrow/article/details/7096460 1. inotify主要功能 它是一个内核用于通知用户空间程序文件系统变化的机制. 众所 ...
- Linux内核功能介绍及如何使用保护您的网页安全
在本文中,我们快速浏览了Linux内核的许可流程,并向您展示了如何使用它们来保护您的网页或应用安全 传统上,Linux内核通过以下两类来区分其进程: 特权进程:这些进程使用户可以绕过所有内核权限检查. ...
随机推荐
- android获取View上某点的颜色
//根据坐标获取 ImageView imageView = ((ImageView)v); Bitmap bitmap = ((BitmapDrawable)imageView.getDrawabl ...
- ORA-01092 ORA-12432: LBAC error: zllegnp:OCIStmtExecute 故障一例
最近由于数据库hang住,无奈之下直接干掉了pmon进程,再次启动的时候收到了ORA-01092: ORACLE instance terminated. Disconnection forced以及 ...
- JVM类加载过程学习总结
JVM类加载过程学习总结 先不说JVM类加载的原理,先看实例: NormalTest类,包含了一个静态代码块,执行的任务就是打印一句话. /** * 在正常类加载条件下,看静态代码块是否会执行 * @ ...
- 操作3 mongodb和mysql 开启慢查询日志 ,以及mongodb从配置文件启动
1. mongodb从配置文件启动 创建配置文件:/usr/local/mongodb/etc/mongodb.conf 配置文件的内容为: #Directory and relavent set d ...
- 基于visual Studio2013解决C语言竞赛题之1062高与矮
题目 解决代码及点评 /************************************************************************/ /* 62 ...
- 简单的javascript抽奖程序
<html> <head> <title>手机号码抽奖程序</title> <script> //声明一个数组装住号码,可根 ...
- fedora 搭建pptp vpn server
1 首先去sourceforge上下载pptpd的源码 http://sourceforge.net/projects/poptop/files/?source=navbar 2 对源码进行编译 ./ ...
- 由于“Table(T_Test)”没有主键,因此无法在其上执行 Create、Update 或 Delete 操作
在使用Linq To Sql查询的时候,遇到这么个问题,如图所示: 出现这个问题的原因就像途中所说的——没有主键(现在终于初步知道“为什么别人常说数据库中的逻辑主键是为了在编程中方便使用”的原因了,估 ...
- mysql 分区和集群
集群和分区:http://han-zw.iteye.com/blog/1662941http://www.php-note.com/article/detail/794 分区:http://lober ...
- 遇到Audio/Speech相关问题,如何抓取log
[DESCRIPTION] 遇到Audio/Speech相关问题时,经常需要抓取相关log信息,总结抓取方法如下 [SOLUTION] 1. 通话声音相关的问题: Case 1: 通话中某一 ...