linux--管道pipe
来源:互联网 发布:知羽电子相册模板下载 编辑:程序博客网 时间:2024/06/01 20:26
在Linux中,管道是一种使用非常频繁的通信机制。从本质上说,管道也是一种文件,但它又和一般的文件有所不同,管道可以克服使用文件进行通信的两个问题,具体表现为: · 限制管道的大小。实际上,管道是一个固定大小的缓冲区。在Linux中,该缓冲区的大小为1页,即4K字节,使得它的大小不象文件那样不加检验地增长。使用单个固定缓冲区也会带来问题,比如在写管道时可能变满,当这种情况发生时,随后对管道的write()调用将默认地被阻塞,等待某些数据被读取,以便腾出足够的空间供write()调用写。 · 读取进程也可能工作得比写进程快。当所有当前进程数据已被读取时,管道变空。当这种情况发生时,一个随后的read()调用将默认地被阻塞,等待某些数据被写入,这解决了read()调用返回文件结束的问题。
注意:从管道读数据是一次性操作,数据一旦被读,它就从管道中被抛弃,释放空间以便写更多的数据
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main(void)
{
intfd[2],nbytes; pid_tchildpid;
charstring[]="Hello,world!\n";
charreadbuffer[80]; pipe(fd);
if((childpid=fork())==-1) { perror("fork"); exit(1); }
if(childpid==0) { /*Child process closes up in put side of pipe*/ close(fd[0]); /*Send"string"through the out put side of pipe*/
write(fd[1],string,strlen(string));
exit(0); }
else {
/*Parent process closes up out put side of pipe*/ close(fd[1]);
/*Readinastringfromthepipe*/ nbytes=read(fd[0],readbuffer,sizeof(readbuffer));
printf("Receivedstring:%s",readbuffer); }
return(0);
}
- linux--管道pipe
- Linux 管道(pipe)
- Linux pipe(管道)
- linux管道pipe
- linux管道(pipe)
- linux管道pipe详解
- 【linux】匿名管道pipe
- Linux 管道pipe
- linux管道pipe详解
- linux pipe 无名管道
- linux管道pipe详解
- linux 管道(linux Pipe与named Pipe)
- Linux C编程 - 管道pipe
- linux之无名管道pipe
- linux shell 管道命令(pipe)
- Linux C编程 - 管道pipe
- Linux管道pipe使用实例
- linux管道通信(pipe)
- linux经典书
- 搜索引擎技术之概要预览
- JAVA开发中的MVC框架(SSH,MVC)(一)
- [Internet]使用IP安全策略阻止Ping
- 自动重发请求(ARQ)协议
- linux--管道pipe
- error LNK2001: unresolved external symbol "void __stdcall _com_issue_error (long)"
- poj 2923 Relocation(状态压缩+01背包)
- linux 网络编程手记
- VC 彻底关闭Excel进程,将Excel文件保存为HTML网页,不显示任何警告对话框
- iphone 计算文件长度
- ORACLE的在线表格重定义
- 引用clone 浅clone 深clone 利用序列化clone 利用反射深clone
- 数据库连接池的配置