消息队列的属性保存在系统维护的数据结构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