msgctl() - Unix,Linux系統呼叫 - 技術教學
Tutorials Point


  Unix入門
  Unix Shell程式設計
  高階Unix
  Unix有用參考資料
  Unix有用資源
  精選閱讀

版權所有 © 2014 tutorialspoint



  首頁     參考資料     討論論壇     關於TP  

msgctl() - Unix,Linux系統呼叫


previous next AddThis Social Bookmark Button

廣告

名稱

msgctl - 訊息控制操作

概要

#include <sys/types.h> 
#include <sys/ipc.h> 
#include <sys/msg.h> 

int msgctl(int msqid, int cmd, struct msqid_ds *buf);

描述

msgctl() 對識別符號為 msqid 的訊息佇列執行由 cmd 指定的控制操作。

msqid_ds 資料結構在 <sys/msg.h> 中定義如下:


struct msqid_ds { struct ipc_perm msg_perm; /* Ownership and permissions time_t msg_stime; /* Time of last msgsnd() */ time_t msg_rtime; /* Time of last msgrcv() */ time_t msg_ctime; /* Time of last change */ unsigned long __msg_cbytes; /* Current number of bytes in queue (non-standard) */ msgqnum_t msg_qnum; /* Current number of messages in queue */ msglen_t msg_qbytes; /* Maximum number of bytes allowed in queue */ pid_t msg_lspid; /* PID of last msgsnd() */ pid_t msg_lrpid; /* PID of last msgrcv() */ };

ipc_perm 結構在 <sys/ipc.h> 中定義如下(突出顯示的欄位可以使用 IPC_SET 設定):


struct ipc_perm {
    key_t key;            /* Key supplied to msgget() */
    uid_t uid;            /* Effective UID of owner */
    gid_t gid;            /* Effective GID of owner */
    uid_t cuid;           /* Effective UID of creator */
    gid_t cgid;           /* Effective GID of creator */
    unsigned short mode;  /* Permissions */
    unsigned short seq;   /* Sequence number */
};

cmd 的有效值:

標籤描述
IPC_STAT
 將與 msqid 關聯的核心資料結構中的資訊複製到 buf 指向的 msqid_ds 結構中。呼叫者必須對訊息佇列具有讀取許可權。
IPC_SET
 buf 指向的 msqid_ds 結構的某些成員的值寫入與該訊息佇列關聯的核心資料結構,並更新其 msg_ctime 成員。將更新結構的以下成員:msg_qbytesmsg_perm.uidmsg_perm.gid 和(msg_perm.mode 的最低有效 9 位)。呼叫程序的有效 UID 必須與訊息佇列的所有者 (msg_perm.uid) 或建立者 (msg_perm.cuid) 匹配,或者呼叫者必須具有特權。需要適當的特權(Linux:CAP_IPC_RESOURCE 功能)才能將 msg_qbytes 值提高到系統引數 MSGMNB 以上。
IPC_RMID
 立即刪除訊息佇列,喚醒所有等待讀取和寫入程序(返回錯誤並設定 errnoEIDRM)。呼叫程序必須具有適當的特權,或者其有效使用者 ID 必須是訊息佇列的建立者或所有者。
IPC_INFO(Linux 特定)
 將系統範圍的訊息佇列限制和引數資訊返回到 buf 指向的結構中。此結構的型別為 msginfo(因此需要強制轉換),如果定義了 _GNU_SOURCE 功能測試宏,則在 <sys/msg.h> 中定義。

struct msginfo { int msgpool; /* Size in bytes of buffer pool used to hold message data; unused */ int msgmap; /* Max. # of entries in message map; unused */ int msgmax; /* Max. # of bytes that can be written in a single message */ int msgmnb; /* Max. # of bytes that can be written to queue; used to initialize msg_qbytes during queue creation (msgget()) */ int msgmni; /* Max. # of message queues */ int msgssz; /* Message segment size; unused */ int msgtql; /* Max. # of messages on all queues in system; unused */ unsigned short int msgseg; /* Max. # of segments; unused */ };

msgmnimsgmaxmsgmnb 設定可以透過同名的 /proc 檔案更改;有關詳細資訊,請參見 proc(5)。
MSG_INFO(Linux 特定)
 返回一個 msginfo 結構,其中包含與 IPC_INFO 相同的資訊,但以下欄位返回有關訊息佇列消耗的系統資源的資訊:msgpool 欄位返回系統上當前存在的消 息佇列數;msgmap 欄位返回系統上所有佇列中訊息的總數;msgtql 欄位返回系統上所有佇列中所有訊息的總位元組數。
MSG_STAT(Linux 特定)
 返回一個 msqid_ds 結構,與 IPC_STAT 相同。但是,msqid 引數不是佇列識別符號,而是核心內部陣列中的索引,該陣列維護有關係統上所有訊息佇列的資訊。

返回值

成功時,IPC_STATIPC_SETIPC_RMID 返回 0。成功的 IPC_INFOMSG_INFO 操作返回核心內部陣列中最高使用條目的索引,該陣列記錄有關所有訊息佇列的資訊。(此資訊可與重複的 MSG_STAT 操作一起使用,以獲取系統上所有佇列的資訊。)成功的 MSG_STAT 操作返回其索引在 msqid 中指定的佇列的識別符號。

出錯時,返回 -1,errno 指示錯誤。

錯誤

失敗時,errno 設定為以下之一:
標籤描述
EACCES 引數 cmd 等於 IPC_STATMSG_STAT,但呼叫程序對訊息佇列 msqid 沒有讀取許可權,並且沒有 CAP_IPC_OWNER 功能。
EFAULT 引數 cmd 的值為 IPC_SETIPC_STAT,但 buf 指向的地址不可訪問。
EIDRM 訊息佇列已刪除。
EINVAL cmdmsqid 的值無效。或者:對於 MSG_STAT 操作,msqid 中指定的索引值指的是當前未使用的陣列槽。
EPERM 引數 cmd 的值為 IPC_SETIPC_RMID,但呼叫程序的有效使用者 ID 不是訊息佇列的建立者(在 msg_perm.cuid 中找到)或所有者(在 msg_perm.uid 中找到),並且程序沒有特權(Linux:它沒有 CAP_SYS_ADMIN 功能)。

註釋

IPC_INFOMSG_STATMSG_INFO 操作由 ipcs(8) 程式用於提供有關已分配資源的資訊。將來,這些操作可能會被修改或移動到 /proc 檔案系統介面。

在 Linux 2.2 下,struct msqid_ds 中的各個欄位是短整型,在 Linux 2.4 下已成為長整型。為了利用這一點,在 glibc-2.1.91 或更高版本下重新編譯就足夠了。(核心透過 cmd 中的 IPC_64 標誌區分舊呼叫和新呼叫。)

符合標準

SVr4, POSIX.1-2001。

參見



previous next Printer Friendly

廣告


  

廣告



廣告