进程通信

进程通信石头剪刀布

裁判进程

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

struct msgbuf {                                         //消息缓冲区,具体看 play_01.c
	long msgtype;//类型
	int rps;//出招
};



int result[105];                       //保留游戏的结果
int player_01_info[105];       //保留选手出手的情况信息。
int player_02_info[105];       

	int count_0 = 0;     // the two players are all win
	int count_1 = 0;    // player_01 win
	int count_2 = 0;

int judge(int cha){
	int res=0;
	if(cha==0) {
		count_0++;
		res=0;
	}
	else if(cha==-1||cha==2) {
		count_1++;
		res=1;
	}
	else {
		count_2++;
		res=2;
	}
	return res;
}

int main () {
	char *apt[] = {"石头","剪刀","布"}; //石头>剪刀>布

	
	
	key_t key_01 = ftok(".", 21);//创建子进程,选手一
	key_t key_02 = ftok(".", 22);//创建子进程,选手二
	int Msg_ID_01 = msgget(key_01, IPC_CREAT|0777);//获取消息队列的消息,进行消息通信
	int Msg_ID_02 = msgget(key_02, IPC_CREAT|0777);
	struct msgbuf Snd_Buf_01 = {1,-1};
	struct msgbuf Rcv_Buf_01;
	struct msgbuf Snd_Buf_02 = {2,-1};
	struct msgbuf Rcv_Buf_02;

	int index = 0;       // The index of wfo
	//五局三胜
	for(index=0;index<5;index++){
		int nsend_01 = msgsnd(Msg_ID_01, &Snd_Buf_01, sizeof(Snd_Buf_01)- sizeof(long),0);
		
		int nread_01 = msgrcv(Msg_ID_01, &Rcv_Buf_01, sizeof(Rcv_Buf_01)-sizeof(long), 521, 0);
		
		int nsend_02 = msgsnd(Msg_ID_02, &Snd_Buf_02, sizeof(Snd_Buf_02)-sizeof(long), 0);
		
		int nread_02 = msgrcv(Msg_ID_02, &Rcv_Buf_02, sizeof(Rcv_Buf_02) - sizeof(long), 522, 0);
		
		int player_01_rps = Rcv_Buf_01.rps;  // 选手一的结果
		int player_02_rps = Rcv_Buf_02.rps;
		int cha = player_01_rps - player_02_rps;
		
		
		int temp=judge(cha);
		
		int info1 = player_01_rps;
		int info2 = player_02_rps;
		
		result[index] = temp;
		player_01_info[index] = info1;
		player_02_info[index] = info2;
	}
	//以下代码输出游戏的统计结果
	printf("游戏结果如下:\n");
	
	int ret1 = msgctl(Msg_ID_01, IPC_RMID, NULL);
	int ret2 = msgctl(Msg_ID_02, IPC_RMID, NULL);
	if (ret1 == -1 || ret2 == -1) {
		printf("fail to destory the msgqueue\n");
	}
	printf("%d---------------------------------------------------\n",index);
	for (int i = 0; i < index; i++) {
		int w = result[i];
		char* win;
		if (w == 0) {
			win = "他们两平局";
		}
		else if(w == 1) {
			win = "李萍老师赢了";
		}
		else {
			win = "小王同学赢了";
		}
		
		int p1 = player_01_info[i];
		int p2 = player_02_info[i];

		char* player_01 = apt[p1];
		char* player_02 = apt[p2];
		printf("结果: %s, 选手_01: %s, 选手_02: %s\n", win, player_01, player_02);
		printf("---------------------------------------------------\n");
	}
	printf("平局的数量: %d\n", count_0);
	printf("---------------------------------------------------\n");
	printf("李萍老师赢了 %d 场\n", count_1);
	printf("---------------------------------------------------\n");
	printf("小王同学赢了 %d 场\n", count_2);
	printf("---------------------------------------------------\n");
	if (count_1 == count_2) {
		printf("他们是平局\n");
	}
	else if (count_1 < count_2) {
		printf("赢家是小王同学\n");
	}
	else {
		printf("赢家是李萍老师\n");
	}
	return 0;
}

选手1进程

#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>


/*
	消息缓冲区的定义
	long msgtype;     消息类型
	int rps;                  代表石头剪刀布  0  1  2
*/
struct msgbuf {                                          
	long msgtype;                         
	int rps;                                    
};

int main () {
	struct msgbuf Snd_Buf = {521, -1};             // 发送消息类型为 521 到消息队列
	struct msgbuf Rcv_Buf;
	key_t key = ftok(".", 21);                                  // key 与 裁判程序里面的一样
	int Msg_ID = msgget(key, IPC_CREAT|0777);
	if (Msg_ID == -1) {
		printf("Msg create failure!\n");
	}
	int index = 0;
	srand(time(NULL));
	while (index < 5) {
		int nread = msgrcv(Msg_ID, &Rcv_Buf, sizeof(Rcv_Buf) - sizeof(long), 1, 0);    
		// 这个函数 的 sizeof(Rcv_Buf) - sizeof(long)很重要
			
		//int rps_01 = rand()%3;
		int rps_01;
		scanf("%d",&rps_01);
		Snd_Buf.rps = rps_01%3;
		int nsnd = msgsnd(Msg_ID, &Snd_Buf, sizeof(Snd_Buf) - sizeof(long), 0);
		
		index++;
	}	
	return 0;
}

选手二进程:

#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>

struct msgbuf {
	long msgtype;
	int rps;
};

int main () {
	struct msgbuf Snd_Buf = {522, -1};
	struct msgbuf Rcv_Buf;
	key_t key = ftok(".", 22);
	int Msg_ID = msgget(key, IPC_CREAT|0777);
	if (Msg_ID == -1) {
		printf("Msg create failure!\n");
	}
	int index = 0;
	srand(time(NULL));
	while (index < 5) {
		int nread = msgrcv(Msg_ID, &Rcv_Buf, sizeof(Rcv_Buf) - sizeof(long), 2, 0);
			
		//int rps_02 = rand()%3;
		int rps_02;
		scanf("%d",&rps_02);
		Snd_Buf.rps = rps_02%3;
		int nsnd = msgsnd(Msg_ID, &Snd_Buf, sizeof(Snd_Buf) - sizeof(long), 0);
		
		index++;
	}	
	return 0;
}
全部评论

相关推荐

10-18 13:01
已编辑
西安理工大学 C++
小米内推大使:建议技能还是放上面吧,hr和技术面试官第一眼想看的应该是技能点和他们岗位是否匹配
点赞 评论 收藏
分享
有趣的牛油果开挂了:最近这个阶段收到些杂七杂八的短信是真的烦
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务