Linux IPC BSD 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 BSD Pipe的更多相关文章
- linux IPC的PIPE
一.PIPE(无名管道) 函数原型: #include <unistd.h> ]); 通常,进程会先调用pipe,接着调用fork,从而创建从父进程到子进程的IPC通道. 父进程和子进程之 ...
- Linux IPC BSD socket编程基础
头文件 #include<unistd.h> #include <sys/types.h> #include <sys/socket.h> #include< ...
- Linux IPC实践(1) -- 概述
进程的同步与互斥 进程同步: 多个进程需要相互配合共同完成一项任务. 进程互斥: 由于各进程要求共享资源,而且有些资源需要互斥使用,因此各进程间竞争使用这些资源,进程的这种关系为进程的互斥;系统中某些 ...
- CVE-2017-7494 Linux Samba named pipe file Open Vul Lead to DLL Execution
catalogue . 漏洞复现 . 漏洞代码原理分析 . 漏洞利用前提 . 临时缓解 && 修复手段 1. 漏洞复现 . SMB登录上去 . 枚举共享目录,得到共享目录/文件列表,匿 ...
- 【转载】Linux 与 BSD 有什么不同?
原创:Linux中国 https://linux.cn/article-3186-1.html 原创:LCTT https://linux.cn/article-3186-1.html 本文地址:ht ...
- Linux 与 BSD 有什么不同?
Linux 与 BSD 有什么不同? 这篇文章是别人写的,并做了一点修改. 汉澳sinox就是基于bsd开发的,因此能够理解为一个bsd分支,可是由于sinox不开源,被排除在外.bsd不是商业软件, ...
- Linux mount BSD disk partition
Linux mount BSD disk partition 来源 https://www.cnblogs.com/jhcelue/p/6858159.html 假设须要从第二块硬盘复制文件.该硬盘格 ...
- linux ipc/its
linux进程间双向消息队列 server.c #include <stdio.h> #include <stdlib.h> #include <string.h> ...
- Linux 与 BSD
1)Linux 与 BSD 有什么不同? http://linux.cn/article-3186-1.html 2)BSD(Unix)家族 http://blog.csdn.net/cradmin/ ...
随机推荐
- Java如何处理运行时异常?
在Java编程中,如何处理运行时异常? 此示例显示如何处理java程序中的运行时异常. package com.yiibai; public class RuntimeExceptions { sta ...
- unity3d-----Collider 组件参考
Collider 组件参考 点击 属性检查器 下面的 添加组件 按钮,然后从 添加碰撞组件 中选择需要的 Collider 组件,即可添加 Collider组件到节点上. Collider 组件属性 ...
- ubuntu 系统启动异常之无登录界面和版本号启动四个点的地方卡住
zlib 搞的鬼,还没结局,由于rtmpdump 安装需要安装独立zlib库,装完后重启,完了吓一跳,卡住,尼玛这一年的代码都在里面啊!!! ldd /usr/sbin/python 查询库依赖zli ...
- (转)在SDL工程中让SDL_ttf渲染汉字
有时候在关于SDL的博文中看到一些评论,说SDL对中文的支持不佳,因为当程序涉及中文时总是输出乱码. 照我个人观点,这里面很多都是误解.下面就根据我在windows下使用SDL的情况,说说我的观点. ...
- php -- php模拟浏览器访问网址
目前我所了解到的在php后台中,用php模拟浏览器访问网址的方法有两种: 第一种:模拟GET请求:file_get_contents($url) 通过php内置的 file_get_contents ...
- vue input 赋值无效
1.js代码如下 var vm = new Vue({ el:'#rrapp', data:{ q:{ name: null }, dict: {} }, }); 2.文本框代码如下 <inpu ...
- hihoCoder 1033: 交错和
(1)题目描述: 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an - 1,定义交错 ...
- Getting Started with Google Guava.pdf
- WebGL入门
1.清空绘图区 清空绘图区是使用指定的背景颜色填充canvas,使用gl.clearColor设置背景色.gl.clearColor(red, green, blue, alpha).openGL的颜 ...
- windows7 安装IIS没有default web site 解决方法
因当时是第一次在Windows 7上使用IIS,不小心把default web site删除了,后来无论怎么重装IIS都不行,最后想到能不能直接把别人安装好后的IIS相关文件夹拷过来使用,用宿舍一哥们 ...