获取和设置消息队列的属性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[];
}; 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,删除消息队列的更多相关文章
- java 中利用反射机制获取和设置实体类的属性值
摘要: 在java编程中,我们经常不知道传入自己方法中的实体类中到底有哪些方法,或者,我们需要根据用户传入的不同的属性来给对象设置不同的属性值,那么,java自带的反射机制可以很方便的达到这种目的,同 ...
- java反射获取和设置实体类的属性值 递归所有父类
最近做一个通用数据操作接口,需要动态获取和设置实体类的属性值,为了通用实体做了多重继承,开始网上找到代码都不支持父类操作,只能自己搞一个工具类了,此工具类可以设置和获取所有父类属性,代码贴下面拿走不谢 ...
- Tabcontrol动态添加TabPage(获取或设置当前选项卡及其属性)
http://blog.csdn.net/xiongxyt2/article/details/6920575 •MultiLine 属性用true 或false来确定是否可以多行显示 •Appeara ...
- 锋利的jQuery-3--.css()获取和设置元素的数字属性
$('p').css({"fontSize": "30px", "backgroundColor": "#666"}); ...
- jQuery中使用attribute,prop获取,设置input的checked值【转】
1.prop方法获取.设置checked属性 当input控件checkbox设置了checked属性时,无论checked=”“或 checked=”checked”,$(obj).prop(“ch ...
- jQuery中使用attribute,prop获取,设置input的checked值
1.prop方法获取.设置checked属性 当input控件checkbox设置了checked属性时,无论checked=”“或 checked=”checked”,$(obj).prop(“ch ...
- RabbitMQ消费端ACK与重回队列机制,TTL,死信队列详解(十一)
消费端的手工ACK和NACK 消费端进行消费的时候,如果由于业务异常我们可以进行日志的记录,然后进行补偿. 如果由于服务器宕机等严重问题,那么我们就需要手工进行ACK保障消费端成功. 消费端重回队列 ...
- 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 ...
- jQuery -> 获取/设置/删除DOM元素的属性
jQuery的属性操作很easy,以下以一个a元素来说明属性的获取/设置/删除操作 <body> <a>jquery.com</a> </body> 加 ...
随机推荐
- nodejs 导出 exel文件 xlsx
参考: https://www.npmjs.com/package/node-xlsx Building a xlsx import xlsx from 'node-xlsx'; // Or var ...
- VC++实现程序重启的方法(转载)
转载:http://blog.csdn.net/clever101/article/details/9327597 很多时候系统有很多配置项,修改了配置项之后能有一个按钮实现系统重启.所谓重启就是杀死 ...
- C++11标准 STL正则表达式 验证电子邮件地址
转自:http://www.cnblogs.com/yejianfei/archive/2012/10/07/2713715.html 我们最经常遇到的验证,就是电子邮件地址验证.网站上常见.各种网页 ...
- #网页中动态嵌入PDF文件/在线预览PDF内容#
摘要:在web开发时我们有时会需要在线预览PDF内容,在线嵌入pdf文件: 问题1:如何网页中嵌入PDF: 在网页中: 常用的几种PDF预览代码片段如下: 代码片段1: 1 <object ty ...
- Error: Checksum mismatch.
bogon:bin macname$ brew install go ==> Downloading https://homebrew.bintray.com/bottles-portable- ...
- pyqt 渲染html
from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtWebE ...
- Ubuntu 定时任务
一.cron相关命令 #重载cron sudo service cron reload #查看cron状态 service cron status #查看cron pid pidof ...
- Windows 上安装 MySQL
Windows 上安装 MySQL https://www.mysql.com/downloads/ 1.下载MySQL安装包(官网下载链接): 选择 DOWNLOADS ——> Communi ...
- Python day20正则表达式和re方法
元字符6个函数以及几个元字符1.'.'通配符2.'^'以什么开头3.'$'以什么结尾4.'*'紧挨着的字符0~∞次5.'+'紧挨着的字符1~∞次6.'?'紧挨的字符0次或1次7.'{}' {0,}== ...
- a 样式重置 常见用法
样式重置 a:link, a:visited, a:hover, a:active{ color: #fff; text-decoration: none; } 常见用法 ( rel=& ...