獲取和設置消息隊列的屬性msgctl,刪除消息隊列


消息隊列的屬性保存在系統維護的數據結構msqid_ds中,用戶可以通過函數msgctl獲取或設置消息隊列的屬性。

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

msgctl:系統調用對msgqid標識的消息隊列執行cmd操作,系統定義了3種cmd操作:

IPC_STAT:該命令用來獲取消息隊列對應的msqid_ds數據結構,並將其保存到buf指向的地址空間

IPC_SET:該命令用來設置消息隊列的屬性,要設置的屬性存儲在buf中,可設置的屬性包括:

msg_perm.uid 、 msg_perm.gid、msg_perm.mode以及msg_qbytes

IPC_RMID:從內核中刪除msgqid標識的消息隊列

 

struct msqid_ds {
struct ipc_perm msg_perm; /* Ownership and permissions */
time_t msg_stime; /* Time of last msgsnd(2) */
time_t msg_rtime; /* Time of last msgrcv(2) */
time_t msg_ctime; /* Time of last change */
unsigned long __msg_cbytes; /* Current number of bytes in
queue (nonstandard) */
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(2) */
pid_t msg_lrpid; /* PID of last msgrcv(2) */
};

 

 

 

struct ipc_perm {
key_t __key; /* Key supplied to msgget(2) */
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 */
};

代碼示例:

 

#include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include <stdlib.h> #include <string.h> #include <sys/msg.h> #include <time.h>



//用戶自定義消息緩沖
struct mymsgbuf{ long msgtype; char buf[256]; }; void showmsgattr(int qid, struct msqid_ds buf) { if(msgctl(qid, IPC_STAT, &buf) == -1) { perror("msgctl error:"); return; } printf("***information of message queue%d****\n", qid); printf("msg_stime:%s\n", ctime(&(buf.msg_stime))); printf("msg_rtime:%s\n", ctime(&(buf.msg_rtime))); printf("last change msg time is:%s\n", ctime(&(buf.msg_ctime))); printf("number of message in queue is:%lu\n", buf.msg_qnum); printf("msg uid is:%lu\n", buf.msg_perm.uid); printf("***information end*******************\n"); } int main() { struct mymsgbuf mymsgbuffer; int msglen = 0; int i = 0; int msgkey = 0; struct msqid_ds msgattr; int qid = 0;//消息隊列標識符 //獲取鍵值
    msgkey = ftok(".", 11); qid = msgget(msgkey, IPC_CREAT|0660); printf("msgget return %d\n", qid); //輸出消息隊列的屬性
 showmsgattr(qid, msgattr); //填充消息結構,發送到消息隊列
    msglen = sizeof(struct mymsgbuf) - 4; strcpy(mymsgbuffer.buf , "manman"); mymsgbuffer.msgtype = 4; if (msgsnd(qid, &mymsgbuffer, msglen, 0) == -1) { perror("msgsnd error\n"); exit(1); } //消息發送后輸出消息隊列的屬性
 showmsgattr(qid, msgattr); //設置消息隊列的屬性
    msgattr.msg_perm.uid = 33; msgctl(qid, IPC_SET, &msgattr); //設置屬性后輸出消息隊列的屬性
 showmsgattr(qid, msgattr); //刪除后再輸出消息隊列的屬性
 msgctl(qid, IPC_RMID, NULL); showmsgattr(qid, msgattr); return 0; }

 

 

 

   

 執行結果:

 

msgget return 32768
***information of message queue32768****
msg_stime:Thu Jan 1 08:00:00 1970

 

msg_rtime:Thu Jan 1 08:00:00 1970

 

last change msg time is:Thu Apr 20 13:29:25 2017

 

number of message in queue is:0
msg uid is:0
***information end*******************
***information of message queue32768****
msg_stime:Thu Apr 20 13:29:25 2017

 

msg_rtime:Thu Jan 1 08:00:00 1970

 

last change msg time is:Thu Apr 20 13:29:25 2017

 

number of message in queue is:1
msg uid is:0
***information end*******************
***information of message queue32768****
msg_stime:Thu Apr 20 13:29:25 2017

 

msg_rtime:Thu Jan 1 08:00:00 1970

 

last change msg time is:Thu Apr 20 13:29:25 2017

 

number of message in queue is:1
msg uid is:33
***information end*******************
msgctl error:: Invalid argument

 

1、發送消息后消息隊列的屬性會改變

2、修改了屬性后,消息隊列的屬性會改變。

 

----------------------------------------------------

關於消息隊列的刪除:

msgctl(qid, IPC_RMID, NULL);

刪除前ipcs指令查看:

 

------ Message Queues --------
key     msqid    owner    perms   used-bytes    messages
0x0b014424   0      root    660      256      1

 

 

刪除后:

 

------ Message Queues --------
key    msqid   owner   perms   used-bytes    messages

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM