我正在C linux中实现消息队列。我正在发送integer = 17
并且正在接收integer = 0
。请参阅下面的内容,让我知道我的msgsnd
和msgrcv
函数有什么问题。请注意:rbuf
将数据存储在rbuf->m->msglen
还是rbuf->mtype
中。
发送过程中
msgsnd(msqid, sbuf,sizeof(int), 0);
printf("nmsglen = %d",rbuf->m->msglen); // 17
在接收过程中。两者都有相同的msqid。我已经验证了。
msgrcv(msqid, rbuf, sizeof(int), 1, 0);
printf("nmsglen = %d",rbuf->m->msglen); // 0
//msqid=98305, some valid id
这是我在另一个文件中定义的结构定义。
typedef struct message1
{
int msglen;
unsigned char *cp;
}msg1;
typedef struct msgbuf
{
long mtype;
msg1 *m;
} message_buf;
您发送的消息包含指向message1结构的指针。接收进程取消引用该指针,但在该进程中,它不指向相同的东西。事实上,我很惊讶你没有得到一个segfault。
你应该这样定义msgbuf:
typedef struct msgbuf
{
long mtype;
msg1 m;
} message_buf;
以便msg1结构包含在msgbuf中,而不是由它指向。
此外,您需要指定的大小是sizeof(message_buf(,而不是sizeof(int(。
//header files
#include"msgbuf.h" //where I have put my structures
#define AUTOMATIC 1
#define USER_DATA 2
int main()
{
int msgflg = IPC_CREAT | 0666,ch,len=0;
size_t msgsize = 0;
int msqid;
key_t key;
message_buf *sbuf;
char ans;
char *data;
key = ftok("/home/user",15);
printf("Do you want to send messagest");
scanf("%c",&ans);
getchar();
if (((ans=='y' || ans=='Y') && (msqid = msgget(key, msgflg )) ==-1))
{ perror("msgget");
exit(1);
}
else
fprintf(stderr,"msgget: msgget succeeded: msqid = %dn", msqid);
while(ans=='y' || ans=='Y')
{
printf("n1. Automatic datan2. Enter datan3. ExitnEnter your choicet");
scanf("%d",&ch);
getchar();
switch(ch)
{
case AUTOMATIC: len=strlen("Did you get this?");
sbuf=malloc(len+sizeof(int));
memset(sbuf, 0, sizeof(message_buf));
sbuf->m=malloc(len+sizeof(int));
memset(sbuf->m, 0, sizeof(msg1));
sbuf->m->msglen=len;
sbuf->m->cp=malloc(sizeof(len));
strncpy(sbuf->m->cp, "Did you get this?",strlen("Did you get this?"));
sbuf->m->cp[strlen(sbuf->m->cp)]=' ';
break;
case USER_DATA: printf("nEnter datat");
fflush(stdout);
len = getline(&data, &msgsize, stdin);
sbuf=malloc(len+sizeof(int));
memset(sbuf, 0, sizeof(message_buf));
sbuf->m=malloc(len+sizeof(int));
memset(sbuf->m, 0, sizeof(msg1));
strcpy(sbuf->m->cp, data);
sbuf->m->cp[strlen(sbuf->m->cp)]=' ';
sbuf->m->msglen=len;
break;
case 3: msgctl(msqid, IPC_RMID, NULL);
printf("nQueue with q id = %d is removedn",msqid);
exit(1);
default:printf("nTRY AGAINt");
scanf("%c",&ans);
getchar();
}
printf("nmsglen = %dnmsgcp= %s",sbuf->m->msglen,sbuf->m->cp);
/* Send a message */
sbuf->mtype=1;
if (msgsnd(msqid, sbuf,2*sizeof(int), 0) < 0)
{
printf("Msg q id= %dnMsg type= %dnMsg Text %snMsg Len= %dn", msqid, sbuf->mtype, sbuf->m->cp,sbuf->m->msglen);
perror("msgsnd");
exit(1);
}
else
printf("nMessage: %sn Sentn", sbuf->m->cp);
}
msgctl(msqid, IPC_RMID, NULL);
printf("nQueue with q id = %d is removedn",msqid);
return 0;
}
正在接收代码
//header files
#include"msgbuf.h"
int main()
{
int msqid;
key_t key;
int msgflg = 0666;
message_buf *rbuf;
int msg_len_rcvd=0;
rbuf=malloc(150);
rbuf->m=malloc(100);
key = ftok("/home/user",15);
if ((msqid = msgget(key, msgflg)) ==-1)
{
perror("msgget");
exit(1);
}
printf("nn%dn",msqid);
/* Receive an answer of message type 1. */
while(1)
{
if ( (msg_len_rcvd=msgrcv(msqid, rbuf, 2*sizeof(int), 1, 0)) < 0)
{
perror("msgrcv");
exit(1);
}
else
{
printf("n Number of bytes received:: %d", msg_len_rcvd);
printf("nmtype1 = %d",rbuf->mtype);
printf("nmsglen= %d",rbuf->m->msglen);
}
break;
}
return 0;
}