消息队列总结及其实现代码

来源:互联网 发布:马兰币数据 编辑:程序博客网 时间:2024/05/18 02:58

1、消息队列解释一个信息的链表。可以把消息看作一个记录,具有特定的格式,进程可以向中按照一定的规则添加新消息,另一些进程则可以从消息队列中读走消息

2、对消息队列的操作:

①打开或者创建消息队列

②进行读写操作

③获取或者设置消息队列属性

3、创建打开消息队列函数(msgget函数)

函数的作用:创建消息队列

函数的原型:int msgget(key_t key,int msgflg);

函数的参数:key:键值:IPC_PRIVATE,设定一个数字(可能会与其他进程重复),也可以由ftok获得

返回值:成功:消息队列ID;出错:-1

头文件:#include<sys/types.h>

        #include<sys.ipc.h>

        #include<sys/shm.h>

 

4、写数据到消息队列函数(msgsnd函数)

函数的作用:写数据到消息队列

函数的原型:int msgsnd(int msgid,void * msggp,size_t msgsize,int msgflg);

函数的参数:msggp:消息结构————》struct  msgbuf

                                                            {

                                                                 long  mtype

                                                                 char  mtex[i];

                                                             }

            msgsize:正文 消息的字节数

            msgflgIPC_NOWAIT写不进去直接返回

                    0:一直等待到能写进去消息位置

返回值:成功:0;出错:-1

 

5、从队列读数据函数(msgrcv函数)

函数的作用:从消息队列中读取数据

函数的原型:int msgrcv(int msgid,void * msgp,size_t msgtyp,int msgflg)

函数的参数:msgtyp0:接收消息队列第一个消息

                    >0:接收消息队列第一个msgtyp消息

                    <0:接收消息队列第一个类型值

 

6、消息队列控制函数(msgctl函数)

函数的作用:控制消息队列,可以删除消息队列

函数的原型:int msgctl(int msgid,int cmd,struct msgid_ds * buf);

函数的参数:msgid:消息队列ID

            IPC_STAT:读取msgid_ds,并存储在buf指定位置

            IPC_SET:设置权限,设置msgid_ds中的ipc_perm值,这个值取自buf

            IPC_RMID:从系统内核中删除消息队列

函数返回值:成功:0;出错:-1

 

函数实现代码:

读:#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <errno.h>

#include <unistd.h>

 

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

 

struct my_msg_st

{

    long int my_msg_type;

char some_text[BUFSIZ];

};

 

int main(void)

{

    int running=1;

int msgid;

struct my_msg_st some_data;

long int msg_to_receive=0;

 

/*创建消息队列*/

msgid=msgget((key_t)1234,0666 | IPC_CREAT);

if(msgid==-1)

{

    fprintf(stderr,"msgget failed with error: %d\n",errno);

exit(EXIT_FAILURE);

}

/*循环从消息队列中接收消息*/

while(running)

{

/*读取消息*/

    if(msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_receive,0)==-1)

{

    fprintf(stderr,"msgrcv failed with error: %d\n",errno);

exit(EXIT_FAILURE);

}

 

printf("You wrote: %s",some_data.some_text);

 

/*接收到的消息为“end”时结束循环*/

if(strncmp(some_data.some_text,"end",3)==0)

{

    running=0;

}

}

 

/*从系统内核中移走消息队列*/

if(msgctl(msgid,IPC_RMID,0)==-1)

{

    fprintf(stderr,"msgctl(IPC_RMID) failed\n");

exit(EXIT_FAILURE);

}

exit(EXIT_SUCCESS);

}

 

写:

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <errno.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

#define MAX_TEXT 512

 

struct my_msg_st

{

    long int my_msg_type;

char some_text[MAX_TEXT];

};

 

int main(void)

{

    int running=1;

struct my_msg_st some_data;

int msgid;

char buffer[BUFSIZ];

 

/*创建消息队列*/

msgid=msgget((key_t)1234,0666 | IPC_CREAT);

if(msgid==-1)

{

    fprintf(stderr,"msgget failed with error:%d\n",errno);

exit(EXIT_FAILURE);

    }

 

/*循环向消息队列中添加消息*/

while(running)

{

    printf("Enter some text:");

fgets(buffer,BUFSIZ,stdin);

some_data.my_msg_type=1;

strcpy(some_data.some_text,buffer);

 

/*添加消息*/

if(msgsnd(msgid,(void *)&some_data,MAX_TEXT,0)==-1)

{

    fprintf(stderr,"msgsed failed\n");

exit(EXIT_FAILURE);

}

 

/*用户输入的为“end”时结束循环*/

if(strncmp(buffer,"end",3)==0)

{

    running=0;

}

}

exit(EXIT_SUCCESS);

}

0 0