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

复习 --- 消息队列

进程间通信机制(IPC)

简述

IPC:Inter Process Communication

进程和进程之间的用户空间相互独立,但是4G内核空间共享,进程间的通信就是通过这4G的内核空间

分类

传统的进程间通信机制

无名管道(pipe)

有名管道(fifo)

信号(signal)

System V中的IPC对象和IPC的区别

消息队列(message queue)

共享内存(shared memory)

信号灯集(semaphore)

可用于主机传输的通信机制

套接字(socket)

消息队列

概念

在内核内存中创建一个队列,进程需要将数据打包成结点,添加到队尾中,或者从队列中读取结点,可以通过消息类型进行消息分类

特点

需要打包,有特定的格式以及消息类型

按照先进先出原则,但是也可以限制消息类型读取

独立于进程,灯进程结束后,消息队列以及其中的内容不会删除,除非中期操作系统或者手动删除

在Linux系统下,可以通过命令ipcs查看消息队列,通过ipcrm -q msqid删除消息队列

函数

ftok()函数,获得一个Key值用来创建消息队列,通过相同的key值可以访问对应的消息队列

msgget()函数创建一个消息队列获得起id号

msgsnd()发送消息到指定消息队列

msgrcv()获取对应消息队列中对应的消息

msgctl()控制消息队列,常用于删除消息队列

#include<myhead.h>
#include<sys/msg.h>
#include<sys/ipc.h>
struct msgbuf
{long mtype;      //消息类型char mtext[128]; //消息内容
};//线程1函数
void *task1(void *arg)
{int msqid = *(int *)arg;//获取消息队列id号struct msgbuf msbuf;//声明消息结构体printf("A:\n\t");fflush(stdout);while (1){msbuf.mtype = 1;//消息类型fgets(msbuf.mtext,sizeof(msbuf.mtext),stdin);msbuf.mtext[strlen(msbuf.mtext)-1]='\0';if(msgsnd(msqid,&msbuf,sizeof(msbuf)-sizeof(long),0) == -1){perror("msgsnd");return NULL;}//printf("线程%d:发送成功\n",getpid());printf("A:\n\t");fflush(stdout);if (!strcmp(msbuf.mtext,"quit")){system("clear");exit(0);}}pthread_exit(NULL);
}//线程2函数
void *task2(void *arg)
{int msqid = *(int *)arg;struct msgbuf buf;ssize_t num = 0;while (1){bzero(&buf,sizeof(buf));if ((num = msgrcv(msqid,&buf,sizeof(buf.mtext),2,0))<0){//perror("msgrcv");return NULL;}printf("\nB:\n\t%s\n",buf.mtext);printf("A:\n\t");fflush(stdout);if (!strcmp(buf.mtext,"quit")){msgctl(msqid,IPC_RMID,NULL);system("clear");exit(0);}}pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{key_t key = ftok("./",0);if (key == -1){perror("ftok");return -1;/* code */}umask(0);int msqid = msgget(key,IPC_CREAT|0664);if (msqid == -1){perror("msgget");return -1;}pthread_t tid1,tid2;if (pthread_create(&tid1,NULL,task1,&msqid) != 0){printf("线程1创建失败\n");}if (pthread_create(&tid2,NULL,task2,&msqid) != 0){printf("线程1创建失败\n");}pthread_join(tid1,NULL);pthread_join(tid2,NULL);return 0;
}
#include<myhead.h>
//#include<sys/msg.h>
//#include<sys/ipc.h>
struct msgbuf
{long mtype;      //消息类型char mtext[128]; //消息内容
};//线程1函数
void *task1(void *arg)
{int msqid = *(int *)arg;struct msgbuf msbuf;printf("B:\n\t");fflush(stdout);while (1){msbuf.mtype = 2;fgets(msbuf.mtext,sizeof(msbuf.mtext),stdin);msbuf.mtext[strlen(msbuf.mtext)-1]='\0';if(msgsnd(msqid,&msbuf,sizeof(msbuf)-sizeof(long),0) == -1){perror("msgsnd");return NULL;}//printf("线程%d:发送成功\n",getpid());printf("B:\n\t");fflush(stdout);if (!strcmp(msbuf.mtext,"quit")){system("clear");exit(0);}}pthread_exit(NULL);
}//线程2函数
void *task2(void *arg)
{int msqid = *(int *)arg;struct msgbuf buf;ssize_t num = 0;while (1){bzero(&buf,sizeof(buf));if ((num = msgrcv(msqid,&buf,sizeof(buf.mtext),1,0))<0){//perror("msgrcv");return NULL;}printf("\nA:\n\t%s\n",buf.mtext);printf("B:\n\t");fflush(stdout);if (!strcmp(buf.mtext,"quit")){msgctl(msqid,IPC_RMID,NULL);system("clear");exit(0);}}pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{key_t key = ftok("./",0);if (key == -1){perror("ftok");return -1;/* code */}umask(0);int msqid = msgget(key,IPC_CREAT|0664);if (msqid == -1){perror("msgget");return -1;}pthread_t tid1,tid2;if (pthread_create(&tid1,NULL,task1,&msqid) != 0){printf("线程1创建失败\n");}if (pthread_create(&tid2,NULL,task2,&msqid) != 0){printf("线程1创建失败\n");}pthread_join(tid1,NULL);pthread_join(tid2,NULL);return 0;
}

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

相关文章:

  • AcWing 288. 休息时间,《算法竞赛进阶指南》
  • ES6中字符串的扩展
  • GEO生信数据挖掘(四)数据清洗(离群值处理、低表达基因、归一化、log2处理)
  • CI/CD工具中的CI和CD的含义
  • 用go获取IPv4地址,WLAN的IPv4地址,本机公网IP地址详解
  • Android自定义Drawable---灵活多变的矩形背景
  • ParagonNTFSforMac_15.5.102中文版最受欢迎的NTFS硬盘格式读取工具
  • Kafka 搭建过程
  • 七、2023.10.1.Linux(一).7
  • 一文教你搞懂Redis集群
  • 树上启发式合并 待补
  • minio分布式文件存储
  • Linux新的IO模型io_uring
  • FFmpeg 命令:从入门到精通 | FFmpeg 基本介绍
  • 数组篇 第一题:删除排序数组中的重复项
  • 堆的初步认识
  • CycleGAN模型之Pytorch实战
  • C++(STL容器适配器)
  • 软考 系统架构设计师系列知识点之软件架构风格(7)
  • 【Vue3】自定义指令
  • UG\NX CAM二次开发 加工模块获取 UF _ask_application_module
  • 借助GPU算力编译Android
  • docker-compose一键部署mysql
  • MATLAB 函数签名器
  • 2019强网杯随便注bugktu sql注入
  • Html+Css+Js计算时间差,返回相差的天/时/分/秒(从未来的一个日期时间到当前日期时间的差)。
  • mybatis项目启动报错:reader entry: ���� = v
  • 【GIT版本控制】--什么是版本控制
  • ChatGPT付费创作系统V2.3.4独立版 +WEB端+ H5端 + 小程序最新前端
  • GEE16: 区域日均降水量计算