代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/msg.h>
#include<unistd.h>
#include<sys/ipc.h>
void msg_show_attr(int msg_id,struct msqid_ds msg_info)
{
int ret=-1;
sleep(1);
ret=msgctl(msg_id,IPC_STAT,&msg_info);
if(-1==ret)
{
printf("obtain news information failed!\n");
return ;
}
printf("\n");
printf("now queue byte num:%d\n",msg_info.msg_cbytes);
printf("queue news num:%d\n",msg_info.msg_qnum);
printf("queue max byte num:%d\n",msg_info.msg_qbytes);
printf("finally send information jincheng pid:%d\n",msg_info.msg_lspid);
printf("finally recv information jincheng pid:%d\n",msg_info.msg_lrpid);
printf("finally send information time:%s\n",ctime(&(msg_info.msg_stime)));
printf("finally recv information time:%s\n",ctime(&(msg_info.msg_rtime)));
printf("finally change time: %s\n",ctime(&(msg_info.msg_ctime)));
printf("information UID is: %d\n",msg_info.msg_perm.uid);
printf("informatiomn GID is:%d\n",msg_info.msg_perm.gid);
}
int main(void)
{
int ret=-1;
int msg_flags,msg_id;
key_t key;
struct msgmbuf
{
int mtype;
char mtext[10];
};
struct msqid_ds msg_info;
struct msgmbuf msg_mbuf;
int msg_sflags,msg_rflags;
char *msgpath="ipc/msg/";
key=ftok(msgpath,'b');
if(key!=-1)
{
printf("succeed build KEY\n");
}
else
{
printf("build KEY failed\n");
}
msg_flags=IPC_CREAT|IPC_EXCL;
msg_id=msgget(key,msg_flags|0x777);
if(-1==msg_id)
{
printf("information build failed\n");
return 0;
}
msg_show_attr(msg_id,msg_info);
msg_sflags=IPC_NOWAIT;
msg_mbuf.mtype=10;
memcpy(msg_mbuf.mtext,"test news",sizeof("test news"));
ret=msgsnd(msg_id,&msg_mbuf,sizeof("test news"),msg_sflags);
if(-1==ret)
{
printf("send information failed\n");
}
msg_rflags=IPC_NOWAIT|MSG_NOERROR;
ret=msgrcv(msg_id,&msg_mbuf,10,10,msg_rflag);
if(-1==ret)
{
printf("recv information failed\n");
}
else
{
printf("recv information succeed,length,%d\n",ret);
}
msg_show_attr(msg_id,msg_info);
msg_info.msg_perm.uid=8;
msg_info.msg_perm.gid=8;
msg_info.msg_qbytes=12345;
ret=msgctl(msg_id,IPC_SET,&msg_info);
if(-1==ret)
{
printf("set information attribute failed!");
return 0;
}
msg_show_attr(msg_id,msg_info);
ret=msgctl(msg_id,IPC_RMID,NULL);
if(-1==ret)
{
printf("delete information failed\n");
return 0;
}
return 0;
}
运行结果:

请问专家,我把文件属性设为777,为什么发送消息接收消息都失败?谢谢!