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的更多相关文章

  1. linux IPC的PIPE

    一.PIPE(无名管道) 函数原型: #include <unistd.h> ]); 通常,进程会先调用pipe,接着调用fork,从而创建从父进程到子进程的IPC通道. 父进程和子进程之 ...

  2. Linux IPC BSD socket编程基础

    头文件 #include<unistd.h> #include <sys/types.h> #include <sys/socket.h> #include< ...

  3. Linux IPC实践(1) -- 概述

    进程的同步与互斥 进程同步: 多个进程需要相互配合共同完成一项任务. 进程互斥: 由于各进程要求共享资源,而且有些资源需要互斥使用,因此各进程间竞争使用这些资源,进程的这种关系为进程的互斥;系统中某些 ...

  4. CVE-2017-7494 Linux Samba named pipe file Open Vul Lead to DLL Execution

    catalogue . 漏洞复现 . 漏洞代码原理分析 . 漏洞利用前提 . 临时缓解 && 修复手段 1. 漏洞复现 . SMB登录上去 . 枚举共享目录,得到共享目录/文件列表,匿 ...

  5. 【转载】Linux 与 BSD 有什么不同?

    原创:Linux中国 https://linux.cn/article-3186-1.html 原创:LCTT https://linux.cn/article-3186-1.html 本文地址:ht ...

  6. Linux 与 BSD 有什么不同?

    Linux 与 BSD 有什么不同? 这篇文章是别人写的,并做了一点修改. 汉澳sinox就是基于bsd开发的,因此能够理解为一个bsd分支,可是由于sinox不开源,被排除在外.bsd不是商业软件, ...

  7. Linux mount BSD disk partition

    Linux mount BSD disk partition 来源 https://www.cnblogs.com/jhcelue/p/6858159.html 假设须要从第二块硬盘复制文件.该硬盘格 ...

  8. linux ipc/its

    linux进程间双向消息队列 server.c #include <stdio.h> #include <stdlib.h> #include <string.h> ...

  9. Linux 与 BSD

    1)Linux 与 BSD 有什么不同? http://linux.cn/article-3186-1.html 2)BSD(Unix)家族 http://blog.csdn.net/cradmin/ ...

随机推荐

  1. FTP服务器的配置与实现

    一.准备工作 实验目的:完成FTP服务器的配置,并能熟练操作. 环境搭建: 虚拟机  vmware workstation windows2003镜像文件 Serv-U 主机 二.步骤 1,在虚拟机中 ...

  2. 第三百四十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—craw母版l创建自动爬虫文件—以及 scrapy item loader机制

    第三百四十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—craw母版l创建自动爬虫文件—以及 scrapy item loader机制 用命令创建自动爬虫文件 创建爬虫文件是根据scrap ...

  3. 【经验之谈】Git使用之TortoiseGit配置VS详解;国内几大Git代码托管网站

    转载自: http://www.cnblogs.com/xishuai/p/3590705.html   http://www.cnblogs.com/shanyou/p/3662482.html

  4. Docker命令之 search

    docker search : 从Docker Hub查找镜像 语法 docker search [OPTIONS] TERM OPTIONS说明: --automated :只列出 automate ...

  5. SAX解析XML笔记

    关于基本操作,请参考:Java用SAX解析XML,这里不重复造轮子了,以下是个人笔记:

  6. 设置更改root密码 连接mysql mysql常用命令

  7. JSP内置对象—session

    什么是session? session对象是用来在每个用户之间分别保存每个用户信息的对象,以便跟踪用户的操作状态.session的信息保存在server端,session的id保存在client的co ...

  8. 在mvc4中多语言建站的实例

    环境:vs2012 asp.net mvc4. 实现方式:resource 资源文件,根据路由规则中Lang参数来判断载入哪种语言方式 在网上找到了相关资料,顺便自己做了个练习,新建工程之类的步骤就免 ...

  9. 针对降质模型中的模糊SR

    (PDF) Deep Plug-and-Play Super-Resolution for Arbitrary Blur Kernels https://www.researchgate.net/pu ...

  10. Grunt--Less

    摘要: 之前介绍了自动构建工具Grunt,其中有一个模块是"grunt-contrib-less",下面是配置Grunt自动编译less文件. 安装: Grunt是基于node,功 ...