消息队列就是一个消息的链表。可以把消息看作一个记录,具有特定的格式以及特定的优先级。对消息队列有写权限的进程可以向其中按照一定的规则添加新消息;对消息队列有读权限的进程则可以从消息队列中读走消息。
消息队列和共享内存类似消息队列它允许一个或多个进程向它写消息,一个或多个进程向它写读消息。消息队列存在于系统内核中,消息的数量受系统限制。我们来看一下有关消息队列的函数。
Linux的消息队列(queue)实质上是一个链表, 它有消息队列标识符(queue ID). msgget创建一个新队列或打开一个存在的队列;msgsnd向队列末端添加一条新消息; msgrcv从队列中取消息, 取消息是不一定遵循先进先出的, 也可以按消息的类型字段取消息。
System V机制
标识符(des)和键(key):
消息队列, 信号量和共享存储段, 都属于内核中的IPC结构, 它们都用标识符来描述. 这个标识符是一个非负整数, 与文件描述符不同的是, 创建时并不会重复利用通过删除回收的整数, 而是每次+1, 直到整数最大值回转到0.
标识符是IPC对象的内部名, 而它的外部名则是key(键), 它的基本类型是key_t, 在头文件中定义为长整型. 键由内核变换成标识符.
消息队列状态msqid_ds:
每个消息队列都有一个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 */ };
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
msgflg:IPC_CREAT值,若没有该队列,则创建一个并返回新标识符;若已存在,则返回原标识符。IPC_EXCL值,若没有该队列,则返回-1;若已存在,则返回0。
例1:
#include<stdio.h> #include<stdlib.h> #include<sys/msg.h> #define MYMSG_KEY 6666 int main(int argc ,char *argv[]) { int msgid; msgid=msgget(ftok(".",100),IPC_CREAT | 0644); //msgid=msg(MYMSG_KEY,IPC_CREAT | 0644); printf("msgid=%d\n",msgid); if(msgid==-1) { perror("msgget error :"); exit(EXIT_FAILURE); } return 0; }
<span style="font-size:14px;">struct msgstru{ long mtype; //大于0 char mtext[512]; };</span>msgsz:消息的大小。
#include<stdio.h> #include<stdlib.h> #include<sys/msg.h> #include<string.h> #define MYMSG_KEY 6666 struct mymesg { long mtype; /* positive message type */ char mtext[512]; /* message data, of length nbytes */ }; int main(int argc ,char *argv[]) { int msgid,msgsend_ret; char buf[25]; struct mymesg msg_send; msgid=msgget(ftok(".",100),IPC_CREAT | 0644); //msgid=msg(MYMSG_KEY,IPC_CREAT | 0644); printf("msgid=%d\n",msgid); if(msgid==-1) { perror("msgget error :"); exit(EXIT_FAILURE); } // write messages to msg queue msg_send.mtype=1; printf("enter a message:\n"); gets(buf); strcpy(msg_send.mtext,buf); msgsend_ret=msgsnd(msgid,&msg_send,strlen(msg_send.mtext)+1,0); if(msgsend_ret==-1) { perror("msgget error: "); exit(EXIT_FAILURE); } return 0; }解析:运行一次,写入消息后,消息数为1,再运行一次后,我们可以看到消息的数量为2。
#include<stdio.h> #include<stdlib.h> #include<sys/msg.h> #include<string.h> #define MYMSG_KEY 6666 struct mymesg { long mtype; /* positive message type */ char mtext[512]; /* message data, of length nbytes */ }; int main(int argc ,char *argv[]) { int msgid,msgsend_ret; char buf[25]; struct mymesg msg_send; msgid=msgget(ftok(".",100),IPC_CREAT | 0644); //msgid=msg(MYMSG_KEY,IPC_CREAT | 0644); printf("msgid=%d\n",msgid); if(msgid==-1) { perror("msgget error :"); exit(EXIT_FAILURE); } // write messages to msg queue /* 0 msg_send.mtype=1; printf("enter a message:\n"); gets(buf); strcpy(msg_send.mtext,buf); msgsend_ret=msgsnd(msgid,&msg_send,strlen(msg_send.mtext)+1,0); if(msgsend_ret==-1) { perror("msgget error: "); exit(EXIT_FAILURE); } */ //read mseeags from msg queue int msgrcv_ret; struct mymesg mymsgrece; msgrcv_ret=msgrcv(msgid,&mymsgrece, sizeof(struct mymesg)-sizeof(long),1,0); //读取消息的类型为1,长度为sizeof(struct mymesg)-sizeof(long)把读到 //的消息放到mymsgrece这这结构体中。 if(msgrcv_ret==-1) { perror("msgrcv error:"); exit(EXIT_FAILURE); } printf("received msg from queue : %s\n",mymsgrece.mtext); return 0; }解析:分别运行2次后,去除消息队列中的数据。
//send.c #include<stdio.h> #include<stdlib.h> #include<sys/msg.h> #include<string.h> #define MYMSG_KEY 6666 struct mymesg { long mtype; /* positive message type */ char mtext[512]; /* message data, of length nbytes */ }; int main() { int msgid,msgsend; struct mymesg mymsgsend; msgid=msgget(MYMSG_KEY,IPC_CREAT|0644); printf("msgid=%d\n",msgid); if(msgid==-1) { perror("msgget error: "); exit(EXIT_FAILURE); } mymsgsend.mtype=1; while(1) { printf("enter a type and a msg use to send :\n"); scanf("%d%s",&mymsgsend.mtype,mymsgsend.mtext); msgsend=msgsnd(msgid,&mymsgsend,strlen(mymsgsend.mtext)+1,0); if(msgsend==-1) { perror("msgget error: "); exit(EXIT_FAILURE); } if(strcmp(mymsgsend.mtext,"exit")==0) break; } return 0; }
//recv.c #include<stdlib.h> #include<sys/msg.h> #define MYMSG_KEY 6666 struct mymesg { long mtype; /* positive message type */ char mtext[512]; /* message data, of length nbytes */ }; int main() { int msgid,msgrcv_ret,msgctl_ret; struct mymesg mymsgrece; msgid=msgget(MYMSG_KEY ,IPC_CREAT|0644); printf("msgid=%d\n",msgid); if(msgid==-1) { perror("msgget error:"); exit(EXIT_FAILURE); } while(1) { scanf("%d",&mymsgrece.mtype); msgrcv_ret=msgrcv(msgid,&mymsgrece,512,mymsgrece.mtype,0); if(msgrcv_ret==-1) { perror("msgrcv error:"); exit(EXIT_FAILURE); } if(strcmp(mymsgrece.mtext,"exit")==0) break; printf("received msg : %s\n",mymsgrece.mtext); } msgctl_ret=msgctl(msgid,IPC_RMID,0); if(msgctl_ret==-1) { perror("msgrcv error:"); exit(EXIT_FAILURE); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/tfygg/article/details/47278911