Linux —— 进程间通信

目录

一,进程间通信

二,管道

匿名管道

命名管道


一,进程间通信

        进程间通信(IPC,InterProcess Communication),即在不同进程之间进行信息的传播或交换;由于一般进程用户地址空间是独立的,不可直接访问其他进程地址空间,因此进程间进行信息交换必须通过系统内核进行;

进程间通信目的

  • 数据传输,将一个进程的数据发送给另一个进程;
  • 资源共享,多个进程间共享同样的资源;
  • 通知事件,一个进程向另一个进程或一组进程发送信息,通知它们发生了某种事件(如进程终止时通知父进程);
  • 进程控制,有些进程希望完全控制另一个进程的执行(如debug),此时进程希望能够拦截另一个进程的所有异常,并能够及时知道其状态的改变;

进程间通信的分类

  • 管道
    • 匿名管道pipe
    • 命名管道
  • System V IPC
    • system V 消息队列
    • system V 共享内存
    • system V 信号量
  • POSIX IPC
    • 信息队列
    • 共享内存
    • 信息量
    • 互斥量
    • 调节变量
    • 读写锁

二,管道

        匿名管道pipe、命名管道;

匿名管道

        Linux通过使用竖线(管道符 | )来连接多个命令,以形成一个管道;管道符前面命令的输出作为管道符后面命令的输入,管道中的数据只能单向流动(即半双工通信),要实现双向流动需创建两个管道;另外,此管道为匿名管道,用完即被自动销毁,且只能在父子进程间通信;

[wz@192 Desktop]$ cat test.c | grep main
int main() 

        父进程需读写都打开文件,这样子进程继承时才会有读写,然后通过关闭父子进程对应的读写,来实现信息的传输;不关闭相应的读写,也可通信,但一般关闭防止误操作;

管道函数 pipe

  • 创建匿名管道
    • 创建成功,返回0;
    • 创建失败,返回-1;

pipefd为文件描述符数组

  • pipefd[0],指定管道读端,默认值为3;
  • pipefd[1],指定管道写端,默认值为4;
#include <stdio.h>    
#include <unistd.h>    
    
int main()    
{    
  int pipefd[2];    
  if(pipe(pipefd) < 0)    
  {    
    perror("pipe");                                                                   
    return 1;    
  }    
  printf("pipefd[0]: %d\n", pipefd[0]);    
  printf("pipefd[1]: %d\n", pipefd[1]);    
  return 0;    
}   
[wz@192 pipe]$ ./test
pipefd[0]: 3
pipefd[1]: 4
int main()      
{      
  int pipefd[2];      
  if(pipe(pipefd) < 0){      
    perror("pipe");      
    return 1;      
  }      
  char buf[32];      
  write(pipefd[1],"hellopipe",32); //向管道内写
  read(pipefd[0],buf,32); //从管道内读     
  printf("buf: %s\n", buf);                                                           
  return 0;      
}   
[wz@192 pipe]$ ./test 
buf: hellopipe

//子进程写入,父进程读取                                                                                     
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
  
int main()
{
    int pipefd[2];
    if(pipe(pipefd) < 0){
        perror("pipe");
        return 1;
    }
  
    pid_t id = fork();
    if(id < 0){
        perror("fork");
        return 1;
    }
  
    else if(id == 0){
        close(pipefd[0]);
        char* msg = "child msg";
        int count=5;
        while(count){
            printf("child write: %s\n",msg);
            write(pipefd[1],msg,strlen(msg));
            sleep(1);
            count--;
       }
       close(pipefd[1]);
       exit(0);
    }
    
    else{
        char buf[64];
        close(pipefd[1]);
        while(1){
            ssize_t sz=read(pipefd[0],buf,sizeof(buf)-1);
            if(sz>0){
                buf[sz]=0;
                printf("father read: %s\n",buf);
            }
            else if(sz==0){
                printf("pipe file empty!\n");
                break;
            }
        }
        close(pipefd[0]);
        printf("close read\n");

        int status = 0;    
        pid_t wait_pid = waitpid(id,&status,0);    
        if(WIFEXITED(status) && wait_pid==id)    
            printf("child exit normal, exit code: %d\n", WEXITSTATUS(status));    
        else     
            printf("child exit error, exit sig: %d\n", WTERMSIG(status));  
    }
    return 0;
}
  • 如管道为空,则读端需等待数据就绪,即read阻塞;
  • 如管道在写端已写满,需等待管道有空闲空间才可继续写入,即write阻塞;
  • 管道自带同步机制;
  • 管道是单向通信的;
  • 管道是面向字节流的;
  • 管道只能保证具有血缘关系的进程通信;
  • 管道可保证一定程度数据读取的原子性;
//子进程持续在写入,父进程关闭读
//此时OS会直接关闭子进程
    else{
        char buf[64];
        close(pipefd[1]);
        while(1){
            ssize_t sz=read(pipefd[0],buf,sizeof(buf)-1);
            if(sz>0){
                buf[sz]=0;
                printf("father read: %s\n",buf);
                close(pipefd[0]);
                break;
            }
            else if(sz==0){
                printf("pipe file empty!\n");
                break;
            }
        }
        printf("close read\n");

        int status = 0;    
        pid_t wait_pid = waitpid(id,&status,0);    
        if(WIFEXITED(status) && wait_pid==id)    
            printf("child exit normal, exit code: %d\n", WEXITSTATUS(status));    
        else     
            printf("child exit error, exit sig: %d\n", WTERMSIG(status));  
    }
//子进程退出信号为13,即SIGPIPE
[wz@192 pipe]$ ./test 
child write: child msg
father read: child msg
close read
child write: child msg
child exit error, exit sig: 13
[wz@192 pipe]$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7154
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 4096
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

命名管道

        命名管道是一种特殊类型的文件,可在不相关进程间交换数据,使用FIFO文件实现;

使用命令mkfifo,创建命名管道

[wz@192 pipe]$ mkfifo pipefile
[wz@192 pipe]$ ll pipefile
prw-rw-r--. 1 wz wz 0 8月  18 08:24 pipefile

使用函数mkfifo,创建命名管道

匿名管道与命名管道区别

  • 匿名管道由函数pipe创建并打开;
  • 命名管道由函数或命令mkfifo创建,再由open打开;
  • 唯一区别即创建和打开方式不同;
[wz@192 pipe]$ echo abc > pipefile 
[wz@192 pipe]$ while :; do echo "1,##########"; cat pipefile; echo "2,#########"; sleep 1; done
1,##########
abc
2,#########
1,##########

实现server&client通信

//makefile
.PHONY:all
all: server client
    
server:server.c
  gcc -o $@ $^    
client:client.c
  gcc -o $@ $^    
    
.PHONY:clean
clean:
  rm -rf server client  
//server.c 
//创建命名管道,并读
#include <stdlib.h>    
#include <string.h>    
#include <unistd.h>    
#include <sys/types.h>    
#include <sys/stat.h>    
#include <fcntl.h>    
    
int main()    
{    
  int ret = mkfifo("pipefile", 0644);    
  if(ret == -1){    
    perror("mkfifo");    
    return 1;    
  }    
  int pipefd = open("pipefile", O_RDONLY);    
  if(pipefd < 0){    
    perror("open");    
    return 2;    
  }    
  char msg[64]={0};    
  while(1){    
    printf("please wait ...\n");    
    ssize_t sz = read(pipefd, msg, sizeof(msg)-1);    
    if(sz > 0){    
      msg[sz]=0;    
      printf("server read: %s\n", msg);                                                                        
    }    
    else if(sz == 0){    
      printf("client quit!\n");    
      break;    
    }    
    else{    
      perror("read");    
      return 3;    
    }    
  }    
  close(pipefd);    
  return 0;    
}  
//client.c
//向管道写入
#include <stdlib.h>    
#include <string.h>    
#include <unistd.h>    
#include <sys/types.h>    
#include <sys/stat.h>    
#include <fcntl.h>    
    
int main()    
{    
  int pipefd = open("pipefile", O_WRONLY);    
  if(pipefd < 0){    
    perror("open");    
    return 1;    
  }    
    
  char msg[64]={0};    
  while(1){    
    printf("please write ...\n");    
    ssize_t sz = read(0, msg, sizeof(msg)-1);    
    if(sz > 0){    
      msg[sz]=0;    
      write(pipefd, msg, strlen(msg));    
    }    
    else if(sz == 0){    
      printf("client read empty!\n");    
      break;    
    }    
    else{    
      perror("client read\n");    
      return 2;    
    }                                                                                                          
  }    
  close(pipefd);    
  return 0;    
} 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/79754.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

高效使用ChatGPT之ChatGPT客户端

ChatGPT客户端&#xff0c;支持Mac, Windows, and Linux 下载地址见文章结尾 软件截图 Windows: Mac&#xff1a; 说明 chatgpt桌面版&#xff0c;相比于网页版的chatgpt&#xff0c;最大的特色是支持历史聊天对话记录导出&#xff0c;且支持三种格式&#xff1a;PNG、PDF、…

如何使用 ChatGPT 将文本转换为 PowerPoint 演示文稿

推荐&#xff1a;使用 NSDT场景编辑器 助你快速搭建可二次编辑的3D应用场景 步骤 1&#xff1a;将文本转换为幻灯片演示文稿 第一步涉及指示 ChatGPT 根据给定的文本生成具有特定数量幻灯片的演示文稿。首先&#xff0c;您必须向 ChatGPT 提供要转换的文本。 使用以下提示指示…

控制方法笔记

基于模型的控制&#xff1a;LQR&#xff0c;模型建立如果不准确&#xff0c;会给控制带来不确定性。 运动学和动力学&#xff1f; 大货车很多参数不了解的话&#xff0c;有时候不如用运动学。所以说&#xff0c;建模不精准不如用运动学。 LQR 模型是状态空间线性的。目标函…

Harvard transformer NLP 模型 openNMT 简介入门

项目网址&#xff1a; OpenNMT - Open-Source Neural Machine Translation logo&#xff1a; 一&#xff0c;从应用的层面先跑通 Harvard transformer GitHub - harvardnlp/annotated-transformer: An annotated implementation of the Transformer paper. ​git clone https…

【脚踢数据结构】查找

(꒪ꇴ꒪ )&#xff0c;Hello我是祐言QAQ我的博客主页&#xff1a;C/C语言&#xff0c;Linux基础&#xff0c;ARM开发板&#xff0c;软件配置等领域博主&#x1f30d;快上&#x1f698;&#xff0c;一起学习&#xff0c;让我们成为一个强大的攻城狮&#xff01;送给自己和读者的…

JDBC配置文件抽取-spring11

改成context,到这里我们context命名空间就引入完毕&#xff0c;加载我们外部properties配置文件&#xff1a; 用它&#xff1a;第一个属性&#xff0c;第二个类型 在未加载路径下&#xff1a; 现在我已经把spring加载到配置文件里了。 现在我需要在这个位置引入proper…

04 qt功能类、对话框类和文件操作

一 QT中时间和日期 时间 ---- QTime日期 ---- QDate对于Qt而言,在实际的开发过程中, 1)开发者可能知道所要使用的类 ---- >帮助手册 —>索引 -->直接输入类名进行查找 2)开发者可能不知道所要使用的类,只知道开发需求文档 ----> 帮助 手册,按下图操作: 1 …

人类反馈强化学习RLHF;微软应用商店推出AI摘要功能

&#x1f989; AI新闻 &#x1f680; 微软应用商店推出AI摘要功能&#xff0c;快速总结用户对App的评价 摘要&#xff1a;微软应用商店正式推出了AI摘要功能&#xff0c;该功能能够将数千条在线评论总结成一段精练的文字&#xff0c;为用户选择和下载新应用和游戏提供参考。该…

小程序中display:flex和v-show,v-show不生效,uni-app

小程序中display:flex和v-show&#xff0c;v-show不生效、、 解决方案&#xff1a; display&#xff1a;flex样式的优先级高于了v-show &#xff0c;v-show其实就是display&#xff1a;none&#xff0c;display&#xff1a;flex优先级高于display&#xff1a;none。 使用 :s…

opencv 矩阵运算

1.矩阵乘&#xff08;*&#xff09; Mat mat1 Mat::ones(2,3,CV_32FC1);Mat mat2 Mat::ones(3,2,CV_32FC1);Mat mat3 mat1 * mat2; //矩阵乘 结果 2.元素乘法或者除法&#xff08;mul&#xff09; Mat m Mat::ones(2, 3, CV_32FC1);m.at<float>(0, 1) 3;m.at…

(stm32)低功耗模式

低功耗模式 执行哪个低功耗模式的程序判断流程 标志位设置操作一定要在WFI/WFE之前&#xff0c;调用此指令后立即进入睡眠判断流程 模式对比 睡眠模式 停止模式 待机模式

中间件的介绍

1.1 什么是中间件 中间件是介于应用系统和系统软件之间的一类软件&#xff0c;他使用系统软件所提供的基础服务&#xff0c;衔接网络上应用系统的各个部分或不同的应用&#xff0c;能够达到资源共享、功能共享的目的。 例如MySQL就可以看作是具备中间件特性的一种技术&#x…

centos下使用jemalloc解决Mysql内存泄漏问题

参考&#xff1a; MySQL bug&#xff1a;https://bugs.mysql.com/bug.php?id83047&tdsourcetags_pcqq_aiomsg https://github.com/jemalloc/jemalloc/blob/dev/INSTALL.md &#xff08;1&#xff09;ptmalloc 是glibc的内存分配管理 &#xff08;2&#xff09;tcmalloc…

Ubuntu软件源、pip源大全,国内网站网址,阿里云、网易163、搜狐、华为、清华、北大、中科大、上交、山大、吉大、哈工大、兰大、北理、浙大

文章目录 一、企业镜像源1、阿里云2、网易1633、搜狐镜像4、华为 二&#xff1a;高校镜像源1、清华源2、北京大学3、中国科学技术大学源 &#xff08;USTC&#xff09;4、 上海交通大学5、山东大学6、 吉林大学开源镜像站7、 哈尔滨工业大学开源镜像站8、 西安交通大学软件镜像…

Android Retrofit原理浅析

官方地址:Retrofit 原理:Retrofit 本质上是代理了OKhttp,使用代理模式,Type-Safe 类型安全 编译器把类型检查出 避免类型错误, enqueue 异步 切换线程 execute 同步 不切换线程 enqueue:Call接口定义的抽象方法 Retrofit.Create() 方法首先验证接口validateServiceInterf…

RGOS日常管理操作

RGOS日常管理操作 一、前言二、RGOS平台概述2.1、锐捷设备的常用登陆方式2.2、使用Console登入2.3、Telnet远程管理2.4、SSH远程管理2.5、登陆软件&#xff1a;SecureCRT 三、CLI命令行操作3.1、CLI命令行基础3.2、CLI模式3.3、CLI模式互换3.4、命令行特性3.4.1、分屏显示3.4.2…

0基础入门C++之类和对象上篇

目录 1.面向过程和面向对象初步认识2.类的引入3.类的定义3.1类的两种定义方式:3.2成员变量命名规则的建议 4.类的访问限定符及封装4.1访问限定符4.2封装 5.类的作用域6.类的实例化7.类对象模型7.1如何计算类对象的大小7.2 类对象的存储方式猜测 8.this指针8.1this指针的引出8.2…

通过请求头传数据向后端发请求

axios &#xff08;get post请求、头部参数添加&#xff09;傻瓜式入门axios_axiospost请求参数_web_blog的博客-CSDN博客

http库 之 OKHttpUtil

源码位置 方便实用&#xff0c;个人感觉不错 依赖 <dependency><groupId>io.github.admin4j</groupId><artifactId>common-http-starter</artifactId><version>0.7.5</version> </dependency>代码实践 /*** 通用http的pos…

在 IDEA 中使用 Git开发 图文教程

在 IDEA 中使用 Git开发 图文教程 一、连接远程仓库二、IDEA利用Git进行开发操作三、分支操作3.1 新建分支3.2 切换分支3.3 删除分支3.4 比较分支3.5 合并分支 四、常用快捷键 一、连接远程仓库 一、打开IDEA&#xff0c;进入目录&#xff1a;File ->New ->Project from…
最新文章