免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5435 | 回复: 5
打印 上一主题 下一主题

msgget(),msgrcv的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-04 09:47 |只看该作者 |倒序浏览
在fedora7.0平台上写一个消息队列的代码,有一个奇怪的问题请教:
如果用msgget(key, IPC_CREAT | 0600)来创建消息队列,在colinux上的fedora7.0可以正常运行,但是在vmware上的fedora7.0却提示“Permission denied”,如下:
[lb@lb server]$ ./server
msgget:create: Permission denied

还是在vmware上的fedora7.0,如果用msgget(key, IPC_CREAT | 0000)来创建消息队列,那么,msgget似乎可以了,可是在msgrcv的时候又出错了,也是“Permission denied”,如下:
[lb@lb server]$ ./server
msgrcv err!: Permission denied


想知道这是什么原因?msgget的0600这个值是怎么取得的?

另外,我用umask查看了两个环境的值,都是0002,这应该不会是原因吧?

_____________________代码_________________________________________________


#include <sys/types.h>        ///////////////////////////
#include <sys/ipc.h>        //// msgget(), msgrcv() ///
#include <sys/msg.h>        ///////////////////////////
#include <stdio.h>
#include <stdlib.h>

#define BUFSZ 512

/* Message structure */
struct msg {
        long msg_type;
        char msg_text[BUFSZ];
};

int main(void)
{
        int qid; /* The queue identifier */
        key_t key; /* The queue key */
       
        key = 125;
       
        struct msg pmsg; /* A message structure */
        int len; /* Length of message */
       
        /* Create the queue */
        //if((qid = msgget(key, IPC_CREAT | 0666)) < 0)
        if((qid = msgget(key, IPC_CREAT | 0600)) < 0)
        {
                perror("msgget:create");
                exit(EXIT_FAILURE);
        }
        //printf("created queue id = %d\n", qid);
       
        /* Open the queue again */
        if((qid == msgget(key, 0)) < 0)
        {
                perror("msgget:open");
                exit(EXIT_FAILURE);
        }
        //printf("opened queue id = %d\n", qid);
       
        /* Retrieve and display a message from the queue */
        len = msgrcv(qid, &pmsg, BUFSZ, 0, 0);
        if(len > 0)
        {
                (&pmsg)->msg_text[len] = '\0';
                printf("reading queue id: %05d\n", qid);
                printf("message type: %05ld\n", (&pmsg)->msg_type);
                printf("message length: %d bytes\n", len);
                printf("message text: %s\n", (&pmsg)->msg_text);
        }
        else
        {
                perror("msgrcv err!");
                exit(EXIT_FAILURE);
        }
       
        exit(EXIT_SUCCESS);
}

论坛徽章:
0
2 [报告]
发表于 2009-03-04 09:54 |只看该作者
报告一个新线索:
还是在vmware上的fedora7.0,但是用root权限运行

也还是用“msgget(key, IPC_CREAT | 0000)”或
“msgget(key, IPC_CREAT | 0600)”来创建消息队列

都可以正常的运行。

论坛徽章:
0
3 [报告]
发表于 2009-03-04 10:16 |只看该作者
原来好像有人遇到过。
是不是你用的那个key已经被用掉了,然后你又没有权限去打开那个消息队列。
加个IPC_EXCL试下

论坛徽章:
0
4 [报告]
发表于 2009-03-04 11:17 |只看该作者
用“msgget(key, IPC_CREAT | IPC_EXCL)”,提示是“File exists”:
[**@** server]$ ./server
msgget:create: File exists

论坛徽章:
0
5 [报告]
发表于 2009-03-04 12:53 |只看该作者
原帖由 reer 于 2009-3-4 09:47 发表
在fedora7.0平台上一个消息队列的代码,有一个奇怪的问题请教:
如果用msgget(key, IPC_CREAT | 0600)来创建消息队列,在colinux上的fedora7.0可以正常运行,但是在vmware上的fedora7.0却提示“Permission d ...

to: LZ

你第一行挺吓人的


0000 或 0600 部分是权限控制

论坛徽章:
0
6 [报告]
发表于 2009-03-04 13:43 |只看该作者
呵呵,我这里看起来都正常的啊?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP