Example of message queue function in C++

  • 2020-05-19 05:27:48
  • OfStack

Example of message queue function in C++

1. Definition of message queue structure


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. Create a message queue:


   /***************************************************
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. Send message msgsnd to the message queue


/***********************************************************************************
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> Have space for new messages 
                   2> by msqid The identified message queue is deleted from the system ( return EIDRM error )
                   3> The calling thread is interrupted by a captured signal ( return EINTR error )
       IPC_NOWAIT: If there is no room for new messages , The function returns immediately 
                   1> There are too many bytes in the specified queue 
                   2> There are too many messages system-wide 
*****************************************************************************************/
#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;
}

Get the message msgrcv from the queue


/*********************************************************************
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 Is smaller than the length of the message in the message body 
Para :
    ptr: point to message struct 
    msglen:  by ptr The size of the data portion of the buffer pointed to , This is the maximum amount of data that this function can return 
    type: message type;

              1> 0: Returns the earliest message in the queue 
              2>  Is greater than 0: The type in the return message queue is type The first 1 A message 
              3>  Less than 0: Returns the type in the message queue less than or equal to type The absolute value of the smallest of the message types 1 A message 
    flag: 0<wait>  When there is no message or the message type does not match , Thread waiting 
          The response : 1> There are 1 Two messages of the requested type can be retrieved 
            2>msqid The message queue was deleted by the system , return 1 a EIDRM
            3> The calling thread is interrupted by a captured signal 
       IPC_NOWAIT: In the absence of data , Return immediately 1 a ENOMSG error 
       MSGNOERROR: When the portion of the message data received is greater than msglen When the length of the , Get the truncated part of the data , Otherwise returns E2BIG error 
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. Message queue control 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;

}

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: