Day10.19

#include <myhead.h>

typedef struct msgbuf
{
	long mtype;
	char mtext[1024];

}msg_t;


#define SIZE sizeof(msg_t)-sizeof(long)


void* funcw(void* arg)
{
	key_t wkey=ftok("./",'k');
	if(wkey==-1)
	{
		perror("ftok");
		return NULL;
	}
	int mspid;
	if((mspid=msgget(wkey,IPC_CREAT|0664))==-1)
	{
		perror("msgget");
		return NULL;
	}
	msg_t info={.mtype=2};
	while(1)
	{
		bzero(info.mtext,SIZE);
		fgets(info.mtext,SIZE,stdin);
		info.mtext[strlen(info.mtext)-1]=0;
		msgsnd(mspid,&info,SIZE,0);

		if(!strcmp(info.mtext,"quit"))
		{
			exit(0);
		}

	}

	return NULL;

}

void* funcr(void* arg)
{
	key_t rkey;
	if((rkey=ftok("./",'k'))==-1)
	{
		perror("ftok");
		return NULL;
	}
	int mspid;
	if((mspid=msgget(rkey,IPC_CREAT|0664))==-1)
	{
		perror("msgget");
		return NULL;
	}

	msg_t info;
	while(1)
	{
		bzero(info.mtext,SIZE);
		msgrcv(mspid,&info,SIZE,1,0);

		if(!strcmp(info.mtext,"quit"))
		{
			exit(0);
		}
		printf("%s\n",info.mtext);
	}

	return NULL;
}


int main(int argc, const char *argv[])
{
	pthread_t wfd;
	if((pthread_create(&wfd,NULL,funcw,NULL))!=0)
	{
		printf("pthread create failed!\n");
		return -1;
	}

	pthread_t rfd;
	if((pthread_create(&rfd,NULL,funcr,NULL))!=0)
	{
		printf("pthread create failed!\n");
		return -1;
	}





	pthread_join(wfd,NULL);
	pthread_join(rfd,NULL);

	return 0;
}
#include <myhead.h>

typedef struct msgbuf
{
	long mtype;
	char mtext[1024];

}msg_t;


#define SIZE sizeof(msg_t)-sizeof(long)


void* funcw(void* arg)
{
	key_t wkey=ftok("./",'k');
	if(wkey==-1)
	{
		perror("ftok");
		return NULL;
	}
	int mspid;
	if((mspid=msgget(wkey,IPC_CREAT|0664))==-1)
	{
		perror("msgget");
		return NULL;
	}
	msg_t info={.mtype=1};
	while(1)
	{
		bzero(info.mtext,SIZE);
		fgets(info.mtext,SIZE,stdin);
		info.mtext[strlen(info.mtext)-1]=0;
		msgsnd(mspid,&info,SIZE,0);

		if(!strcmp(info.mtext,"quit"))
		{
			exit(0);
		}

	}

	return NULL;

}

void* funcr(void* arg)
{
	key_t rkey;
	if((rkey=ftok("./",'k'))==-1)
	{
		perror("ftok");
		return NULL;
	}
	int mspid;
	if((mspid=msgget(rkey,IPC_CREAT|0664))==-1)
	{
		perror("msgget");
		return NULL;
	}

	msg_t info;
	while(1)
	{
		bzero(info.mtext,SIZE);
		msgrcv(mspid,&info,SIZE,2,0);

		if(!strcmp(info.mtext,"quit"))
		{
			exit(0);
		}
		printf("%s\n",info.mtext);

	}
	return NULL;
}


int main(int argc, const char *argv[])
{
	pthread_t wfd;
	if((pthread_create(&wfd,NULL,funcw,NULL))!=0)
	{
		printf("pthread create failed!\n");
		return -1;
	}

	pthread_t rfd;
	if((pthread_create(&rfd,NULL,funcr,NULL))!=0)
	{
		printf("pthread create failed!\n");
		return -1;
	}





	pthread_join(wfd,NULL);
	pthread_join(rfd,NULL);

	return 0;
}

用消息队列实现通信。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值