day0823

#include <myhead.h>
struct msgbuf
{
	long mtype;
	char mtext[128];
};
void *task1(void *arg)
{
	int msqid = *(int *)arg;
	struct msgbuf msbuf;
	printf("陈:\n\t");
	fflush(stdout);
	while(1)
	{
		msbuf.mtype = 1;
		fgets(msbuf.mtext,sizeof(msbuf.mtext),stdin);
		msbuf.mtext[strlen(msbuf.mtext)-1]='\0';
		if(msgsnd(msqid,&msbuf,sizeof(msbuf)-sizeof(long),0)==-1)
		{
			perror("msgsnd");
			return NULL;
		}
		printf("陈:\n\t");
		fflush(stdout);
		if(!strcmp(msbuf.mtext,"quit"))
		{
			system("clear");
			exit(0);
		}
	}
	pthread_exit(NULL);
}
void *task2(void *arg)
{
	int msqid = *(int *)arg;
	struct msgbuf buf;
	ssize_t num = 0;
	while(1)
	{
		bzero(&buf,sizeof(buf));
		if((num = msgrcv(msqid,&buf,sizeof(buf.mtext),2,0))<0)
		{
			//perror("msgsnd");
			return NULL;
		}
		printf("\n李:\n\t%s\n",buf.mtext);
		printf("陈:\n\t");
		fflush(stdout);
		if(!strcmp(buf.mtext,"quit"))
		{
			msgctl(msqid,IPC_RMID,NULL);
			system("clear");
			exit(0);
		}
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	key_t key = ftok("./",0);
	if(key == -1)
	{
		perror("fork error");
		return -1;
	}
	umask(0);
	int msqid = msgget(key,IPC_CREAT|0664);
	if(msqid == -1)
	{
		perror("msgget error");
		return -1;
	}
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,task1,&msqid)!=0)
	{
		printf("线程1创建失败\n");
	}
	if(pthread_create(&tid2,NULL,task2,&msqid)!=0)
	{
		printf("线程2创建失败\n");
	}
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	return 0;
}
#include <myhead.h>
struct msgbuf
{
	long mtype;
	char mtext[128];
};
void *task1(void *arg)
{
	int msqid = *(int *)arg;
	struct msgbuf msbuf;
	printf("李:\n\t");
	fflush(stdout);
	while(1)
	{
		msbuf.mtype = 2;
		fgets(msbuf.mtext,sizeof(msbuf.mtext),stdin);
		msbuf.mtext[strlen(msbuf.mtext)-1]='\0';
		if(msgsnd(msqid,&msbuf,sizeof(msbuf)-sizeof(long),0)==-1)
		{
			perror("msgsnd");
			return NULL;
		}
		printf("李:\n\t");
		fflush(stdout);
		if(!strcmp(msbuf.mtext,"quit"))
		{
			system("clear");
			exit(0);
		}
	}
	pthread_exit(NULL);
}
void *task2(void *arg)
{
	int msqid = *(int *)arg;
	struct msgbuf buf;
	ssize_t num = 0;
	while(1)
	{
		bzero(&buf,sizeof(buf));
		if((num = msgrcv(msqid,&buf,sizeof(buf.mtext),1,0))<0)
		{
			//perror("msgsnd");
			return NULL;
		}
		printf("\n陈:\n\t%s\n",buf.mtext);
		printf("李:\n\t");
		fflush(stdout);
		if(!strcmp(buf.mtext,"quit"))
		{
			msgctl(msqid,IPC_RMID,NULL);
			system("clear");
			exit(0);
		}
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	key_t key = ftok("./",0);
	if(key == -1)
	{
		perror("fork error");
		return -1;
	}
	umask(0);
	int msqid = msgget(key,IPC_CREAT|0664);
	if(msqid == -1)
	{
		perror("msgget error");
		return -1;
	}
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,task1,&msqid)!=0)
	{
		printf("线程1创建失败\n");
	}
	if(pthread_create(&tid2,NULL,task2,&msqid)!=0)
	{
		printf("线程2创建失败\n");
	}
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值