当前位置: 首页 > article >正文

进程间通信IPC——管道(1) 两个进程间通信

IPC: InterProcess Communication

IPC的方式通常有管道(包括无名管道和命名管道)、消息队列、信号量、共享存储、Socket、Streams等。其中 Socket和Streams支持不同主机上的两个进程IPC。

不同进程在物理内存上是相互独立的。

想要实现进程间通信,就要找到公共的资源,主要有三种方式:

  • 一个管道有且只有2个端口,最多支持两个进程通信。

  • 当缓冲区满时阻塞。

  • 像管道一样连接两个进程,一个进程的输入作为另一个进程的输出。

  • 本质是字节流,一个端口负责输入字节,一个端口负责输出字节。

  • 访问一个管道的两个进程:单工,固定的读端和写端。

  • 访问两个管道的两个进程:半双工。

  • 只能用于具有亲缘关系的进程之间的通信(父子进程或兄弟进程)。

  • 可以看成是一种特殊的文件,但它不属于任何文件系统,存在于内核中。

一、管道

通常指无名管道。

管道是内核空间的一片缓冲区,被封装成文件的形式,因此用户可以使用文件IO接口读写。

1、特点

  • 单向,具有固定的读端和写端。

  • 像管道一样连接两个进程,一个进程的输入作为另一个进程的输出。

  • 只能用于具有亲缘关系的进程之间的通信(父子进程或兄弟进程)。

  • 可以看成是一种特殊的文件,但它不属于任何文件系统,只存在于内核中。

2、API

1 #include <unistd.h>  // 头文件
2 int pipe(int fd[2]); // 返回值:若成功返回0,失败返回-1

pipe是系统调用,参数int fd[2],作为传出参数,用户可以得到两个描述符fd[0]读端fd[1]写端

关闭管道只需将这两个文件描述符关闭。

3、例子

单向通信

示例代码:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>#define handle_error(msg) \{ perror(msg); exit(-1); }int main(void)
{int fd_pipe[2] = {0};int pid = -1;if(-1 == pipe(fd_pipe))handle_error("pipe");if(-1 == (pid = fork()))handle_error("fork");if(0 == pid){char buf[20] = {0};int len;close(fd_pipe[0]);puts("child process start");len = read(1, buf, 20);write(fd_pipe[1], buf, len + 1);close(fd_pipe[1]);puts("child process end");exit(0);}else if(0 < pid){char buf[20] = {0};int len;close(fd_pipe[1]);puts("parent process start");len = read(fd_pipe[0], buf, 20);write(2, buf, len);close(fd_pipe[0]);puts("parent process end");exit(0);}return 0;
}

运行结果:

book@100ask:~/Desktop/mycode/linux/IPC$ gcc main.c -o app
book@100ask:~/Desktop/mycode/linux/IPC$ ./app 
parent process start
child process start
Hello, world!
child process end
Hello, world!
parent process end

双向通信

示例:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>#define handle_error(msg) \{ perror(msg); exit(-1); }int main(void)
{int fd_pipe[2] = {0};int fd_pipe_2[2] = {0};int pid = -1;if(-1 == pipe(fd_pipe) || -1 == pipe(fd_pipe_2))handle_error("pipe");if(-1 == (pid = fork()))handle_error("fork");if(0 == pid){char buf[20] = {"child process"};close(fd_pipe[0]);close(fd_pipe_2[1]);puts("child process start");printf("input string: ");scanf("%s", buf);write(fd_pipe[1], buf, strlen(buf));buf[read(fd_pipe_2[0], buf, 20)] = '\0';printf("%s\n", buf);close(fd_pipe[1]);close(fd_pipe_2[0]);puts("child process end");exit(0);}else if(0 < pid){char buf[20] = {0};int len;close(fd_pipe[1]);close(fd_pipe_2[0]);puts("parent process start");len = read(fd_pipe[0], buf, 20);printf("%s\n", buf);strcat(buf, "Okay");write(fd_pipe_2[1], buf, 20);close(fd_pipe[0]);close(fd_pipe_2[1]);puts("parent process end");exit(0);}return 0;
}

运行结果:

book@100ask:~/Desktop/mycode/linux/IPC/pipe$ ./app 
parent process start
child process start
input string: hello
hello
parent process end
helloOkay
child process end

http://www.lryc.cn/news/2418252.html

相关文章:

  • myeclipse 9.0
  • swf反编译工具
  • python主要应用于哪些方面,python一般用来做什么
  • Linux运维工程师常见基础面试题
  • 电子商务网站后台管理系统的设计与实现
  • js中void 0是什么意思,javascript:void()
  • 负载均衡与反向代理
  • UG软件安装教程
  • 深度学习(十七)——SSD, YOLOv2
  • 关于TL494
  • NodeJS某微信小程序云开发的论坛系统的设计与实现-毕业设计源码41083
  • Lazada新手批量发布产品快速铺货详细教程:
  • FFmpeg(12)-使用NDK通过GLSurfaceView完成视频的播放
  • 会声会影X5
  • 2022年氧化工艺复审考试及氧化工艺模拟试题
  • PHP工程师简要指南:如何使用settimestamp函数
  • 电感基础知识
  • 存储服务器 自建,搭建及了解存储服务器
  • 并发测试工具
  • lamp配置
  • 第二课 matlab 系统环境
  • PostScript 程序初探
  • 各种资源网站
  • 【网络信息搜集】03 网络扫描技术
  • 微信小程序开发五大技术框架
  • PHP语言基础知识
  • java数独游戏破解工具源代码
  • Python入门难吗?零基础可以自学吗?
  • 001 谷歌账号注册方法
  • 网络设备安全配置