Linux消息队列 .

来源:互联网 发布:node v8.1.0 x64.msi 编辑:程序博客网 时间:2024/06/13 21:55

 一、概念

  消息队列就是一个消息的链表。可以把消息看作一个记录,具有特定的格式以及特定的优先级。对消息队列有写权限的进程可以向中按照一定的规则添加新消息;有读权限的进程则可以读走消息。读走就没有了。消息队列是随内核持续的。 只有内核重启或人工删除时,该消息才会被删除。在系统范围内,消息队列与键值唯一对应。

二、步骤及思路

1、取得键值

2、打开、创建消息队列

3、发送消息

4、接收消息

下面具体看看:

1、取得键值

      key_t ftok(char *pathname, char proj)

 

   头文件为<sys/ipc.h>。返回文件名对应的键值,失败返回 -1。proj是项目名,随便写,不为0就行。

   fname就是你指定的文件名(已经存在的文件名)。需要有-t 的权限,或用root权限执行,通常设为/tmp或设为" . "。这里我感觉不用这个函数也行,因为key值可以自己指定,例如: #define KEY_MSG 0x101

2、打开、创建消息队列

     int msgget(key_t key, int msgflg)

 

  头文件为<sys/msg.h>。key由ftok获得。

  msgflg有:

IPC_CREAT 创建新的消息队列,应配有文件权限0666。

IPC_EXCL  与IPC_CREAT一同使用,表示如果要创建的消息队列已经存在,则返回错误。

IPC_NOWAIT 读写消息不阻塞。

 

  当没有与key相对应的消息队列并且msgflg中包含了IPC_CREAT标志 或 key的参数为IPC_PRIVATE 时,创建一个新的消息队列。

3、发送消息

         int msgsnd(int msqid, struct msgbuf *msgp, int msgsz, int msgflg)

    向消息队列发送一条消息。msqid为消息队列的id,msgp为存放消息的结构体。msgsz是消息的长度,和数组的大小不一样哦。msgflg为消息标志,通常为0,也可以为IPC_NOWAIT。出错返回 -1。

 

消息格式

      struct msgbuf  {

                   long mtype;

                   char mtext[100];

      };

4、接收消息

        int msgrcv(int msqid, struct msgbuf *msgp, int msgsz, long msgtyp, int msgflg)

    从msqid代表的消息队列中读取一个msgtyp类型的消息,并把消息存储在msgp指定的msgbuf结构中。读取后队列中的消息将会删除。size为结构体中数据的大小,不要计算msgtyp。出错返回 -1。

三、举例

创建一消息队列,子进程发数据,父进程收数据。实现向队列中存放数据与读取数据。

 

[cpp] view plaincopyprint?
  1. #include<stdio.h>   
  2. #include<sys/msg.h>   
  3. #include<fcntl.h>   
  4. #include<stdlib.h>   
  5. #include<string.h>   
  6. #define max 100   
  7.   
  8. struct haha{  
  9.         long mtype;  
  10.         char data[max];  
  11. };  
  12.   
  13. int main(int argc,char *argv[]){  
  14.         int pid;  
  15.         if(argc!=2){  
  16.                 printf("Usage: msg [Message]\n");  
  17.                 return -1;  
  18.         }  
  19.         key_t key;  
  20.         if((key=ftok("/tmp",'g'))<0){           //这里文件夹必须存在,有t属性并且上级目录也要有t属性  
  21.                 printf("Getting key error! \n");  
  22.                 return -1;  
  23.         }  
  24.         int mgsid;  
  25.         if((mgsid=msgget(key,IPC_CREAT|0666))==-1){     //key值随便写一个数也能用  
  26.                         printf("mgs queue create error\n");  
  27.                         return -1;  
  28.         }  
  29.         pid=fork();  
  30.         if(pid <0){  
  31.                 printf("fork create error!\n");  
  32.                 _exit(1);  
  33.         }  
  34.         if(pid == 0){  
  35.                 printf("welcome in child process\n Sending the message......\n");  
  36.                 sleep(1);  
  37.                 struct haha hehe;  
  38.                 hehe.mtype=getppid();  
  39.                 strcpy(hehe.data,argv[1]);  
  40.                 if(msgsnd(mgsid,&hehe,sizeof(hehe.data),0)<0){  //此处注意长度  
  41.                         printf("Sending error!!!\n");  
  42.                         _exit(1);  
  43.                 }else {  
  44.                         printf("Sending complete!\n");  
  45.                         _exit(0);  
  46.                 }  
  47.         }else{  
  48.                 wait(NULL);  
  49.                 printf("welcome in parents process\n Receiving the message......\n");  
  50.                 sleep(1);  
  51.                 struct haha gaga;  
  52.                 if(msgrcv(mgsid,&gaga,max,getpid(),0)<0){  
  53.                         printf("Receiving error!!!\n");  
  54.                         _exit(1);  
  55.                 }else {  
  56.                         printf("Receiving complete!\n");  
  57.                         printf("The message is %s \n",gaga.data);  
  58.                 }  
  59.         }  
  60.         return 0;  
  61. }  


原创粉丝点击