C++获取消息队列的属性实战

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/89046647

一 代码

#include <sys/types.h>
#include <sys/msg.h>
#include <unistd.h>
#include <ctime>
#include "stdio.h"
#include "errno.h"
void msg_stat(int,struct msqid_ds );
main()
{
    int gflags,sflags,rflags;
    key_t key;
    int msgid;
    int reval;
    struct msgsbuf{
            int mtype;
            char mtext[1];
        }msg_sbuf;
    struct msgmbuf
        {
        int mtype;
        char mtext[10];
        }msg_rbuf;
    struct msqid_ds msg_ginfo;
    char  msgpath[]="./test";

    key=ftok(msgpath,'b');
    gflags=IPC_CREAT|IPC_EXCL;
    msgid=msgget(key,gflags|00666);
    if(msgid==-1)
    {
        printf("msg create error\n");
    }
    //创建一个消息队列后,输出消息队列缺省属性
    msg_stat(msgid,msg_ginfo);
    sflags=IPC_NOWAIT;
    msg_sbuf.mtype=10;
    msg_sbuf.mtext[0]='a';
    reval=msgsnd(msgid,&msg_sbuf,sizeof(msg_sbuf.mtext),sflags);
    if(reval==-1)
    {
        printf("message send error\n");
    }
    //发送一个消息后,输出消息队列属性
    msg_stat(msgid,msg_ginfo);

     
    reval=msgctl(msgid,IPC_RMID,NULL);//删除消息队列
    if(reval==-1)
    {
        printf("unlink msg queue error\n");
    }
}
void msg_stat(int msgid,struct msqid_ds msg_info)
{
    int reval;
    sleep(1);//只是为了后面输出时间的方便
    reval=msgctl(msgid,IPC_STAT,&msg_info);
    if(reval==-1)
    {
        printf("get msg info error\n");
    }
    printf("\n");
    printf("current number of bytes on queue is %d\n",msg_info.msg_cbytes);
    printf("number of messages in queue is %d\n",msg_info.msg_qnum);
    printf("max number of bytes on queue is %d\n",msg_info.msg_qbytes);
    //每个消息队列的容量(字节数)都有限制MSGMNB,值的大小因系统而异。在创建新的消息队列时,//msg_qbytes的缺省值就是MSGMNB
    printf("pid of last msgsnd is %d\n",msg_info.msg_lspid);
    printf("pid of last msgrcv is %d\n",msg_info.msg_lrpid);
    printf("last msgsnd time is %s", ctime(&(msg_info.msg_stime)));
    printf("last msgrcv time is %s", ctime(&(msg_info.msg_rtime)));
    printf("last change time is %s", ctime(&(msg_info.msg_ctime)));
    printf("msg uid is %d\n",msg_info.msg_perm.uid);
    printf("msg gid is %d\n",msg_info.msg_perm.gid);
}

二 结果

[root@localhost test]# g++ test.cpp -o test
[root@localhost test]# ./test

current number of bytes on queue is 0
number of messages in queue is 0
max number of bytes on queue is 16384
pid of last msgsnd is 0
pid of last msgrcv is 0
last msgsnd time is Thu Jan  1 08:00:00 1970
last msgrcv time is Thu Jan  1 08:00:00 1970
last change time is Fri Apr  5 19:11:13 2019
msg uid is 0
msg gid is 0

current number of bytes on queue is 1
number of messages in queue is 1
max number of bytes on queue is 16384
pid of last msgsnd is 1055
pid of last msgrcv is 0
last msgsnd time is Fri Apr  5 19:11:15 2019
last msgrcv time is Thu Jan  1 08:00:00 1970
last change time is Fri Apr  5 19:11:13 2019
msg uid is 0
msg gid is 0

三 说明

可以看到,刚开始时,消息队列的消息是0,发送一个后,队列里面的消息个数就变成1了。因为我们发送的消息是字符“a”,长度是1,所以消息队列的长度就是一个字节。

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/89046647