Linux进程间通讯方式之管道pipe
来源:互联网 发布:mongodb java 编辑:程序博客网 时间:2024/05/01 17:23
Linux 进程间通讯方式有以下几种:
1-》管道(pipe)和有名管道(fifo).
2-》消息队列
3-》共享内存
4-》信号量
5-》信号(signal)
1-》管道(pipe)和有名管道(fifo).
2-》消息队列
3-》共享内存
4-》信号量
5-》信号(signal)
6-》套接字(sicket)
在Linux系统中,管道通信可以通过使用系统调用来实现。
使用格式为:
表头文件 #include<unistd.h>
定义函数 int pipe(int filedes[2]);
函数说明
pipe()会建立管道,并将文件描述词由参数 filedes 数组返回。
filedes[0]为管道里的读取端,所以pipe用read调用的
filedes[1]则为管道的写入端。
返回值: 若成功则返回零,否则返回-1,错误原因存于 errno 中。
错误代码:
EMFILE 进程已用完文件描述词最大量
ENFILE 系统已无文件描述词可用。
EFAULT 参数 filedes 数组地址不合法。
但值得我们注意的是:管道它有自身的特点。
(1)管道通信是单向的,并且遵守先进先出的原则,即先写入的数据先读出。
(2)管道是一个无结构,无固定大小的字节流。
(3) 管道把一个进程的标准输出和另一个进程的标准输入连接在一起。数据读出后就意味着从管道中移走了,消失了。其它的进程都不能
再读到这些数据。就像我们平常见到的管子水流走了就没有了。 这点很重要!!
(4) pipe这种管道用于两个有亲缘关系的进程之间。eg:父子进程......
#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;}
1 0
- Linux进程间通讯方式之管道pipe
- Linux进程间通信方式之管道(pipe)
- Linux进程间通信方式之管道(pipe)
- Linux进程间通信方式之管道(pipe)
- Linux进程间通信之管道(pipe)
- Linux进程间通信之管道(pipe)
- Linux进程间通信之管道(pipe)
- Linux进程间通讯一--无名管道PIPE
- 【IPC进程间通讯之二】管道Pipe
- Linux 进程间通讯之有名管道方式
- Linux 进程间通讯之有名管道方式
- Linux IPC通讯之-管道(PIPE、FIFO)
- Linux 进程间通讯方式 pipe()函数
- linux进程间通信之管道(pipe与fifo)
- Linux进程间通信之无名管道(PIPE)
- 【Linux】进程间通信之管道pipe与FIFO
- Linux环境进程间通信 --- 管道Pipe
- Linux 进程间管道pipe通信
- Makefile笔记
- 关于WPF画图性能问题
- Android ListView 下拉刷新 上拉更多[实例]
- 第七周项目4-友元类
- 【Java编程】Java基本数据类型
- Linux进程间通讯方式之管道pipe
- [iOS] UITextField 輸入完成跳至下一個UITextField
- oracle存储过程转化成sqlserver
- C标准预定义宏 -- 需要整理一下
- 数据结构---二叉树总结
- 无剑无我 2547
- 根据经纬度调用Google地图显示对应位置
- POJ1141 Brackets Sequence
- Java Web学习交流网的设计(模仿百度贴吧)