Linux IPC Pipe
mkfifo()
//创建有名管道(FIFO special file),创建完了就像普通文件一样open(),再读写,成功返回0,失败返回-1设errno。VS$man 3 mkfifo
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
pathname:the FIFO special file's name
mode :the FIFO's permissions.
//创建FIFO管道文件
int res=mkfifo(“./a.fifo”,0664);
if(-1==res)
perror("mkfifo"),exit(-1);
res=open(“./a.fifo”,O_RDONLY);
if(-1==res)
perror(“open”),exit(-1);
pipe()
//创建无名管道,相当于直接把open()返回的fd直接放到形参中,而不需额外的变量接收管道文件的描述符,用于父子进程间通过管道进行IPC通信,,成功返回0,失败返回-1设errno
#include <unistd.h>
int pipe(int pipefd[2]); //代码自注释,表示它需要的参数是一个有两个元素的数组,如果虽然作为形参,实质上和int* pipefd没有区别
pipefd :return two fds referring to the ends of the pipe.
- pipefd[0] :read end,读端
- pipefd[1] :write end,读端.
fork()创建的child也会文件描述符总表也会复制一份So,对于child, 应该先关闭读端, 再写,对于parent,应该先关闭写端, 再读
//使用pipe()实现父子进程的通信
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<stdio.h>
int main(){
//1. 创建无名管道
int pipefd[2];
int res=pipe(pipefd);
if(-1==res)
perror("pipe"),exit(-1);
//2. 创建子进程
pid_t pid=fork();
if(-1==pid)
perror("fork"),exit(-1);
//3. 子进程开始启动,写入1~100;
if(0==pid){
close(pipefd[0]);
int i=0;
for(i=1;i<=100;i++){
write(pipefd[1],&i,sizeof(int));
}
close(pipefd[1]);//关闭写端
exit(0);
}
//4. 父进程开始启动,读取管道中的数据
close(pipefd[1]);
int i=0;
for(i=1;i<=100;i++){
int x=0;
read(pipefd[0],&x,sizeof(int));
printf("%d ",x);
}
printf("\n");
close(pipefd[0]);
return 0;
}
Linux IPC Pipe的更多相关文章
- Linux IPC实践(1) -- 概述
进程的同步与互斥 进程同步: 多个进程需要相互配合共同完成一项任务. 进程互斥: 由于各进程要求共享资源,而且有些资源需要互斥使用,因此各进程间竞争使用这些资源,进程的这种关系为进程的互斥;系统中某些 ...
- linux ipc/its
linux进程间双向消息队列 server.c #include <stdio.h> #include <stdlib.h> #include <string.h> ...
- Linux IPC之共享内存C 事例
Linux IPC之共享内存 标签: linuxrandomnull工作 2011-08-25 11:52 4123人阅读 评论(0) 收藏 举报 分类: Linux(3) 读书札记(3) 版权 ...
- linux管道pipe详解
linux管道pipe详解 https://blog.csdn.net/qq_42914528/article/details/82023408
- Linux IPC实践(2) --匿名PIPE
管道概念 管道是Unix中最古老的进程间通信的形式,我们把从一个进程连接到另一个进程的一个数据流称为一个"管道", 管道的本质是固定大小的内核缓冲区; 如:ps aux | gre ...
- Linux IPC BSD Pipe
mkfifo() //创建有名管道(FIFO special file),创建完了就像普通文件一样open(),再读写,成功返回0,失败返回-1设errno.VS$man 3 mkfifo #incl ...
- linux IPC的PIPE
一.PIPE(无名管道) 函数原型: #include <unistd.h> ]); 通常,进程会先调用pipe,接着调用fork,从而创建从父进程到子进程的IPC通道. 父进程和子进程之 ...
- Linux下pipe使用注意事项
转自:http://blog.yufeng.info/archives/1485 Linux下的pipe使用非常广泛, shell本身就大量用pipe来粘合生产者和消费者的. 我们的服务器程序通常会用 ...
- linux IPC总结——管道
管道 管道是unix ipc的最古老形式,是一种在内存中的特殊文件,只能在具有公共祖先的进程之间使用(即父子进程,兄弟进程). 管道由pipe函数创建 #include <unistd.h> ...
随机推荐
- POJ 3276 Face The Right Way 反转
大致题意:有n头牛,有些牛朝正面,有些牛朝背面.现在你能一次性反转k头牛(区间[i,i+k-1]),求使所有的牛都朝前的最小的反转次数,以及此时最小的k值. 首先,区间反转的顺序对结果没有影响,并且, ...
- 放弃OT了,找了个新框架ThinkCMF
放弃OT了,找了个新框架ThinkCMF,感觉还不错,用用看. 选择OT的原因: 1. OT基于ThinkPHP 2. OT对ThinkPHP进行了封装,使得开发应用更加简单 3. yershop应用 ...
- 使用Spring的@Scheduled实现定时任务
Spring配置文件xmlns加入xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation ...
- js 字符串 replace replaceAll
var str = "男的女的老的少的"; alert(str.replace('的','')); 可以看到替换后的str的值为"男女的老的少的",replac ...
- Play 中如何使用 Ajax
Play在内部使用了 jQuery 这个JavaScript库,让我们能够非常方便的进行Ajax操作.同时,为了能在JavaScript中方便地生成某个action对应的Url,Play还提供了一个 ...
- 移动Web开发的bug及解决方案
我目前移动Web开发遇到的bug以及解决方案(慢慢补充当中). 1.android4.0以上一部分手机的webview中,当canvas小于屏幕大小时,绘图时会出现重影,就是说一个图只绘制了一遍,却出 ...
- SharePoint回环检查(Loopback Check)相关问题
Loopback Check(回环检查)本来不是一个SharePoint问题,是Windows Server为了增强自身安全性在Server 2003 SP1后引入的一个功能, 在近几个月中导致了一系 ...
- atitit.userService 用户系统设计 v4 q316 .doc
atitit.userService 用户系统设计 v4 q316 .doc 1. 新特性1 2. Admin login1 3. 用户注册登录2 3.1. <!-- 会员注册使用 --> ...
- The specified file or folder name is too long
You receive a "The specified file or folder name is too long" error message when you creat ...
- smarty访问数组中的数据,如果是关联数组直接用点.
$tpl=new Smarty();//新建一个smarty对象,我使用的是Smarty-3.1.6版本 1.设置smarty模板路径$tpl->setTemplateDir():默认情况下是t ...