Linux訊息佇列實踐(1),linux訊息佇列實踐

來源:互聯網
上載者:User

Linux訊息佇列實踐(1),linux訊息佇列實踐
訊息佇列基本概念

    訊息佇列提供了一個從一個進程向另外一個進程發送一塊資料的方法(僅局限與本機)

    每個資料區塊都被認為是有一個類型,接收者進程接收的資料區塊可以有不同的類型值

    訊息佇列也有管道一樣的不足,就是每個訊息的最大長度是有上限的(MSGMAX),每個訊息佇列的總的位元組數是有上限的(MSGMNB),系統上訊息佇列的總數也有一個上限(MSGMNI)

 

管道 vs. 訊息佇列:

管道:流管道         訊息:有邊界

  先進先出          可以後進入、先出來

 

訊息佇列大小三大限制

   cat /proc/sys/kernel/msgmax #最大訊息長度限制

   cat /proc/sys/kernel/msgmnb #訊息佇列總的位元組數

  cat /proc/sys/kernel/msgmni #訊息條目數

 

 

IPC對象資料結構

   核心為每個IPC對象維護一個資料結構

[cpp] view plaincopy
  1. struct ipc_perm  
  2. {  
  3.     key_t          __key;       /* Key supplied to msgget(2) */  
  4.     uid_t          uid;         /* Effective UID of owner */  
  5.     gid_t          gid;         /* Effective GID of owner */  
  6.     uid_t          cuid;        /* Effective UID of creator */  
  7.     gid_t          cgid;        /* Effective GID of creator */  
  8.     unsigned short mode;        /* Permissions */  
  9.     unsigned short __seq;       /* Sequence number */  
  10. };  

 

訊息佇列特有的結構

[cpp] view plaincopy
  1. struct msqid_ds  
  2. {  
  3.     struct ipc_perm msg_perm;     /* Ownership and permissions */  
  4.     time_t          msg_stime;    /* Time of last msgsnd(2) */  
  5.     time_t          msg_rtime;    /* Time of last msgrcv(2) */  
  6.     time_t          msg_ctime;    /* Time of last change */  
  7.     unsigned long   __msg_cbytes; /* Current number of bytes in 
  8.                                                 queue (nonstandard) */  
  9.     msgqnum_t       msg_qnum;     /* Current number of messages 
  10.                                                 in queue */  
  11.     msglen_t        msg_qbytes;   /* Maximum number of bytes 
  12.                                                 allowed in queue */  
  13.     pid_t           msg_lspid;    /* PID of last msgsnd(2) */  
  14.     pid_t           msg_lrpid;    /* PID of last msgrcv(2) */  
  15. };  

 

訊息佇列在核心中的表示


訊息佇列函數樣本

msgget函數 

功能:用來建立和訪問一個訊息佇列

原型:

[cpp] view plaincopy
  1. int msgget(key_t key, int msgflg);  

參數:

    key: 某個訊息佇列的名字

    msgflg:由九個許可權標誌構成,如0644,它們的用法和建立檔案時使用的mode模式標誌是一樣的(但是訊息佇列沒有x(執行)許可權)

 

傳回值:

   成功返回訊息佇列編號,即該訊息佇列的標識碼;失敗返回-1

 

編程實踐

[cpp] view plaincopy
  1. //實踐1:  IPC_PRIVATE:宏,值為0  
  2. int main()  
  3. {  
  4.   //IPC_PRIVATE每次建立的訊息佇列的描述符都是不同的!  
  5.   //因此,計時MessageID(key)傳送給其他進程(除非有關聯的進程),  
  6.   //其他進程也無法使用該訊息佇列(血緣fork除外)  
  7.     int msgid = msgget(IPC_PRIVATE,0666);  
  8.     if (msgid < 0)  
  9.     {  
  10.         //如果訊息佇列不存在,則開啟錯誤,errno=ENOENT  
  11.         if (errno == ENOENT)  
  12.         {  
  13.             cout << "ENOENT" << endl;  
  14.         }  
  15.         err_exit("mesget error");  
  16.     }  
  17.     else  
  18.     {  
  19.         cout << "msgid = " << msgid << endl;  
  20.     }  
  21.   
  22.     return 0;  
  23. }  
  24. /** 
  25.   IPC_PRIVATE建立的訊息佇列,只能用在與當前進程有關係的進程中使用! 
  26. */  


[cpp] view plaincopy
  1. //實踐2:  IPC_CREAT  
  2. int main()  
  3. {  
  4.     //指定IPC_CREAT,則一定會建立訊息佇列  
  5.     int msgid = msgget(0x1235,0666|IPC_CREAT);  
  6.     if (msgid < 0)  
  7.     {  
  8.         //如果訊息佇列不存在,則開啟錯誤,errno=ENOENT  
  9.         if (errno == ENOENT)  
  10.         {  
  11.             cout << "ENOENT" << endl;  
  12.         }  
  13.         err_exit("mesget error");  
  14.     }  
  15.     else  
  16.     {  
  17.         cout << "msgid = " << msgid << endl;  
  18.     }  
  19.   
  20.     return 0;  
  21. }  


[cpp] view plaincopy
  1. //實踐3:  IPC_CREAT|IPC_EXCL  
  2. int main()  
  3. {  
  4.     //指定IPC_EXCL, 如果已經存在,則報告檔案已經存在(錯誤)  
  5.     int msgid = msgget(0x1235,0666|IPC_CREAT|IPC_EXCL);  
  6.     if (msgid < 0)  
  7.     {  
  8.         err_exit("mesget error");  
  9.     }  
  10.     else  
  11.     {  
  12.         cout << "msgid = " << msgid << endl;  
  13.     }  
  14.   
  15.     return 0;  
  16. }  




[cpp] view plaincopy
  1. //實踐4:低許可權建立,高許可權開啟  
  2. int main()  
  3. {  
  4.     //低許可權建立  
  5.     int msgid = msgget(0x255,0444 | IPC_CREAT);  
  6.     if (msgid < 0)  
  7.     {  
  8.         err_exit("mesget error");  
  9.     }  
  10.     else  
  11.     {  
  12.         cout << "Create Mes OK, msgid = " << msgid << endl;  
  13.     }  
  14.   
  15.     //高許可權開啟  
  16.     msgid = msgget(0x255,0644 | IPC_CREAT);  
  17.     if (msgid < 0)  
  18.     {  
  19.         err_exit("mesget error");  
  20.     }  
  21.     else  
  22.     {  
  23.         cout << "Create Mes OK, msgid = " << msgid << endl;  
  24.     }  
  25.   
  26.     return 0;  
  27. }  


msgget函數參數關係圖

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.