Linux进程间的通信--管道通信
2016-10-24 23:00
225 查看
管道通信
·管道是单向的、先进先出的,它把一个进程的输出和另一个进程的输入连接在一起。
·一个进程(写进程)在管道的尾部写入数据,另一个进程(读进程)从管道的头部读出数据
·数据被一个进程读出后,将被从管道中删除,其它读进程将不能再读到这些数据。
·管道提供了简单的流控制机制,进程试图读空管道时,进程将阻塞。同样,管道已经满时,进程再试图向管道写入数据,进程将阻塞
无名管道:
1.pipe
函数的原型:int pipe(int fds[2])
函数的参数:新建的两个描述符fds数组返回,fds[0]表示管道的读取端,fds[1]表示管道的写入端。
返 回 值:成功0,出错-1
头 文 件:#include <unistd.h>
管道读写:
1.先创建管道pipe;
2.fork创建子进程
[html] view
plain copy
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int pipe_fd[2];
if(pipe(pipe_fd)<0)
{
printf("pipe create error\n");
return -1;
}
else
printf("pipe create success\n");
close(pipe_fd[0]);
close(pipe_fd[1]);
}
有名管道:
·创建管道mkfifo
·打开管道open
·读管道read
·写管道write
·关闭管道close
·删除管道unlink
2.mkfifo
函数的作用:创建有名管道
函数的原型:int mkfifo(const char * filename,mode_t mode)
参数的作用:filename:有名管道的路径、名称
mode:打开管道的方式
O_NONBLOCK: FIFO打开的时候,费阻塞
O_RDONLY: 只读
O_WRONLY: 只写
O_RDWR: 可读写
返 回 值:成功 0 出错 -1
·管道是单向的、先进先出的,它把一个进程的输出和另一个进程的输入连接在一起。
·一个进程(写进程)在管道的尾部写入数据,另一个进程(读进程)从管道的头部读出数据
·数据被一个进程读出后,将被从管道中删除,其它读进程将不能再读到这些数据。
·管道提供了简单的流控制机制,进程试图读空管道时,进程将阻塞。同样,管道已经满时,进程再试图向管道写入数据,进程将阻塞
无名管道:
1.pipe
函数的原型:int pipe(int fds[2])
函数的参数:新建的两个描述符fds数组返回,fds[0]表示管道的读取端,fds[1]表示管道的写入端。
返 回 值:成功0,出错-1
头 文 件:#include <unistd.h>
管道读写:
1.先创建管道pipe;
2.fork创建子进程
[html] view
plain copy
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int pipe_fd[2];
if(pipe(pipe_fd)<0)
{
printf("pipe create error\n");
return -1;
}
else
printf("pipe create success\n");
close(pipe_fd[0]);
close(pipe_fd[1]);
}
有名管道:
·创建管道mkfifo
·打开管道open
·读管道read
·写管道write
·关闭管道close
·删除管道unlink
2.mkfifo
函数的作用:创建有名管道
函数的原型:int mkfifo(const char * filename,mode_t mode)
参数的作用:filename:有名管道的路径、名称
mode:打开管道的方式
O_NONBLOCK: FIFO打开的时候,费阻塞
O_RDONLY: 只读
O_WRONLY: 只写
O_RDWR: 可读写
返 回 值:成功 0 出错 -1
相关文章推荐
- Linux下C编程,进程通信之标准流管道通信(即系统调用)
- Linux下的进程通信方式(IPC)——管道通信
- Linux进程通信-管道通信
- Linux下C编程,进程通信之标准流管道通信(即系统调用)
- Linux下C语言编程 -- 进程通信、消息管理
- Linux 进程通信
- Linux进程通信-使用信号量
- Linux进程通信
- Linux下C语言编程--进程通信、消息管理
- linux 进程通信-信号学习总结(1)
- 【嵌入式Linux学习七步曲之第七篇 Linux的高级应用编程】进程间的通信机制
- Linux中实现内部进程通信
- 关于Linux进程通信
- Linux下C语言编程--进程通信、消息管理
- linux下的进程通信
- Linux进程通信--信号
- Linux下的C编程实战(三)――进程控制与进程通信编程 推荐
- 在Linux中实现内部进程通信
- linux进程通信--管道(pipe)(含实例代码)
- linux进程通信-信号量使用