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. e840. 监听JTabbedPane中选中卡片的改变

    A tabbed pane fires a change event whenever the selected tab is changed either by the user or progra ...

  2. e811. 创建具有嵌套菜单的弹出式菜单

    See e810 创建弹出菜单 for an example on how to display a popup menu. final JPopupMenu popupMenu = new JPop ...

  3. Error configuring application listener of class org.springframework.web.context.ContextLoaderListener

    严重:   Error   configuring   application   listener   of   class   org.springframework.web.context.Co ...

  4. 使用selenium遇到java.lang.NoSuchMethodError: org.apache.xpath.XPathContext,排查

    初试selenium webdriver,运行小程序,抛如下错误:   java.lang.NoSuchMethodError: org.apache.xpath.XPathContext.<i ...

  5. EXP-00056遇到Oracle错误1455问题解决办法

    简单描述一下问题:需要备份一下生产环境的数据库到测试环境,使用EXP命令备份数据库时出现错误 EXP-00056: 遇到 ORACLE 错误 1455 ORA-01455: 转换列溢出整数数据类型 E ...

  6. 转载:帮你提升 Python 的 27 种编程语言

    帮你提升 Python 的 27 种编程语言: 出处:http://www.oschina.net/translate/languages-to-improve-your-python

  7. windows server 安装phpVirtualBox web服务

    这是我在Windows server 2012 下的安装过程 (1) 安装virtualbox (2) 安装IIS和PHP, 参考: https://msdn.microsoft.com/zh-cn/ ...

  8. Linux语言设置修改乱码

    1.system-config-language 命令语言改成英文.(安装yum install  system-config-language) 如何系统安装后,使用的语言不是自己想要的.但是在图形 ...

  9. Android学习之——GridView

    背景知识 GridView在Android开发中和ListView一样经常被使用.如我们经常使用的快图浏览,里面就有将图片的布局改为网格(即GridView)的选项.还有约X神器——陌陌的搜索界也是用 ...

  10. mysql日期问题

    1.在java中,在当前时间的基础上增加30天.Date d = new Date();   SimpleDateFormat df = new SimpleDateFormat("yyyy ...