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. MySQL5.6主从复制搭建基于日志(binlog)

    什么是MySQL主从复制 简单来说,就是保证主SQL(Master)和从SQL(Slave)的数据是一致性的,向Master插入数据后,Slave会自动从Master把修改的数据同步过来(有一定的延迟 ...

  2. C# 批量生成邮箱地址代码

    如图,是我生成好的5万条邮箱数据, 其实,网上有大量批量生成邮箱的软件,多种多样的生成格式,各种设置,各种组合, 我不需要那么强大,只需要生成不重复的邮箱地址即可,所以,我懒得从网上下载了, 反正就几 ...

  3. spring + springMVC + spring Data + jpa + maven 项目框架搭建

    首先看一下项目结构: 所用到的jar(pom.xml): <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...

  4. Scala学习笔记——安装

    安装scala,不要使用sudo apt-get install scala来安装 1.从下面网址来下载Scala文件 http://www.scala-lang.org/download/2.11. ...

  5. Excel破解密码代码

    Option ExplicitPublic Sub AllInternalPasswords()' Breaks worksheet and workbook structure passwords. ...

  6. 自动批改android模拟器的imei的小程序 和 下载各个版本SDK Tools及ADT

    ADT 22.6.0版本的下载路径是:http://dl.google.com/android/ADT-22.6.0.zip ADT22.6.1版本的下载路径是:http://dl.google.co ...

  7. UNIX环境编程学习笔记(23)——信号处理初步学习

    lienhua342014-10-29 1 信号的概念 维基百科中关于信号的描述是这样的: 在计算机科学中,信号(英语:Signals)是 Unix.类 Unix 以及其他 POSIX 兼容的操作系统 ...

  8. C语言之Bit-wise Operation和Logical Operation

    首先第一点:十六进制位运算和逻辑运算 都是先转化二进制,后输出结果(十六进制,二或十)Bit-Wise Operations (位运算)包括:& 按位与 | 按位或 ^ 按位异或 ~ 取反 & ...

  9. mysql查询两个日期之间相差多少天?

    需求描述: 在mysql中,查看两个日期之间相差多少天 操作过程: 1.通过datediff函数,查看两个日期之间相差多少天 mysql> select datediff('2018-06-26 ...

  10. 2018-10-29 A股主要指数的市盈率(PE)估值高度

    全指材料(SH000987) - 2018-10-29日,当前值:11.9289,平均值:30.66,中位数:26.1407,当前 接近历史新低.全指材料(SH000987)的历史市盈率PE详情 全指 ...