A进程
```c
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include <pthread.h>
struct msgbuf
{
long mtype;
char mtext[128];
};
struct msgarr
{
long ntype;
char ntext[128];
};
int msqid,msqid2;
struct msgarr snd;
struct msgbuf rcv;
ssize_t res =0;
void* callBack_RD(void* arg)
{
while(1)
{
res=msgrcv(msqid2,&rcv,sizeof(rcv.mtext),0,0);
if(res<0)
{
perror("msgrcv");
return NULL;
}
printf("res=%ld [%ld : %s]\n",res,rcv.mtype,rcv.mtext);
if(strcasecmp(rcv.mtext, "quit") == 0)
exit(0);
}
pthread_exit(NULL);
}
void* callBack_WR(void* arg)
{
while(1)
{
scanf("%ld",&snd.ntype);
getchar();
fgets(snd.ntext,sizeof(snd.ntext),stdin);
snd.ntext[strlen(snd.ntext)-1]=0;
if(msgsnd(msqid,&snd,sizeof(snd.ntext),0)<0)
{
perror("msgsnd");
return NULL;
}
printf("消息内容发送成功\n");
if(strcasecmp(snd.ntext, "quit") == 0)
exit(0);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1, tid2;
//创建一个线程,写
if(pthread_create(&tid1, NULL, callBack_WR, NULL) != 0)
{
perror("pthread_create");
return -1;
}
//创建一个线程,读
if(pthread_create(&tid2, NULL, callBack_RD, NULL) != 0)
{
perror("pthread_create");
return -1;
}
key_t key =ftok("./",1);
if(key<0)
{
perror("ftok");
return -1;
}
printf("key=%#x\n",key);
key_t key2 =ftok("./",2);
if(key2<0)
{
perror("ftok");
return -1;
}
printf("key=%#x\n",key2);
msqid=msgget(key,IPC_CREAT|0664);
if(msqid<0)
{
perror("msgget");
return -1;
}
printf("msqid= %d\n",msqid);
msqid2=msgget(key2,IPC_CREAT|0664);
if(msqid2<0)
{
perror("msgget");
return -1;
}
printf("msqid= %d\n",msqid2);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
msgctl(msqid2,IPC_RMID,NULL);
return 0;
}
B进程
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include <pthread.h>
struct msgbuf
{
long mtype;
char mtext[128];
};
struct msgarr
{
long ntype;
char ntext[128];
};
int msqid,msqid2;
struct msgbuf rcv;
ssize_t res =0;
struct msgarr snd;
void* callBack_RD(void* arg)
{
while(1)
{
res=msgrcv(msqid,&rcv,sizeof(rcv.mtext),0,0);
if(res<0)
{
perror("msgrcv");
return NULL;
}
printf("res=%ld [%ld : %s]\n",res,rcv.mtype,rcv.mtext);
if(strcasecmp(rcv.mtext, "quit") == 0)
exit(0);
}
pthread_exit(NULL);
}
void* callBack_WR(void* arg)
{
while(1)
{
scanf("%ld",&snd.ntype);
getchar();
fgets(snd.ntext,sizeof(snd.ntext),stdin);
snd.ntext[strlen(snd.ntext)-1]=0;
if(msgsnd(msqid2,&snd,sizeof(snd.ntext),0)<0)
{
perror("msgsnd");
return NULL;
}
printf("消息内容发送成功\n");
if(strcasecmp(snd.ntext, "quit") == 0)
exit(0);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1, tid2;
//创建一个线程,写
if(pthread_create(&tid1, NULL, callBack_WR, NULL) != 0)
{
perror("pthread_create");
return -1;
}
//创建一个线程,读
if(pthread_create(&tid2, NULL, callBack_RD, NULL) != 0)
{
perror("pthread_create");
return -1;
}
//创建key
key_t key =ftok("./",1);
if(key<0)
{
perror("ftok");
return -1;
}
printf("key=%#x\n",key);
key_t key2 =ftok("./",2);
if(key2<0)
{
perror("ftok");
return -1;
}
printf("key=%#x\n",key2);
//创建消息队列
msqid=msgget(key,IPC_CREAT|0664);
if(msqid<0)
{
perror("msgget");
return -1;
}
printf("msqid= %d\n",msqid);
msqid2=msgget(key2,IPC_CREAT|0664);
if(msqid2<0)
{
perror("msgget");
return -1;
}
printf("msqid= %d\n",msqid2);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
msgctl(msqid,IPC_RMID,NULL);
return 0;
}