现在的位置: 首页 > 综合 > 正文

浅谈消息队列

2013年08月02日 ⁄ 综合 ⁄ 共 2634字 ⁄ 字号 评论关闭

消息队列是系统内核地址空间中的一个内部的链表。

消息可以按照顺序发送到队列中,也可以以几种不同的方式从队列中读取。

每一个消息队列用一个唯一的IPC标识符表示。

数据结构msgbuf,此数据结构可以说是消息数据的模板,虽然此用户结构需要用户自己定义。

在中,此数据结构的定义为:struct msgbuf{long mtype;char request_id;struct cilent_info;}

这里的消息类型字段和前面的一样,但数据结构的其余部分则由其他一个的两个字段所代替,而其中的一个还是另外一个结构。

这就体现了消息队列的灵活之处。msgget()用于创建一个新的消息队列,或者存取一个已经存在的消息队列。

msgsnd()系统调用用于向队列发送一条消息msgrcv()系统调用用于从消息队列中读取一条消息

这个今天下午复习了一下有关消息队列的知识,感觉就是它的识别度比较高,根据你的type来确定你要收发的数据,下面这个程序中编译程msgtool后会生成一个a.out文件

./a.out w 0 "hello"

再在另外一个终端下面输入

./a.out r 0 这样就能根据类型0来识别要写入和输出的数据

0可以换成其他的数字。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MAX_SEND_SIZE 80

struct mymsgbuf{//定义结构体
    long mtype;
    char mtext[MAX_SEND_SIZE];
};

void send_message(int qid,struct mymsgbuf *qbuf,long type,char *text);
void read_message(int qid,struct mymsgbuf *qbuf,long type);
void remove_queue(int qid);
void change_queue_mode(int qid,char *mode);
void usage(void);

int main(int argc,char **argv)
{
    key_t key;
    int msgqueue_id;
    struct mymsgbuf qbuf;
    if(argc==1)
        usage();
    //系统建立IPC通讯 (消息队列、信号量和共享内存) 时必须指定一个ID值。通常情况下,该id值通过ftok函数得到。
    key=ftok(".",'m');

    if((msgqueue_id=msgget(key,IPC_CREAT|0666))==-1)
    {
        perror("msgget");
        exit(1);
    }
    printf("message queue id = [%d] .\n",msgqueue_id);
    
    switch(tolower(argv[1][0]))
    {
        case 's':
            if(argc<4)
            {
                usage();
                break;    
            }    
            send_message(msgqueue_id,&qbuf,atol(argv[2]),argv[3]);//把字符串转换成长整型数
            break;
        case 'r':
            if(argc<3)
            {
                usage();
                break;
            }
            read_message(msgqueue_id,&qbuf,atol(argv[2]));
            break;
        case 'd':
                remove_queue(msgqueue_id);
            break;
        case 'm':
            if(argc<3)
            {
                usage();
                break;
            }
        default:usage();
            
    }
    return 0;
}

void send_message(int qid,struct mymsgbuf *qbuf,long type,char *text)
{
    //给队列发送消息

    printf("sending a message....\n");
    qbuf->mtype=type;//赋数值类型
    strcpy(qbuf->mtext,text);
    
    if((msgsnd(qid,qbuf,strlen(qbuf->mtext)+1,0))==-1)
    {
        perror("msgsnd");
        exit(1);
    }
}

void read_message(int qid,struct mymsgbuf *qbuf,long type)
{
    printf("Reading a message...\n");
    qbuf->mtype=type;
    msgrcv(qid,qbuf,MAX_SEND_SIZE,type,0);
    printf("Type: %1d Text:%s \n",qbuf->mtype,qbuf->mtext);
}
void remove_queue(int qid)
{
    msgctl(qid,IPC_RMID,0);
}
void change_queue_mode(int qid,char *mode)
{
}
void usage(void)
{
    fprintf(stderr,"msgtool -A utility for thinkering with msg queues \n");
    fprintf(stderr,"USAGE:msgtool (s) end <type>> <>type> <>");
    fprintf(stderr,"(r)");
    fprintf(stderr,"(d)");
    fprintf(stderr,"(m)");
    exit(1);
}

抱歉!评论已关闭.