【发布时间】:2019-03-17 08:14:42
【问题描述】:
我正在尝试从发送方 mq_sender() 接收 mq_receive() 数据,当我使用先运行发送文件时它可以工作.. 但是如果我先运行接收,即使在将数据发送到队列之后它也会阻塞..我在下面附上了我的代码
//sender
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<mqueue.h>
#include<string.h>
#include<unistd.h>
#define NAME "/queone"
#define MAX_SIZE 1021
void main()
{
int i;
struct mq_attr attr;
attr.mq_maxmsg = 10;
attr.mq_msgsize = MAX_SIZE;
int fd;
char s[MAX_SIZE]="hello";
int r1,r2;
mq_unlink(NAME);
perror("mq_unlink");
fd=mq_open(NAME,O_CREAT | O_WRONLY ,0664,&attr);
printf("fd=%d\n",fd);
for(i=0;i<5;i++)
r1=mq_send(fd,s,sizeof(s),0);
printf("%d",r1);
}
//receiver
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<mqueue.h>
#include<string.h>
#include<unistd.h>
#define NAME "/queone"
#define MAX_SIZE 1021
void main()
{
struct mq_attr attr;
attr.mq_maxmsg = 10;
attr.mq_msgsize = MAX_SIZE;
int fd,fd1,r;
char s[MAX_SIZE];
fd=mq_open(NAME,O_CREAT|O_RDONLY ,0664,&attr);
perror("mq_open");
printf("fd=%d\n",fd);
mq_getattr(fd,&attr);
printf("%d\n",attr.mq_curmsgs);
printf("%d\n",attr.mq_msgsize);
r=mq_receive(fd,s,sizeof(s),0);
printf("%d",r);
if(r==-1)
perror("mq_receive");
printf("%s\n",s);
mq_unlink(NAME);
}
如何处理
【问题讨论】:
标签: linux posix message-queue