[英]cannot set POSIX message queue attribute
我的環境:
我正在嘗試為POSIX消息隊列設置屬性,但代碼不會更改屬性。 我只獲取默認屬性值。
你能指出我的代碼有什么問題嗎?
我以用戶(不是root用戶)的身份執行a.out。
#include <stdio.h>
#include <mqueue.h> // for message queue
#include <sys/stat.h>
#include <stdlib.h> // for EXIT_FAILURE
#include <string.h>
/*
gcc [file] -lrt
*/
static void showAttr(mqd_t mqd)
{
struct mq_attr attr;
mq_getattr(mqd, &attr);
printf("maxmsg = %d\n", attr.mq_maxmsg);
printf("msgsize = %d\n", attr.mq_msgsize);
printf("curmsgs = %d\n", attr.mq_curmsgs);
}
int main()
{
mqd_t mqd;
int flags;
int ret;
struct mq_attr attr;
flags = O_RDWR | O_CREAT;
attr.mq_flags = 0; // or O_NONBLOCK
attr.mq_maxmsg = 60;
attr.mq_msgsize = 120;
attr.mq_curmsgs = 0;
// POSIX IPC name should start with "/"
mqd = mq_open("/mq", flags,
// (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
0644,
&attr );
if (mqd < 0) {
printf("open failed\n");
exit(EXIT_FAILURE);
}
printf("open ok\n");
sleep(1);
showAttr(mqd);
ret = mq_close(mqd);
if (ret != 0) {
printf("open failed\n");
exit(EXIT_FAILURE);
}
printf("close ok\n");
return 0;
}
我發現以下代碼有效。 但是,當我嘗試設置attr.mq_maxmsg(= 60)時,mq_open失敗。
#include <stdio.h>
#include <mqueue.h> // for message queue
#include <sys/stat.h>
#include <stdlib.h> // for EXIT_FAILURE
#include <string.h>
#include <errno.h>
/*
gcc [file] -lrt
*/
static void showAttr(mqd_t mqd)
{
struct mq_attr attr;
mq_getattr(mqd, &attr);
printf("maxmsg = %d\n", attr.mq_maxmsg);
printf("msgsize = %d\n", attr.mq_msgsize);
printf("curmsgs = %d\n", attr.mq_curmsgs);
}
int main()
{
mqd_t mqd;
int flags;
int ret;
struct mq_attr attr;
flags = O_RDWR | O_CREAT;
// POSIX IPC name should start with "/"
// 1. once open without attribute setting
mqd = mq_open("/mq", flags,
(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) );
mq_getattr(mqd, &attr);
mq_unlink("/mq");
mq_close(mqd);
// 2. set values of attribute
// attr.mq_maxmsg = 10;
attr.mq_msgsize = 120;
// 3. allocate attribute
mqd = mq_open("/mq", flags,
(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
// 0644,
&attr );
if (mqd < 0) {
printf("open failed %d\n", mqd);
exit(EXIT_FAILURE);
}
printf("open ok\n");
sleep(1);
showAttr(mqd);
ret = mq_close(mqd);
if (ret != 0) {
printf("open failed\n");
exit(EXIT_FAILURE);
}
printf("close ok\n");
return 0;
}
mq_open()
返回一個已存在的隊列,該隊列的屬性是在創建時設置的。 因此,O_CREAT標志無效,您指定的屬性也會被忽略。
在打開之前調用mq_unlink()
,也可以設置O_EXCL,並查看差異。
以下代碼有效。
我在mq_open()之前使用mq_unlink()作為pilcrow建議。
而且,問題的另一個原因是我將attr.mq_maxmsg設置為大於10(在/ proc / sys / fs / mqueue / msg_max中定義)。 如果我將attr.mq_maxmsg設置為小於等於10,則設置屬性沒有問題。
#include <stdio.h>
#include <mqueue.h> // for message queue
#include <sys/stat.h>
#include <stdlib.h> // for EXIT_FAILURE
#include <string.h>
#include <errno.h>
/*
gcc [file] -lrt
*/
static void showAttr(mqd_t mqd)
{
struct mq_attr attr;
mq_getattr(mqd, &attr);
printf("maxmsg = %d\n", attr.mq_maxmsg);
printf("msgsize = %d\n", attr.mq_msgsize);
printf("curmsgs = %d\n", attr.mq_curmsgs);
}
int main()
{
mqd_t mqd;
int flags;
int ret;
struct mq_attr attr;
flags = O_RDWR | O_CREAT;
mq_unlink("/mq");
attr.mq_flags = 0;
attr.mq_maxmsg = 3; // ***
attr.mq_msgsize = 141;
attr.mq_curmsgs = 0;
mqd = mq_open("/mq", flags,
(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
&attr );
if (mqd < 0) {
printf("open failed %d\n", mqd);
exit(EXIT_FAILURE);
}
printf("open ok\n");
sleep(1);
showAttr(mqd);
ret = mq_close(mqd);
if (ret != 0) {
printf("open failed\n");
exit(EXIT_FAILURE);
}
printf("close ok\n");
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.