2018.08.21

 

多线程实现通信双方互发消息

一个程序本身可以看做是一个进程,因而只需在程序内建立两个线程,用来接收和发送消息即可,退出时,可以通过一个线程控制另一个线程结束

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>

#define MSGKEY 1234
pthread_t tid1, tid2;
struct msgbuf 
{
	long mtype;     /* message type, must be > 0 */
	char mtext[64];  /* message data */
};

void *mythread(void *arg)
{
		struct msgbuf mbuf;
		int msgid = *(int *)arg;
		int old;
		int ret;
		pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&old);
		while(1)
		{
			memset(&mbuf, 0, sizeof(mbuf));
			
						
			mbuf.mtype = 1;
			scanf("%s",mbuf.mtext);
			ret = msgsnd(msgid,&mbuf,sizeof(mbuf.mtext), 0);
			if(ret == -1)
			{
				perror("msgsnd");
				exit(1);
			}
			if(!strcmp(mbuf.mtext,"bye"))
			{
				pthread_cancel(tid2);
				break;
			}

		}
}

void *mythread1(void *arg)
{
		struct msgbuf mbuf;
		int msgid = *(int *)arg;
		int old;
		int ret;

		pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&old);

	
			while(1)
		{
			ret = msgrcv(msgid,&mbuf,sizeof(mbuf.mtext), 2, 0);
			if(ret == -1)
			{
				perror("msgsnd");
				exit(1);
			}
			

			if(!strcmp(mbuf.mtext,"bye"))
			{
				pthread_cancel(tid1);
	
				break;
			}
			printf("\t\t\t%s\n",mbuf.mtext);

		}
	
}

int main()
{
		int ret;
	int msgid = msgget(MSGKEY,IPC_CREAT | IPC_EXCL);
	if(msgid == -1)
	{
		perror("msgget");
		exit(1);
	}
	ret = pthread_create(&tid1, NULL, mythread, &msgid);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(1);
	}
	ret = pthread_create(&tid2, NULL, mythread1, &msgid);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(1);
	}
	void *status;
	ret = pthread_join(tid1,&status);
	ret = pthread_join(tid2,&status);

	sleep(1);
	msgctl(msgid,IPC_RMID, NULL);
	return 0;
}

另一端:

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>

#define MSGKEY 1234
pthread_t tid1, tid2;
struct msgbuf 
{
	long mtype;     /* message type, must be > 0 */
	char mtext[64];  /* message data */
};

void *mythread(void *arg)
{
		struct msgbuf mbuf;
		int msgid = *(int *)arg;
		int old;
		int ret;
		pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&old);
		while(1)
		{
			memset(&mbuf, 0, sizeof(mbuf));
			
						
			mbuf.mtype = 2;
			scanf("%s",mbuf.mtext);
			ret = msgsnd(msgid,&mbuf,sizeof(mbuf.mtext), 0);
			if(ret == -1)
			{
				perror("msgsnd");
				exit(1);
			}
			if(!strcmp(mbuf.mtext,"bye"))
			{
				pthread_cancel(tid2);
				break;
			}

		}
}

void *mythread1(void *arg)
{
		struct msgbuf mbuf;
		int msgid = *(int *)arg;
		int old;
		int ret;

		pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&old);

	
			while(1)
		{
			ret = msgrcv(msgid,&mbuf,sizeof(mbuf.mtext), 1, 0);
			if(ret == -1)
			{
				perror("msgsnd");
				exit(1);
			}
			

			if(!strcmp(mbuf.mtext,"bye"))
			{
				pthread_cancel(tid1);
	
				break;
			}
			printf("\t\t\t%s\n",mbuf.mtext);

		}
	
}

int main()
{
		int ret;
	int msgid = msgget(MSGKEY,0);
	if(msgid == -1)
	{
		perror("msgget");
		exit(1);
	}
	ret = pthread_create(&tid1, NULL, mythread, &msgid);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(1);
	}
	ret = pthread_create(&tid2, NULL, mythread1, &msgid);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(1);
	}
	void *status;
	ret = pthread_join(tid1,&status);
	ret = pthread_join(tid2,&status);

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值