- 论坛徽章:
- 0
|
最近刚开始使用消息队列进行事件的处理,但是在编译后发现程序可以正确的发送消息,但却不能接受到消息,这是什么问题?哪位高手能解决下。具体程序代码如下:
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf{
int mtype;
char mtext[1];
}msgsbuf;
struct msgmbuf{
int mtype;
char mtext[10];
}msgrbuf;
int msgid;
void catch_child(int sig_num)
{
int i=0;
char a;
extern char TxDataBuffer[2034];//文件发送缓冲区TxDataBuffer
FILE *ifp,*ofp;
ifp=fopen("p.c","r");
while(fscanf(ifp,"%c",&a)==1)
{
TxDataBuffer=a;
i++;
if(i==2034) break;
}
fclose(ifp);
printf("i=%d\n",i);
printf("%s\n",TxDataBuffer);
int gflags,rflags;
int reval;
extern int msgid;
msgsbuf.mtype=10;
msgsbuf.mtext[0]='a';
reval=msgsnd(msgid,&msgsbuf,sizeof(msgsbuf.mtext),0);//发送消息
if (reval==-1)
{
printf("message send error\n");
}
printf("reval=%d\n",reval);
}
char TxDataBuffer[2034];
main()
{
key_t key;
int rflags,gflags,reval;
extern int msgid;
char * msgpath="unix/msgqueue";
key=ftok(msgpath,'a');
gflags=IPC_CREAT|IPC_EXCL;
msgid=msgget(key,gflags|00666);//创建一个消息队列
if (msgid==-1)
{
printf("msg create error\n");
return;
}
signal(SIGINT, catch_child);
int child_pid[2];
reval=msgrcv(msgid,&msgrbuf,4,10,0);//读消息队列,没收到消息进程就阻塞
if (reval==-1)
printf("read mssage error\n");
else
printf("read from msg queue %d bytes\n",reval);
for (j=0; j<30; j++) {
printf("%d\n", j);
sleep(1);
}
} |
|