Linux消息队列实践(2)

消息队列函数

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
  int msgget(key_t key, int msgflg);
  int msgctl(int msqid, int cmd, struct msqid_ds *buf);
  int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
  ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);


msgctl函数

功能:获取/设置消息队列的信息

原型:

int msgctl(int msqid, int cmd, struct msqid_ds *buf);

参数:

    msqid: 由msgget函数返回的消息队列标识码

    cmd:是将要采取的动作(见下)

 

返回值:

    成功返回0,失败返回-1

 

cmd:将要采取的动作(有三个可取值),分别如下:


消息队列数据结构

struct msqid_ds
{
    struct ipc_perm msg_perm;     /* Ownership and permissions */
    time_t          msg_stime;    /* Time of last msgsnd(2) */
    time_t          msg_rtime;    /* Time of last msgrcv(2) */
    time_t          msg_ctime;    /* Time of last change */
    unsigned long   __msg_cbytes; /* Current number of bytes in
                                                queue (nonstandard) */
    msgqnum_t       msg_qnum;     /* Current number of messages
                                                in queue */
    msglen_t        msg_qbytes;   /* Maximum number of bytes
                                                allowed in queue */
    pid_t           msg_lspid;    /* PID of last msgsnd(2) */
    pid_t           msg_lrpid;    /* PID of last msgrcv(2) */
};

 

Ipc_perm数据结构

struct ipc_perm
{
    key_t          __key;       /* Key supplied to msgget(2) */
    uid_t          uid;         /* Effective UID of owner */
    gid_t          gid;         /* Effective GID of owner */
    uid_t          cuid;        /* Effective UID of creator */
    gid_t          cgid;        /* Effective GID of creator */
    unsigned short mode;        /* Permissions */
    unsigned short __seq;       /* Sequence number */
};

//实践:IPC_STAT
int main()
{
    int msgid = msgget(0x1234, 0666);
    if (msgid == -1)
    {
        err_exit("msgget error");
    }

    struct msqid_ds buf;
    if (msgctl(msgid,IPC_STAT,&buf) == -1)
    {
        err_exit("msgctl error");
    }

    printf("buf.msg_perm.mode = %o\n",buf.msg_perm.mode);   //%o以八进制打印
    cout << "buf.__msg_cbytes = " << buf.__msg_cbytes << endl;
    cout << "buf.msg_qbytes = " << buf.msg_qbytes << endl;
    cout << "buf.msg_lspid = " << buf.msg_lspid << endl;
}

//实践:IPC_SET,一般需要先获取,然后再设置
int main()
{
    int msgid = msgget(0x1234, 0666);
    if (msgid == -1)
    {
        err_exit("msgget error");
    }

    //获取消息队列的属性
    struct msqid_ds buf;
    if (msgctl(msgid,IPC_STAT,&buf) == -1)
    {
        err_exit("msgctl get error");
    }

    //设置消息队列的属性
    buf.msg_perm.mode = 0644;
    if (msgctl(msgid,IPC_SET,&buf) == -1)
    {
        err_exit("msgctl set error");
    }

    //获取并打印
    if (msgctl(msgid,IPC_STAT,&buf) == -1)
    {
        err_exit("msgctl get error");
    }

    printf("buf.msg_perm.mode = %o\n",buf.msg_perm.mode);   //%o以八进制打印
}

//实践:IPC_RMID,删除消息队列
/**说明:可以通过在多个窗口上运行几个该程序,测试出:
  消息队列并没有运用”引用计数”的功能!
*/
int main()
{
    int msgid = msgget(0x1234, 0666);
    if (msgid == -1)
    {
        err_exit("msgget error");
    }

    int choice = 0;
    cout << "Please input Your choice: 0-delete, other-continue: ";
    cin >> choice;
    if (!choice)
    {
        //delete msg
        if (msgctl(msgid,IPC_RMID,NULL) == -1)
        {
            err_exit("msgctl IPC_RMID error");
        }
        else
        {
            cout << "msgid = " << msgid << ", IPC_RMID OK!" << endl;
        }
    }
}

消息的发送和接收

msgsnd函数

功能:把一条消息添加到消息队列中

原型

int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

参数

    msgid: 由msgget函数返回的消息队列标识码

    msgp:是一个指针,指针指向准备发送的消息,

    msgsz:是msgp指向的消息长度,这个长度不含保存消息类型的那个long int长整型

    msgflg:控制着当前消息队列满到达系统上限时将要发生的事情

 

返回值:

    成功返回0;失败返回-1

 

    msgflg=IPC_NOWAIT表示队列满不等待,返回EAGAIN错误。

 

    消息结构在两方面受到制约。首先,它必须小于系统规定的上限值;其次,它必须以一个long int长整数开始,接收者函数将利用这个长整数确定消息的类型

 

消息结构参考形式如下:

struct msgbuf
{
    long mtype;       /* message type, must be > 0 */
    char mtext[1];    /* message data */
};

//实践
/**发送结构*/
struct msgBuf
{
    long mtype;       /* message type, must be > 0 */
    char mtext[1024];    /* message data */
};

int main()
{
    int msgid = msgget(0x1234,0666|IPC_CREAT);
    if (msgid == -1)
    {
        err_exit("msgget error");
    }

    //初始化消息结构
    struct msgBuf myBuffer;
    myBuffer.mtype = 1;
    strcpy(myBuffer.mtext,"Hello XiaoFang!");

    //向消息队列发送消息
    if (msgsnd(msgid,&myBuffer,strlen(myBuffer.mtext),IPC_NOWAIT) == -1)
    {
        err_exit("msgsnd error");
    }

    return 0;
}

msgrcv函数

功能:是从一个消息队列接收消息

原型

ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
 

参数

    msgid: 由msgget函数返回的消息队列标识码

    msgp:是一个指针,指针指向准备接收的消息,

    msgsz:是msgp指向的消息长度,这个长度不含保存消息类型的那个long int长整型

    msgtype:它可以实现接收优先级的简单形式

    msgflg:控制着队列中没有相应类型的消息可供接收时将要发生的事

 

返回值:

    成功->返回实际放到接收缓冲区里去的字节数;失败->返回-1

 

msgtyp

msgtyp=0

返回队列第一条信息

msgtyp>0

返回队列第一条类型等于msgtype的消息

msgtyp<0

返回队列第一条类型小于等于msgtype绝对值的消息,并且是满足条件的消息类型最小的消息

 

 

msgflg

msgflg=IPC_NOWAIT

队列没有可读消息不等待,返回ENOMSG错误。

msgflg=MSG_NOERROR

消息大小超过msgsz时被截断 

msgtyp>0且msgflg=MSG_EXCEPT

接收类型不等于msgtype的第一条消息


//实践:消息发送
int main()
{
    int msgid = msgget(0x1234,0666|IPC_CREAT);
    if (msgid == -1)
    {
        err_exit("msgget error");
    }

    struct msgBuf myBuffer;
    for (int i = 0; i < 128; ++i)
    {
        myBuffer.mtype = i+1;
        sprintf(myBuffer.mtext,"Hello, My number is %d",i+1);
        if (msgsnd(msgid,&myBuffer,strlen(myBuffer.mtext),IPC_NOWAIT) == -1)
        {
            err_exit("msgsnd error");
        }
    }

    return 0;
}

//实践:消息接收:从队首不断的取数据
int main()
{
    int msgid = msgget(0x1234,0666);
    if (msgid == -1)
    {
        err_exit("msgget error");
    }

    //从队首不断的取数据,连续取10个
    struct msgBuf myBuffer;
    for (int i = 0; i < 10; ++i)
    {
        int recvBytes = 0;
        if ((recvBytes = msgrcv(msgid,&myBuffer,sizeof(myBuffer.mtext),0,IPC_NOWAIT)) == -1)
        {
            err_exit("msgrcv error");
        }
        else
        {
            cout << "receive recvBytes = " << recvBytes << endl;
            cout << "myBuffer.mtype = " << myBuffer.mtype << endl;
            cout << "\t" << myBuffer.mtext << endl;
        }
    }
    cout << "strlen(myBuffer.mtext) = " << strlen(myBuffer.mtext) << endl;

    return 0;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。