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

进程间通信

#include <unistd.h>

    int pipe(int pipefd[2]);

    功能:创建一个匿名管道,用于进程间通信

    参数:

        -int pipefd[2]:传出参数

            pipefd[0]对应的是管道的读端

            pipefd[0]对应的是管道的写端

    返回值:

        成功返回0,失败返回-1

管道默认是阻塞的,如果管道中没有数据,read阻塞,如果管道满了,write阻塞

    注意:匿名管道只能用于具有关系的进程之间的通信(父子进程、兄弟进程)

1.子进程写数据,父进程读数据并输出

//子进程发送数据给父进程,父进程读取到数据输出
#include <unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main() {//在fork之前创建管道int pipefd[2];int ret = pipe(pipefd);if(ret == -1) {perror("pipe");exit(0);}//创建子进程pid_t pid = fork();if(pid > 0) {//父进程//从管道读取端读取数据char buf[1024] = {0};int len = read(pipefd[0], buf, sizeof(buf));printf("parent recv : %s, pid: %d\n", buf, getpid());} else if(pid == 0){//子进程//在管道中写入数据char * str = "hello, i am child";write(pipefd[1], str, strlen(str));}return 0;
}

2.子进程持续写数据,父进程持续接收

//子进程发送数据给父进程,父进程读取到数据输出
#include <unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main() {//在fork之前创建管道int pipefd[2];int ret = pipe(pipefd);if(ret == -1) {perror("pipe");exit(0);}//创建子进程pid_t pid = fork();if(pid > 0) {//父进程//从管道读取端读取数据printf("i am parent process, pid: %d\n", getpid());char buf[1024] = {0};while(1) {int len = read(pipefd[0], buf, sizeof(buf));printf("parent recv : %s, pid: %d\n", buf, getpid());}} else if(pid == 0){//子进程printf("i am child process, pid: %d\n", getpid());while(1) {//在管道中写入数据char * str = "hello, i am child";write(pipefd[1], str, strlen(str));sleep(1);}}return 0;
}

3.父子进程都能读和写数据

//子进程发送数据给父进程,父进程读取到数据输出
#include <unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main() {//在fork之前创建管道int pipefd[2];int ret = pipe(pipefd);if(ret == -1) {perror("pipe");exit(0);}//创建子进程pid_t pid = fork();if(pid > 0) {//父进程//从管道读取端读取数据printf("i am parent process, pid: %d\n", getpid());char buf[1024] = {0};while(1) {//在管道中读取数据int len = read(pipefd[0], buf, sizeof(buf));printf("parent recv : %s, pid: %d\n", buf, getpid());//在管道中写入数据char * str = "hello, i am parent";write(pipefd[1], str, strlen(str));sleep(1);}} else if(pid == 0){//子进程printf("i am child process, pid: %d\n", getpid());char buf[1024] = {0};while(1) {//在管道中写入数据char * str = "hello, i am child";write(pipefd[1], str, strlen(str));sleep(1);//在管道中读取数据int len = read(pipefd[0], buf, sizeof(buf));printf("child recv : %s, pid: %d\n", buf, getpid());}}return 0;
}

4.查看管道缓冲大小

 

#include <unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>int main() {int pipefd[2];int ret = pipe(pipefd);//获取管道大小long size = fpathconf(pipefd[0], _PC_PIPE_BUF);printf("pipe size: %ld\n", size);return 0;
}

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

相关文章:

  • Ubuntu 22.04.3 LTS安装
  • 记一次manjaro-i3系统sogoupinying候选词无法正常显示中文(变方框了)问题解决方案
  • Lua学习笔记:词法分析
  • flask服务鉴权
  • 【2023华为杯B题】DFT类矩阵的整数分解逼近(思路及代码下载)
  • 基于微信小程序的校园生活管理系统设计与实现(源码+lw+部署文档+讲解等)
  • SQL server 创建存储过程
  • 一文了解亚马逊云科技适用于 Amazon Lightsail 的托管数据库
  • 【antd Col】奇怪的TypeError: Cannot read properties of undefined (reading ‘then‘)
  • requests处理 multipart/form-data 请求以及 boundary值问题
  • FBX文件结构解读【文本格式】
  • JS基础语法
  • 【Zabbix监控一】zabbix的原理与安装
  • 图的十字链表存储结构
  • 精华回顾:Web3 前沿创新者在 DESTINATION MOON 共话未来
  • 【RPC】gRPC 安装及使用
  • Pygame中Sprite类的使用3
  • 23年下考前须知-软考中级信息安全工程师
  • 关于表单快速开发低代码技术平台的内容介绍
  • 比特币 ZK 赏金系列:第 1 部分——支付解密密钥
  • 【Python深度学习】深度学习中框架和模型的区别
  • MyBatis面试题(二)
  • Android之MediaMetricsService实现本质(四十二)
  • Flutter超好用的路由库-fluro
  • 约数个数(蓝桥杯)
  • 越狱(快速幂C++)
  • 电脑入门:怎么进入路由器设置
  • Vue3大屏项目实现数字跳动的效果
  • MATLAB打开历史命令窗口并保持
  • 等差数列和等比数列 常用公式