Linux消息隊列 .

 一、概念

  消息隊列就是一個消息的鏈表。可以把消息看作一個記錄,具有特定的格式以及特定的優先級。對消息隊列有寫權限的進程可以向中按照一定的規則添加新消息;有讀權限的進程則可以讀走消息。讀走就沒有了。消息隊列是隨內核持續的。 只有內核重啓或人工刪除時,該消息纔會被刪除。在系統範圍內,消息隊列與鍵值唯一對應。

二、步驟及思路

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。

三、舉例

創建一消息隊列,子進程發數據,父進程收數據。實現向隊列中存放數據與讀取數據。

 

  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. }  
#include<stdio.h>
#include<sys/msg.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
#define max 100

struct haha{
        long mtype;
        char data[max];
};

int main(int argc,char *argv[]){
        int pid;
        if(argc!=2){
                printf("Usage: msg [Message]\n");
                return -1;
        }
        key_t key;
        if((key=ftok("/tmp",'g'))<0){           //這裏文件夾必須存在,有t屬性並且上級目錄也要有t屬性
                printf("Getting key error! \n");
                return -1;
        }
        int mgsid;
        if((mgsid=msgget(key,IPC_CREAT|0666))==-1){     //key值隨便寫一個數也能用
                        printf("mgs queue create error\n");
                        return -1;
        }
        pid=fork();
        if(pid <0){
                printf("fork create error!\n");
                _exit(1);
        }
        if(pid == 0){
                printf("welcome in child process\n Sending the message......\n");
                sleep(1);
                struct haha hehe;
                hehe.mtype=getppid();
                strcpy(hehe.data,argv[1]);
                if(msgsnd(mgsid,&hehe,sizeof(hehe.data),0)<0){  //此處注意長度
                        printf("Sending error!!!\n");
                        _exit(1);
                }else {
                        printf("Sending complete!\n");
                        _exit(0);
                }
        }else{
                wait(NULL);
                printf("welcome in parents process\n Receiving the message......\n");
                sleep(1);
                struct haha gaga;
                if(msgrcv(mgsid,&gaga,max,getpid(),0)<0){
                        printf("Receiving error!!!\n");
                        _exit(1);
                }else {
                        printf("Receiving complete!\n");
                        printf("The message is %s \n",gaga.data);
                }
        }
        return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章