国庆day1

国庆day1

#include
#include
#include
struct msgbuf
{
	long mtype;      //消息类型
	char mtext[128]; //消息内容
};
 
//线程1函数
void *task1(void *arg)
{
	int msqid = *(int *)arg;//获取消息队列id号
	struct msgbuf msbuf;//声明消息结构体
	printf("A:\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("线程%d:发送成功\n",getpid());
		printf("A:\n\t");
		fflush(stdout);
		if (!strcmp(msbuf.mtext,"quit"))
		{
			system("clear");
			exit(0);
		}
	}
	pthread_exit(NULL);
}
 
//线程2函数
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("msgrcv");
			return NULL;
		}
		printf("\nB:\n\t%s\n",buf.mtext);
		printf("A:\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("ftok");
		return -1;/* code */
	}
	umask(0);
	int msqid = msgget(key,IPC_CREAT|0664);
	if (msqid == -1)
	{
		perror("msgget");
		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("线程1创建失败\n");
	}
	
 
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
 
	
	return 0;
}

=====================================

#include
struct msgbuf
{
	long mtype;      //消息类型
	char mtext[128]; //消息内容
};
 
//线程1函数
void *task1(void *arg)
{
	int msqid = *(int *)arg;
	struct msgbuf msbuf;
	printf("B:\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("线程%d:发送成功\n",getpid());
		printf("B:\n\t");
		fflush(stdout);
		if (!strcmp(msbuf.mtext,"quit"))
		{
			system("clear");
			exit(0);
		}
	}
	pthread_exit(NULL);
}
 
//线程2函数
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("msgrcv");
			return NULL;
		}
		printf("\nA:\n\t%s\n",buf.mtext);
		printf("B:\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("ftok");
		return -1;/* code */
	}
	umask(0);
	int msqid = msgget(key,IPC_CREAT|0664);
	if (msqid == -1)
	{
		perror("msgget");
		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("线程1创建失败\n");
	}
	
 
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
 
	
	return 0;
}

你可能感兴趣的:(learn,c语言)