#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
struct msgbuf
{
long mtype;
char mtext[128];
};
pthread_t tid_w,tid_r;
int msqid;
void *callBackw(void *arg) // A给B发消息
{
struct msgbuf sndbuf;
while (1)
{
memset(&sndbuf, 0, sizeof(sndbuf));
sndbuf.mtype = 1; // A发送的B接收的
if (0 == sndbuf.mtype)
{
break;
}
fgets(sndbuf.mtext, sizeof(sndbuf.mtext), stdin);
sndbuf.mtext[strlen(sndbuf.mtext) - 1] = '\0';
if (msgsnd(msqid, &sndbuf, sizeof(sndbuf.mtext), 0) < 0)
{
perror("msgsnd");
return NULL;
}
if (strcasecmp(sndbuf.mtext, "quit") == 0)
{
break;
}
}
pthread_cancel(tid_r);
pthread_exit(NULL);
}
void *callBackr(void *arg)
{
struct msgbuf rcvbuf;
ssize_t res = 0;
while (1)
{
memset(&rcvbuf, 0, sizeof(rcvbuf));
res = msgrcv(msqid, &rcvbuf, sizeof(rcvbuf.mtext), 2, 0);
if (res < 0)
{
perror("msgrcv");
break;
}
if (strcasecmp(rcvbuf.mtext, "quit") == 0)
{
break;
}
printf("%s\n", rcvbuf.mtext);
}
pthread_cancel(tid_w);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
key_t key=ftok("/home/ubuntu/",4);
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#x\n",key);
//创建消息队列,获取id号
int msqid = msgget(key,IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
//pthread_cancel(tid_w);
return -1;
}
printf("msqid = %d\n",msqid);
if(pthread_create(&tid_w,NULL,callBackw,NULL)!=0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid_r,NULL,callBackr,NULL)!=0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid_w,NULL);
pthread_join(tid_r,NULL);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
struct msgbuf
{
long mtype;
char mtext[128];
};
pthread_t tid_w,tid_r;
int msqid;
void *callBackw(void *arg) // B给A发消息
{
struct msgbuf sndbuf;
//pthread_cancel(tid_w);
while (1)
{
memset(&sndbuf, 0, sizeof(sndbuf));
sndbuf.mtype = 2; // B发送的A接收的
if (0 == sndbuf.mtype)
{
break;
}
fgets(sndbuf.mtext, sizeof(sndbuf.mtext), stdin);
sndbuf.mtext[strlen(sndbuf.mtext) - 1] = '\0';
if (msgsnd(msqid, &sndbuf, sizeof(sndbuf.mtext), 0) < 0)
{
perror("msgsnd");
return NULL;
}
if (strcasecmp(sndbuf.mtext, "quit") == 0)
{
break;
}
}
pthread_cancel(tid_r);
pthread_exit(NULL);
}
void *callBackr(void *arg)
{
struct msgbuf rcvbuf;
ssize_t res = 0;
while (1)
{
memset(&rcvbuf, 0, sizeof(rcvbuf));
res = msgrcv(msqid, &rcvbuf, sizeof(rcvbuf.mtext), 1, 0);
if (res < 0)
{
perror("msgrcv");
break;
}
if (strcasecmp(rcvbuf.mtext, "quit") == 0)
{
break;
}
printf("%s\n", rcvbuf.mtext);
}
pthread_cancel(tid_w);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
key_t key=ftok("/home/ubuntu/",4);
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#x\n",key);
//创建消息队列,获取id号
int msqid = msgget(key,IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
printf("msqid = %d\n",msqid);
if(pthread_create(&tid_w,NULL,callBackw,NULL)!=0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid_r,NULL,callBackr,NULL)!=0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid_w,NULL);
pthread_join(tid_r,NULL);
return 0;
}