消息队列的属性保存在系统维护的数据结构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[];
}; void showmsgattr(int qid, struct msqid_ds buf)
{
if(msgctl(qid, IPC_STAT, &buf) == -)
{
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 = ;
int i = ;
int msgkey = ;
struct msqid_ds msgattr; int qid = ;//消息队列标识符 //获取键值
msgkey = ftok(".", ); qid = msgget(msgkey, IPC_CREAT|);
printf("msgget return %d\n", qid); //输出消息队列的属性
showmsgattr(qid, msgattr); //填充消息结构,发送到消息队列
msglen = sizeof(struct mymsgbuf) - ;
strcpy(mymsgbuffer.buf , "manman");
mymsgbuffer.msgtype = ;
if (msgsnd(qid, &mymsgbuffer, msglen, ) == -)
{
perror("msgsnd error\n");
exit();
}
//消息发送后输出消息队列的属性
showmsgattr(qid, msgattr); //设置消息队列的属性
msgattr.msg_perm.uid = ;
msgctl(qid, IPC_SET, &msgattr);
//设置属性后输出消息队列的属性
showmsgattr(qid, msgattr); //删除后再输出消息队列的属性
msgctl(qid, IPC_RMID, NULL);
showmsgattr(qid, msgattr); return ;
}

执行结果:

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

获取和设置消息队列的属性msgctl,删除消息队列的更多相关文章

  1. java 中利用反射机制获取和设置实体类的属性值

    摘要: 在java编程中,我们经常不知道传入自己方法中的实体类中到底有哪些方法,或者,我们需要根据用户传入的不同的属性来给对象设置不同的属性值,那么,java自带的反射机制可以很方便的达到这种目的,同 ...

  2. java反射获取和设置实体类的属性值 递归所有父类

    最近做一个通用数据操作接口,需要动态获取和设置实体类的属性值,为了通用实体做了多重继承,开始网上找到代码都不支持父类操作,只能自己搞一个工具类了,此工具类可以设置和获取所有父类属性,代码贴下面拿走不谢 ...

  3. Tabcontrol动态添加TabPage(获取或设置当前选项卡及其属性)

    http://blog.csdn.net/xiongxyt2/article/details/6920575 •MultiLine 属性用true 或false来确定是否可以多行显示 •Appeara ...

  4. 锋利的jQuery-3--.css()获取和设置元素的数字属性

    $('p').css({"fontSize": "30px", "backgroundColor": "#666"}); ...

  5. jQuery中使用attribute,prop获取,设置input的checked值【转】

    1.prop方法获取.设置checked属性 当input控件checkbox设置了checked属性时,无论checked=”“或 checked=”checked”,$(obj).prop(“ch ...

  6. jQuery中使用attribute,prop获取,设置input的checked值

    1.prop方法获取.设置checked属性 当input控件checkbox设置了checked属性时,无论checked=”“或 checked=”checked”,$(obj).prop(“ch ...

  7. RabbitMQ消费端ACK与重回队列机制,TTL,死信队列详解(十一)

    消费端的手工ACK和NACK 消费端进行消费的时候,如果由于业务异常我们可以进行日志的记录,然后进行补偿. 如果由于服务器宕机等严重问题,那么我们就需要手工进行ACK保障消费端成功. 消费端重回队列 ...

  8. termios, tcgetattr, tcsetattr, tcsendbreak, tcdrain, tcflush, tcflow, cfmakeraw, cfgetospeed, cfgetispeed, cfsetispeed, cfsetospeed - 获取和设置终端属性,行控制,获取和设置波特率

    SYNOPSIS 总览 #include <termios.h> #include <unistd.h> int tcgetattr(int fd, struct termio ...

  9. jQuery -&gt; 获取/设置/删除DOM元素的属性

    jQuery的属性操作很easy,以下以一个a元素来说明属性的获取/设置/删除操作 <body> <a>jquery.com</a> </body> 加 ...

随机推荐

  1. CSS实现三角形、梯形、平行四边形、圆形、椭圆形、对话框、自适应正方形

    本文篇幅较长,希望能坚持看完,转载请注明出处,如果觉得好文请给个赞吧 CSS实现梯形 CSS实现三角形和梯形主要是依靠border是梯形的特性来做的,有点像相框的那种感觉. 首先我们先给一个正方形设置 ...

  2. poj Meteor Shower - 搜索

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16313   Accepted: 4291 Description Bess ...

  3. bzoj 2427 软件安装 - Tarjan - 树形动态规划

    题目描述 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大). 但是 ...

  4. python函数总结

    1.函数是一种子程序.程序员使用函数来减少代码重复,并用于组织或模块化程序.一旦定义了函数,它可以从程序中的许多不同位置被多次调用.参数允许函数具有可更改的部分.函数定义中出现的参数称之为形参,函数调 ...

  5. 关于即来即停app的功能

    Asmallpark软件接口文档说明 编码均采用UTF-8格式传输全部为http,POST请求状态码:200  操作成功    100  服务器异常,稍后再试  404  请求非法  402  数据库 ...

  6. SRLTE,SGLTE,SVLTE,CSFB,VoLTE的区别【转】

    本文转载自:https://blog.csdn.net/dangbochang/article/details/43851979 SRLTE——Single Radio LTE,俗称单待LTE. SG ...

  7. hystrix两种隔离模式分析

    hystrix隔离模式目前有两种方式:信号量模式和线程池模式. 但信号量并不支持超时,当被调服务发生问题时,有少部分用户会长时间无法得到响应. 另外,使用线程池模式无法传递Header,我估计是由于线 ...

  8. dp暑假专题 训练记录

    A 回文串的最小划分 题意:给出长度不超过1000的字符串,把它分割成若干个回文字串,求能分成的最少字串数. #include <iostream> #include <cstdio ...

  9. python 字符串转变量方法

    1.response=eval('requests.'+func.lower())(destURL, headers=requestHeaders, data=postData, params=que ...

  10. BZOJ4896 [Thu Summer Camp2016]补退选

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...