首页 > 其他分享 >消息队列的创建,删除,发送,读取(含概念)

消息队列的创建,删除,发送,读取(含概念)

时间:2024-07-10 17:57:23浏览次数:11  
标签:sendbuf mtest 读取 队列 msgid 发送 printf message include

 消息队列

 1.特点

 2.相关函数

 

 

 

 ps:

 消息队列实现单个进程的发送和读取

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>

struct msgbuf
{
   long mtype;//消息类型
   char mtest[128];//消息内容容器
   char ID[4];	
};


int main()
{
  struct msgbuf sendbuf,readbuf;
  int msgid;//消息队列id
  int readret;//读取到的字节数
  msgid = msgget(IPC_PRIVATE,0755);
  if(msgid == -1)
  {
      printf("create message queue failed !\n");
      return -1;

  }
 
  printf("create message queue sucess ,msgid = %d\n",msgid);
  system("ipcs -q");//终端打印出消息队列详细信息
  

  //init msgbuf

  sendbuf.mtype = 100;
  printf("please input to message queue :\n");
  fgets(sendbuf.mtest,128,stdin);//手动键盘输入内容

  //send message to message queue
  msgsnd(msgid,(void *)&sendbuf,strlen(sendbuf.mtest),0);

  //read
  memset(readbuf.mtest,0,128);//把readbuf中内容全部清零
  readret = msgrcv(msgid,(void *)&readbuf,128,100,0);//读取
  printf("message is %s\n",readbuf.mtest);
  printf("total have %d byte\n",readret);

  
  return 0;
}

运行结果:创建了id为524290的消息队列,键盘输入ni到该队列中,读取到内容为ni,并且为3个字节(还包含结束符‘\0’)。

我们再运行一次,发现524290消息队列中字节数为0,明明刚刚我们写入了内容,为什么此时为0呢???

原因:消息队列的性质就是如此,当从其中读取完数据后,他就会把该节点中的内容全部清除,但是该节点仍然存在鱼内核中。

ftok函数介绍

消息队列实现多个进程间的数据发送和读取

两个进程代码如下:

/*************msg_write.c*************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>

struct msgbuf
{
   long mtype;
   char mtest[128];
   char ID[4];	
};


int main()
{
  struct msgbuf sendbuf;
  int msgid;
  
  key_t key;
  key = ftok("a.c",1);
  msgid = msgget(key,IPC_CREAT|0755);
  if(msgid == -1)
  {
      printf("create message queue failed !\n");
      return -1;

  }
 
  printf("create message queue sucess ,msgid = %d\n",msgid);
  system("ipcs -q");
  

  //init msgbuf

  sendbuf.mtype = 100;
  while(1)
  {
    memset(sendbuf.mtest,0,128);
    printf("please input to message queue :\n");
    fgets(sendbuf.mtest,128,stdin);
    msgsnd(msgid,(void *)&sendbuf,strlen(sendbuf.mtest),0); //send message to message queue
  }

  return 0;
}











/**************msg_read.c***********/
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>

struct msgbuf
{
   long mtype;
   char mtest[128];
   char ID[4];	
};


int main()
{
  struct msgbuf readbuf;
  int msgid;
  key_t key;
  int readret;
  key = ftok("a.c",1);
  msgid = msgget(key,IPC_CREAT|0755);
  if(msgid == -1)
  {
      printf("create message queue failed !\n");
      return -1;

  }
 
  printf("create message queue sucess ,msgid = %d\n",msgid);
  system("ipcs -q");
  

  //init msgbuf

  readbuf.mtype = 100;


  while(1) //read
  { 
    memset(readbuf.mtest,0,128);//把readbuf中内容全部清零
    readret = msgrcv(msgid,(void *)&readbuf,128,100,0);
    printf("message is %s\n",readbuf.mtest);
    printf("total have %d byte\n",readret);
  }
  
  return 0;
}

 编译:gcc msg_write.c -o msg_write

      gcc msg_read.c -o msg_read

打开两个终端,分别运行 ./msg_write  和   ./msg_read

运行结果如下:

标签:sendbuf,mtest,读取,队列,msgid,发送,printf,message,include
From: https://blog.csdn.net/m0_57257049/article/details/140327958

相关文章

  • JS | fetch发送post请求
    在使用fetchAPI发送POST请求时,通常需要指定请求的方法为"POST",并且通过body参数传递要发送的数据。示例代码下面是一个简单的示例,演示如何使用fetchAPI发送POST请求:consturl='/api/endpoint';//替换为你的后端接口URLconstrequestData={mmsi:2098......
  • uniapp 封装蓝牙-(给设备发指令,分包数据发送)
    注意事项:1、关键点:初始化蓝牙——》uni.openBluetoothAdapter获取本机蓝牙适配器状态——》uni.getBluetoothAdapterState开始搜索蓝牙设备——》uni.startBluetoothDevicesDiscovery监听蓝牙设备(或者蓝牙设备列表,找到需要的那个)——......
  • 利用SpringBoot+rabbitmq 实现邮件异步发送,保证100%投递成功
    在之前的文章中,我们详细介绍了SpringBoot整合mail实现各类邮件的自动推送服务。但是这类服务通常不稳定,当出现网络异常的时候,会导致邮件推送失败。本篇文章将介绍另一种高可靠的服务架构,实现邮件100%被投递成功。类似的短信自动发送等服务也大体相同。一、先来一张流程图......
  • 28、Django-发送邮件
    SMTP全称是(SimpleMailTransferProtocol)即简单邮件传输协议(端口是25)-他是一组用于从源地址到目的地址传输邮件的规范、通过它来控制邮件的中转-属于推送协议-负责发送IMAP-邮件相关协议(InternetMailAccessProtocol)即交互式邮件访问协议、是一个应用层协议、端口是143-用来从本地邮件客户端(OutlookExporess、Foxmail、MzzillaThunderbird)访问远程服务......
  • opencv读取视频文件夹内视频的名字_时长_帧率_分辨率写入excel-cnblog
    看视频的时候有的视频文件名贼长。想要翻看,在文件夹里根本显示不出来,缩短又会丢失一些信息,所以我写了一份Python代码,直接获取视频的名字,时长,帧率,还有分辨率写到excel里。实际效果如下图。可以看到需要的大致信息都被提取出来了接下来直接上代码importosimportxlsxwr......
  • 用python写一个脚本,读取srt文件中的内容,并打印出重复的内容,且将不重复的内容保存到新
    代码:#定义一个函数来处理文件defprocess_file(src_filename,unique_filename):seen=set()duplicates=set()withopen(src_filename,'r',encoding='utf-8')asfile:forlineinfile:#将读取的行转换为小写,以避免大小写差异导......
  • 树莓派4B-用串口读取JY901S陀螺仪数据
    相关知识介绍陀螺仪是一种用来感测与维持方向的装置,基于角动量的理论设计出来的。陀螺仪主要是由一个位于轴心可以旋转的轮子构成,陀螺仪一旦开始旋转,由于轮子的「角动量」,陀螺仪有抗拒方向改变的趋向。陀螺仪多用于导航、定位等系统JY901S是9轴姿态角度传感器,支持串口和IIC......
  • 【漏洞复现】泛微e-cology——resourceservlet——任意文件读取
    声明:本文档或演示材料仅供教育和教学目的使用,任何个人或组织使用本文档中的信息进行非法活动,均与本文档的作者或发布者无关。文章目录漏洞描述漏洞复现测试工具漏洞描述泛微e-cology是一款由泛微网络科技开发的协同管理平台,支持人力资源、财务、行政等多功能管理......
  • linux 上安装FTP : vsftpd (含常见问题:读取目录列表失败,的处理)
    服务器上有时候需要安装ftp以便调试或给不懂使用服务器命令的同学更新文件 1、安装vsftpdyumupdateyuminstallvsftpd2、编辑配置文件确保以下配置的值和下面一致anonymous_enable=NOlocal_enable=YESwrite_enable=YESchroot_local_user=YES这些配置......
  • Spring 发送邮件
    引入依赖<dependency><groupId>jakarta.mail</groupId><artifactId>jakarta.mail-api</artifactId></dependency><dependency><groupId>org.springframework......