消息队列

一 、消息队列含义 : 及消息的列表,用户可以从消息队列中添加消息和读取消息,可以随机查询。消息队列是存在于内核中的,由“队列ID”来标识。

二、应用 :消息队列的实现包括创建或打开消息队列、添加消息、读取消息和控制消息队列这 4 种操作。

  1. 创建或打开消息队列 int msgget(key_t key,int msgflg)

    参数:
    key:消息队列的键值,多个进程可以通过它访问同一个消息队列。其中有个特殊值IPC_PRIVATE,
    它用于创建当前进程的私有消息队列。
    msgflg:权限标志位,可以取值 IPC_CREAT - 如果消息队列对象不存在,则创建之,否则则进行打开操作;IPC_EXCL - 和IPC_CREAT 一起使用(用”|”连接),如果消息对象不存在则创建之,否则产生一个错误并返回。

    返回值:成功-消息队列ID 出错- -1
    2.添加消息 int msgsnd(int msgqid,const void* msgp,size_t msgsz,int msgflg)

    参数:
    msgqid : 消息队列ID
    msgp:指向消息结构的指针,该消息结构msgbuf通常为
    struct msgbuf
    {
    long mtype;/消息类型,该结构必须从这个域开始/
    char mtext[n];/消息正文/
    }
    msgsz:消息正文的字节数(不包括嘻嘻类型指针变量)
    msgflg:IPC_NOWAIT - 若消息无法立即发送(比如:消息队列已满),函数会立即返回; 0 - 阻塞到发送成功为止。

    返回值:成功 - 0 出错- -1
    3.读取消息 int msgrcv(int msgqid,void* msgp,size_t msgsz,int msgtyp,int msgflg)

    参数:
    msgqid:消息队列ID
    msgp:消息缓冲区,同于msgsnd()函数的msgp
    msgsz:消息正文的字节数
    msgtyp:0 —— 接收消息队列中第一个消息 大于0– 接收消息队列中第一个类型为msgtyp的消息 小于0–接收消息队列中第一个类型值不小于msgtyp绝对值且类型值又最小的消息
    msgflg:MSG_NOERROR-若返回的消息比msgsz字节多,则消息就会截短到msgsz字节,且不通知消息发送进程
    IPC_NOWAIT-若在消息队列中并没有相应类型的消息可以接收,则函数立即返回
    0- 阻塞到接收一条相应类型的消息为止

    返回值:成功-0 出错 - -1
    4.控制消息队列 int msgctl(int msgqid,int cmd,struct msqid_ds*buf)

    参数:
    msgqid:消息队列的队列ID
    cmd:IPC_STAT-读取消息队列的数据结构msqid_ds,并将其存储在buf指定的地址中 IPC_SET-设置消息队列的数据结构msqid_ds中的ipc_perm,这个值取自buf参数 IPC_RMID:从系统中内核中删除消息队列
    buf:描述消息队列的msqid_ds结构类型变量
    struct msqid_ds
    {
    struct msqid_ds {
    struct ipc_perm msg_perm;
    struct msg msg_first; / first message on queue,unused */
    struct msg msg_last; / last message in queue,unused */
    __kernel_time_t msg_stime; /* last msgsnd time */
    __kernel_time_t msg_rtime; /* last msgrcv time */
    __kernel_time_t msg_ctime; /* last change time */
    unsigned long msg_lcbytes; /* Reuse junk fields for 32 bit */
    unsigned long msg_lqbytes; /* ditto */
    unsigned short msg_cbytes; /* current number of bytes on queue */
    unsigned short msg_qnum; /* number of messages in queue */
    unsigned short msg_qbytes; /* max number of bytes on queue */
    __kernel_ipc_pid_t msg_lspid; /* pid of last msgsnd */
    __kernel_ipc_pid_t msg_lrpid; /* last receive pid */
    };
    三、使用实例
    消息队列接收msgrcv.c

#define BUFFER_SIZE 512
struct message
{
    long msg_type;
    char msg_text[BUFFER_SIZE];
};

int main()
{
    int qid;
    key_t key;
    struct message msg;

    /*根据不同的路径和关键字产生标准的 key*/
    if ((key = ftok(".", 'a')) == -1)
    {
        perror("ftok");
        exit(1);
    }

    /*创建消息队列*/
    if ((qid = msgget(key, IPC_CREAT|0666)) == -1)
    {
        perror("msgget");
        exit(1);
    }
    printf("Open queue %d\n", qid);

    do
    {
        /*读取消息队列*/
        memset(msg.msg_text, 0, BUFFER_SIZE);
        if(msgrcv(qid,(void*)&msg,BUFFER_SIZE,0,0)<0)
        {
            perror("msgrcv");
            exit(1);
        }
        printf("The message from process %d : %s", msg.msg_type, msg.msg_text);
    }while(strncmp(msg.msg_text,"quit",4));

    /*从系统内核中移走消息队列 */
    if ((msgctl(qid, IPC_RMID, NULL)) < 0)
    {
        perror("msgctl");
        exit(1);
    }
    exit(0);

}

消息队列发送 msgsnd.c

#define BUFFER_SIZE 512

sturct message
{
    long msg_type;
    char msg_text[BUFFER_SIZE];
};


int main()
{
    int qid;
    key_t key;
    struct message msg;

    /*根据不同的路径和关键字产生标准的 key*/
    if ((key = ftok(".", 'a')) == -1)
    {
        perror("ftok");
        exit(1);
    }

    /*创建消息队列*/
    if((qid=msgget(key,IPC_CREATE|0666)) == -1)
    {
        perror("msgget");
        exit(1);
    }
    printf("Open queue %d\n",qid);

    while(1)
    {
        printf("Enter some message to the queue:");
        if((fgets(msg.msg_text, BUFFER_SIZE, stdin)) == NULL)
        {
            puts("no message");
            exit(1);
        }
        msg.msg_type = getpid();

        /*添加消息到消息队列*/
        if ((msgsnd(qid, &msg, strlen(msg.msg_text), 0)) < 0)
        {
            perror("message posted");
            exit(1);
        }
        if (strncmp(msg.msg_text, "quit", 4) == 0)
        {
        break;
        }
    }
    exit(0);
}

开启量个console,分别运行./msgsnd和./msgrcv, msgsnd中发送quit,两个进程都退出。
msgsnd
这里写图片描述
msgrcv
这里写图片描述

发布了28 篇原创文章 · 获赞 6 · 访问量 3万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章