linux pipe 函数( int pipe(int filedes[2]))
来源:互联网 发布:java jdk安装 编辑:程序博客网 时间:2024/05/18 15:07
//http://blog.csdn.net/myarrow/article/details/9037135
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 函数( int pipe(int filedes[2]))
- 进程的管道通信pipe(int)函数的运用
- Linux pipe函数
- Linux pipe函数
- Linux pipe函数
- Linux pipe 函数
- Linux pipe函数
- Linux pipe函数
- Linux pipe函数
- Linux pipe函数介绍
- Linux pipe函数
- Linux pipe函数
- Linux pipe函数
- Linux pipe函数
- Linux pipe函数
- linux下的pipe()管道函数
- linux c语言之pipe()函数
- pipe()函数
- RPC(linux)报错 Cannot register service RPC
- dumpsys 命令
- JDBC
- Android项目开发总结(三)-- 规范篇
- Swift设计模式之装饰模式
- linux pipe 函数( int pipe(int filedes[2]))
- 小米盒子开启ADB调试的方法
- RPC
- 文件I/O与系统编程
- Android开源项目分类汇总【畜生级别】
- Swift设计模式之中介者模式
- Oracle数据库使用nfs挂载的盘执行expdp说明(Linux环境下)
- 有用的网址
- 1011 Problem K