C++ 中消息队列函数实例详解

 更新时间:2017年06月12日 17:02:57   作者:墨言莫问  
这篇文章主要介绍了C++ 中消息队列函数实例详解的相关资料,需要的朋友可以参考下

C++ 中消息队列函数实例详解

1.消息队列结构体的定义

typedef struct{
    uid_t  uid;  /* owner`s user id */
    gid_t  gid;  /* owner`s group id */
    udi_t  cuid;  /* creator`s user id */
    gid_t  cgid;  /* creator`s group id */
    mode_t mode;  /* read-write permissions 0400 MSG_R 0200 MSG_W*/
    ulong_t seq;  /* slot usage sequence number*/
}ipc_perm;

typedef stuct{
    struct ipc_perm msg_perm;   /* read_write perms */
    struct msg   *msg_first;   /* ptr to first message on queue */
    struct msg   *msg_last;   /* ptr to last message on queue */
    msglen_t     msg_cbytes;   /* used bytes current on queue */
    msgqnum_t    msg_qnum;    /* current num of message on queue */
    msglen_t    msg_qbytes;   /* max # of bytes allowed on queue */
    pid_t        msg_lspid;   /* pid of last msgsnd() */
    pid_t        msg_lrpid;   /* pid of last msgrcv() */
    time_t       msg_stime;   /* time of last msgsnd() */
    time_t       msg_rtime;   /* time of last msgrcv() */
    time_t       msg_ctime;   /* time of last msgctl() */
}msqid_ds;

typedef struct
{
    long mtype;
    char mbuf[MSGLEN];
}Message;

2.创建消息队列:

   /***************************************************
Function:
    int msgget(ket_t key,int oflag);
Explain:
    create or view a message queue
Return :
    a int indetify
Include:
    sys/msg.h
introduction:
    oflag: 0400 msg_r 
        0200 msg_w
        0600 msg_wr
    ipc_creat: NO exist and then creat a queue
          exist : reference a queue
    ipc_creat|ipc_excl: NO exist and then creat a queue
              exist : return error
****************************************************/
#include<stdio.h>
#include<sys/msg.h>
#include<stdlib.h>

int MsgGet(int key)
{
    int ret;
    ret=msgget(key,0600|IPC_CREAT);
//   ret=msgget(key,0600|IPC_CREAT|IPC_EXCL);
    if(ret<0)
        perror("creat msgid error");
    printf("msgid=%d/n",ret);
    system("ipcs -q -i ret");
    return ret;
}
int main(int argc,char *agrv[])
{
    int key;
    printf("pleasse input msgkey:");
    scanf("%d",&key);
    MsgGet(key);
    return 0;
}

3.向消息队列中发送消息msgsnd

/***********************************************************************************
Function:
    int msgsnd(int msqid,const void *ptr,size_t length,int flag)
Explain:
    send a message to a queue
Return:
    len: send message len;
Include:
    sys/msg.h
Introduction:
    flag: 0 : if queue full wait:1>具备存放新消息的空间
                   2>由msqid标识的消息队列从系统中删除(返回EIDRM错误)
                   3>调用线程被某个捕获的信号所中断(返回EINTR错误)
       IPC_NOWAIT:如果没有存放新消息的空间,函数马上返回
                   1>指定的队列中有太多的字节
                   2>在系统范围存在太多的消息
*****************************************************************************************/
#include "typemsg.h"
int MsgSnd(int msqid,char *buf,int len,int flag)
{
    int ret;
    ret=msgsnd(msqid,buf,len,flag);
    if(ret<0)
        perror("msgsnd error");
    system("ipcs -q");
    return ret;
}

int main()
{
    int msqid,len,stype;
    Message msgb;
    memset(&msgb,0,sizeof(Message));
    printf("msgsnd:please input msqid:");
    scanf("%d",&msqid);
    printf("please input msgtype:");
    scanf("%d",&stype);
    msgb.mtype=stype;
    strcpy(msgb.mbuf,"zhangweia");
    MsgSnd(msqid,(char *)&msgb,sizeof(Message),0);
    return 0;
}

4.从队列中获取消息 msgrcv

/*********************************************************************
Function:
    int msgrcv(int msqid,const void *ptr,size_t msglen,long type,int flag)
Explain:
    recv message order by type 
    msgrcv error: Argument list too long --> msglen的长度小于消息体中消息的长度
Para :
    ptr: point to message struct 
    msglen: 由ptr指向的缓冲区中数据部分的大小,这个是该函数能够返回的最大数据量
    type: message type;

              1> 0:返回队列中最早的消息
              2> 大于0:返回消息队列中类型为type的第一个消息
              3> 小于0:返回消息队列中类型小于或者等于type的绝对值的消息类型中最小的第一个消息
    flag: 0<wait> 没有消息或者消息类型不符合的时候,线程等待
         响应: 1>有一个所请求类型的消息可以获取
            2>msqid的消息队列被系统删除,返回一个EIDRM
            3>调用线程被某个捕获的信号所中断
       IPC_NOWAIT:在没有数据的情况下,立即返回一个ENOMSG错误
       MSGNOERROR:当所接受的消息数据部分大于msglen长度时,获取截短的数据部分,否则返回E2BIG错误
Return:
    message len
*********************************************************************/
#include "typemsg.h"
int MsgRcv(int msqid,char *buf,int msglen,long type,int flag)
{
    int ret;
    ret=msgrcv(msqid,buf,msglen,type,flag);
    if(ret<0)
    perror("msgrcv error");
    system("ipcs -q");
    return ret;

}

int main()
{
    int msqid,len;
    long ttype;
    Message mbuf;
    printf("msgrcv:please input recv msqid:");
    scanf("%d",&msqid);
    MsgRcv(msqid,(char *)&mbuf,8900,0,IPC_NOWAIT);
    printf("recv message=%s/n",mbuf.mbuf);
    Put_String((unsigned char *)&mbuf,sizeof(Message));
    return 0;
}

6.消息队列的控制msgctl

/**********************************************************
Function:
    int msgctl(int msqid,int cmd,struct msqid_ds *buff)
Explain:
    cdm: IPC_RMID; delete msqid 
       IPC_SET: 
       IPC_STAT: return msqid stat

*********************************************************/
#include "typemsg.h"
int MsgCtl(int msqid,int cmd,struct msqid_ds *buff)
{
    int ret;
    ret=msgctl(msqid,cmd,buff);
    if(ret<0)
    {
        perror("msgctl error");
        return -1;
    }
    return 0;
}

int main()
{
    int msqid,type;
    struct msqid_ds info;
    printf("please input msqid /nand type(1:icp_rmid;2:ipc_stat)");
    scanf("%d%d",&msqid,&type);
    if(type==1)
    {
        MsgCtl(msqid,IPC_RMID,NULL);
        printf("delete queue success:%d/n",msqid);
    }else if(type==2)
    {
        MsgCtl(msqid,IPC_STAT,&info);
        printf("get queue stat:%d/n",msqid);
    }
    return 0;

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • C/C++字符串函数之复制函数详解

    C/C++字符串函数之复制函数详解

    下面小编就为大家带来一篇C/C++字符串函数之复制函数详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • 带你了解C++中vector的用法

    带你了解C++中vector的用法

    大家好,本篇文章主要讲的是带你了解C++中vector的用法,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • Qt定时器和随机数详解

    Qt定时器和随机数详解

    在前一篇中我们介绍了键盘和鼠标事件,其实还有一个非常常用的事件,就是定时器事件,如果要对程序实现时间上的控制,那么就要使用到定时器。而随机数也是很常用的一个功能,在我们要想产生一个随机的结果时就要使用到随机数。本文我们就来简单介绍一下定时器和随机数。
    2015-06-06
  • C++实现涂色游戏(博弈)

    C++实现涂色游戏(博弈)

    这篇文章主要为大家详细介绍了C++实现涂色游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • C++迭代器和显示类型转换方式

    C++迭代器和显示类型转换方式

    这篇文章主要介绍了C++迭代器和显示类型转换方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • 带你了解C++this指针的用法及其深究

    带你了解C++this指针的用法及其深究

    这篇文章主要介绍了C++中this指针的用法,对初学者而言是非常重要的概念,必须加以熟练掌握,需要的朋友可以参考下,希望能给你带来帮助
    2021-08-08
  • C++遍历文件夹下的所有文件

    C++遍历文件夹下的所有文件

    数据分多个文件存储,读取数据就需要对多个文件进行操作。下面通过实例代码给大家讲解C++遍历文件夹下的所有文件,感兴趣的的朋友一起看看吧
    2017-08-08
  • C++实现日期类的方法详解

    C++实现日期类的方法详解

    这篇文章主要给大家介绍了C++实现日期类的方法,文中通过代码示例给大家介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-01-01
  • 关于C语言和命令行之间的交互问题

    关于C语言和命令行之间的交互问题

    这篇文章主要介绍了C语言和命令行之间的交互,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-07-07
  • C++ string 字符串查找匹配实例代码

    C++ string 字符串查找匹配实例代码

    下面小编就为大家带来一篇C++ string 字符串查找匹配实例代码。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-10-10

最新评论