A,B通信,消息队列

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值