空白msgrcv缓冲区

空白msgrcv缓冲区,c,message-queue,C,Message Queue,我尝试运行此处给出的呼叫者和接收者之间电话对话的消息队列程序,并进行了更正。我不断得到以下结果: ./caller Caller Program <<< CALLER:Hello!! /caller 调用程序 如果其中一个库方法返回-1,通常最好调用peror(),而不是printf()打印有关实际错误的信息。您期望的结果是什么?我在一个终端中调用./caller,在另一个终端中调用./receiver。因此,我在终端1中键入的内容应该在终端2中显示。但不,它不显示。

我尝试运行此处给出的呼叫者和接收者之间电话对话的消息队列程序,并进行了更正。我不断得到以下结果:

 ./caller
 Caller Program
 <<<
 CALLER:Hello!!
/caller
调用程序

如果其中一个库方法返回
-1
,通常最好调用
peror()
,而不是
printf()
打印有关实际错误的信息。您期望的结果是什么?我在一个终端中调用./caller,在另一个终端中调用./receiver。因此,我在终端1中键入的内容应该在终端2中显示。但不,它不显示。调用方终端接收消息,但接收方终端不显示消息。正如我所说,尝试使用
打印调试消息。
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>

struct msgbuf
{
long mtype;
char mtext[140];
}msg_buf;

int send_msg_id,receive_msg_id;
key_t send_key,receive_key;

void *send_message(void *a)
{
send_key=ftok("Caller1.c",'A');
if (send_key==-1)
{
    printf("\nCaller send key error");
    exit(1);
}
send_msg_id=msgget(send_key,0666 | IPC_CREAT);
if (send_msg_id==-1)
{   
    printf("\nCaller send msgget error");
    exit(1);
}
printf("\nCALLER:");
while (fgets(msg_buf.mtext,sizeof(msg_buf.mtext),stdin)!=NULL)
{
    msg_buf.mtype=1;
    int len=strlen(msg_buf.mtext);
    if (msg_buf.mtext[len-1]=='\n');
        msg_buf.mtext[len-1]='\0';
    if (msgsnd(send_msg_id,&msg_buf,len+1,0)==-1)
        printf("\nMsg sending error\n");
}
int i=0;
while(i<9999)
    i++;
msgctl(send_msg_id,IPC_RMID,NULL);
return;
}

void *receive_message(void *a)
{
receive_key=ftok("Receiver1.c",'B');
if (receive_key==-1)
{
    printf("\nReceiver send key error");
    exit(1);
}
send_msg_id=msgget(receive_key,0777|IPC_CREAT);
if (receive_msg_id==-1)
{
    printf("\nCaller msgrcv error");
    exit(1);
}
printf("\n<<<");
while(1)
{
    if(msgrcv(receive_msg_id,&msg_buf,sizeof(msg_buf.mtext),1,0)!=-1)
        printf("<<<%s\n",msg_buf.mtext);
}
return;
 }

 void initialize()
 {
    pthread_t send_thread,receive_thread;
    pthread_create(&send_thread,NULL,send_message,NULL);
    pthread_create(&receive_thread,NULL,receive_message,NULL);
    pthread_join(send_thread,NULL);
    pthread_join(receive_thread,NULL);
    return;
 }

 int main() 
 {
    printf("\nCaller Program\n");
    initialize();
    return 0;
 }