歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> Unix資訊 >> 知識講解Unix 消息隊列

知識講解Unix 消息隊列

日期:2017/3/6 11:40:58   编辑:Unix資訊

由於經常學習Unix操作系統的一些知識,今天,我們來學習一下Unix 消息隊列的知識,在傳統的單任務操作系統中 , 程序設計的對象一經運行 , 程序就將獨占整個主機資源, 程序實體之間的不同模塊完全是通過全局變量、函數調用時的參數返回值來進行通信的。

Unix 操作系統是一個分時的多任務操作系統 , 程序運行後都將成為一個獨立的實體——進程 , 進程間的通信不僅包括其內部通信 , 還包括進程間的通信。UNIX System V 中提供了一系列的進程通信機構 , 即 IPC 機構 ,Unix 消息隊列就是其中之一。

在 Unix 系統中 , 所有的消息都放在系統內核當中 , 並且它們都有一個相應的Unix 消息隊列標識符。進程可讀寫任意隊列中特定的消息 , 其次序是消息到達的次序 , 核心負責維護這一適當的次序 , 而且在同一Unix 消息隊列中 , 不同的進程可分別讀出各自需要的消息 , 在其它進程向Unix 消息隊列寫入消息之前 , 進程可一直讀取消息而不必等待消息到達隊列。

每一個位於Unix 消息隊列中的消息都包括如下內容 :
1. 長整數類型 : 定義消息類型
2. 消息的數據長度 : 定義數據長度
3. 數據 : 具體內容

系統內核為Unix 消息隊列維持如下數據結構信息 , 其定義包含在 <msg.h> 頭文件中 :

  1. struct msqid_ds{
  2. struct ipc_perm msg_perms; /*operation permission struct*/
  3. struct msg *msg_first;
  4. /*ptr to first message on q*/
  5. struct msg *msg_last;
  6. /*ptr to last message on q*/
  7. ushort
  8. msg_cbytes;
  9. /*current num bytes on q*/
  10. ushort
  11. msg_qnum;
  12. /*no.message on q*/
  13. ushort
  14. msg_qbuyes;
  15. /*max no.bytes for q*/
  16. ushort
  17. msg_lspid;
  18. /*pid of last megsnd*/
  19. ushort
  20. msg_lrpid;
  21. /*pid of last msgrcv*/
  22. time_t
  23. msg_stime;
  24. /*last msgsnd time*/
  25. time_t
  26. msg_rtime;
  27. /*last msgrcv time*/
  28. time_t
  29. msg_ctime;
  30. /*last change time*/

類型 ushort 和 time_t 與系統實現有關 , 它們包含在頭文件 <types.h> 中定義 ;ipc_perm 結構包含了對應Unix 消息隊列的主人和存取權限 ; 結構 msg 被內核用來把某一隊列上的消息鏈接為隊列。

這次,關於Unix 消息隊列的知識,我們就講解到這裡了。希望大家能夠好好的學習這部分知識。

Copyright © Linux教程網 All Rights Reserved