Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <stdbool.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/types.h>
- #include <sys/sem.h>
- #include <string.h>
- #include <signal.h>
- #include <unistd.h>
- //----PAMIEC WSPOLDZIELONA----
- char *C1_LekkaPiechota;
- char *C1_CiezkaPiechota;
- char *C1_Jazda;
- char *C1_Robotnicy;
- char *C1_Surowce;
- char *C1_Punkty;
- char *C2_LekkaPiechota;
- char *C2_CiezkaPiechota;
- char *C2_Jazda;
- char *C2_Robotnicy;
- char *C2_Surowce;
- char *C2_Punkty;
- int C1_LekkaPiechotaAdr;
- int C1_CiezkaPiechotaAdr;
- int C1_JazdaAdr;
- int C1_RobotnicyAdr;
- int C1_SurowceAdr;
- int C1_PunktyAdr;
- int C2_LekkaPiechotaAdr;
- int C2_CiezkaPiechotaAdr;
- int C2_JazdaAdr;
- int C2_RobotnicyAdr;
- int C2_SurowceAdr;
- int C2_PunktyAdr;
- //----STRUKTURY----
- struct clientStatus {
- long mtype;
- int surowce;
- int lekka_piechota;
- int ciezka_piechota;
- int jazda;
- int robotnicy;
- int punkty;
- };
- struct prepareClient {
- long mtype;
- int CSQueueNumber;
- int SCQueueNumber;
- };
- struct message {
- long mtype;
- int value;
- };
- struct atak {
- long mtype;
- int lekka_p;
- int ciezka_p;
- int jazda;
- };
- //----SEMAFORY----
- int clientReadySID, serverReadySID, memoryMutexSID, c1RecruitingSID, c2RecruitingSID;
- struct sembuf clientReady;
- struct sembuf serverReady;
- struct sembuf memoryMutex;
- struct sembuf c1Recruiting;
- struct sembuf c2Recruiting;
- //----KOLEJKI----
- struct prepareClient pC;
- int pC_ID;
- void removeMQueues() {
- int queueID = msgget(1000, 0606);
- msgctl(queueID, IPC_RMID, 0);
- }
- void listenForFight() {
- if (fork()) {
- while (1) {
- // Klient 1 - Walka
- struct atak attack;
- int attack_ID = msgget(2001, 0606 | IPC_CREAT);
- msgrcv(attack_ID, &attack, 30, 10, 0);
- printf("Gracz 1 - ATAKUJE");
- fflush(stdout);
- int P1_wojsko_lp = attack.lekka_p;
- int P1_wojsko_cp = attack.ciezka_p;
- int P1_wojsko_j = attack.jazda;
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- if (P1_wojsko_lp > atoi(C1_LekkaPiechota))
- P1_wojsko_lp = atoi(C1_LekkaPiechota);
- int stayInBase = atoi(C1_LekkaPiechota) - P1_wojsko_lp;
- char W_Text[10];
- sprintf(W_Text, "%d", stayInBase);
- strcpy(C1_LekkaPiechota, W_Text);
- if (P1_wojsko_cp > atoi(C1_CiezkaPiechota))
- P1_wojsko_cp = atoi(C1_CiezkaPiechota);
- stayInBase = atoi(C1_CiezkaPiechota) - P1_wojsko_cp;
- sprintf(W_Text, "%d", stayInBase);
- strcpy(C1_CiezkaPiechota, W_Text);
- if (P1_wojsko_j > atoi(C1_Jazda))
- P1_wojsko_j = atoi(C1_Jazda);
- stayInBase = atoi(C1_Jazda) - P1_wojsko_j;
- sprintf(W_Text, "%d", stayInBase);
- strcpy(C1_Jazda, W_Text);
- int P2_wojsko_lp = atoi(C2_LekkaPiechota);
- int P2_wojsko_cp = atoi(C2_CiezkaPiechota);
- int P2_wojsko_j = atoi(C2_Jazda);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- int P1_attack = (P1_wojsko_lp * 1) + (P1_wojsko_cp * 1.5) + (P1_wojsko_j * 3.5);
- int P1_defense = (P1_wojsko_lp * 1.2) + (P1_wojsko_cp * 3) + (P1_wojsko_j * 1.2);
- int P2_attack = (P2_wojsko_lp * 1) + (P2_wojsko_cp * 1.5) + (P2_wojsko_j * 3.5);
- int P2_defense = (P2_wojsko_lp * 1.2) + (P2_wojsko_cp * 3) + (P2_wojsko_j * 1.2);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- if (P1_attack > P2_defense) {
- strcpy(C2_LekkaPiechota, "0");
- strcpy(C2_CiezkaPiechota, "0");
- strcpy(C2_Jazda, "0");
- int P1_Points = atoi(C1_Punkty);
- P1_Points += 1;
- char P_Text[10];
- sprintf(P_Text, "%d", P1_Points);
- strcpy(C1_Punkty, P_Text);
- } else {
- printf(" %d, %d, %d, %d\n", P2_wojsko_lp, P2_wojsko_cp, P2_wojsko_j, P1_attack/P2_defense);
- int LP_Straty = (double)((double)P2_wojsko_lp * (double)((double)P1_attack / (double)P2_defense));
- int CP_Straty = (double)((double)P2_wojsko_cp * (double)((double)P1_attack / (double)P2_defense));
- int J_Straty = (double)((double)P2_wojsko_j * (double)((double)P1_attack / (double)P2_defense));
- printf("%d, %d, %d\n", LP_Straty, CP_Straty, J_Straty);
- fflush(stdout);
- if (P2_wojsko_lp - LP_Straty < 0)
- P2_wojsko_lp = 0;
- else
- P2_wojsko_lp = P2_wojsko_lp - LP_Straty;
- if (P2_wojsko_cp - CP_Straty < 0)
- P2_wojsko_cp = 0;
- else
- P2_wojsko_cp = P2_wojsko_cp - CP_Straty;
- if (P2_wojsko_j - J_Straty < 0)
- P2_wojsko_j = 0;
- else
- P2_wojsko_j = P2_wojsko_j - J_Straty;
- int LP_AfterFight = P2_wojsko_lp;
- int CP_AfterFight = P2_wojsko_cp;
- int J_AfterFight = P2_wojsko_j;
- char LPAF_Text[10], CPAF_Text[10], JAF_Text[10];
- sprintf(LPAF_Text, "%d", LP_AfterFight);
- sprintf(CPAF_Text, "%d", CP_AfterFight);
- sprintf(JAF_Text, "%d", J_AfterFight);
- strcpy(C2_LekkaPiechota, LPAF_Text);
- strcpy(C2_CiezkaPiechota, CPAF_Text);
- strcpy(C2_Jazda, JAF_Text);
- }
- int LP_Straty = (double)((double)P1_wojsko_lp * (double)((double)P2_attack / (double)P1_defense));
- int CP_Straty = (double)((double)P1_wojsko_cp * (double)((double)P2_attack / (double)P1_defense));
- int J_Straty = (double)((double)P1_wojsko_j * (double)((double)P2_attack / (double)P1_defense));
- if (P1_wojsko_lp - LP_Straty < 0)
- P1_wojsko_lp = 0;
- else
- P1_wojsko_lp = P1_wojsko_lp - LP_Straty;
- if (P1_wojsko_cp - CP_Straty < 0)
- P1_wojsko_cp = 0;
- else
- P1_wojsko_cp = P1_wojsko_cp - CP_Straty;
- if (P1_wojsko_j - J_Straty < 0)
- P1_wojsko_j = 0;
- else
- P1_wojsko_j = P1_wojsko_j - J_Straty;
- int LP_AfterFight = P1_wojsko_lp + atoi(C1_LekkaPiechota);
- int CP_AfterFight = P1_wojsko_cp + atoi(C1_CiezkaPiechota);
- int J_AfterFight = P1_wojsko_j + atoi(C1_Jazda);
- char LPAF_Text[10], CPAF_Text[10], JAF_Text[10];
- sprintf(LPAF_Text, "%d", LP_AfterFight);
- sprintf(CPAF_Text, "%d", CP_AfterFight);
- sprintf(JAF_Text, "%d", J_AfterFight);
- strcpy(C1_LekkaPiechota, LPAF_Text);
- strcpy(C1_CiezkaPiechota, CPAF_Text);
- strcpy(C1_Jazda, JAF_Text);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- }
- } else {
- while (1) {
- // Klient 2 - Walka
- struct atak attack;
- int attack_ID = msgget(2002, 0606 | IPC_CREAT);
- msgrcv(attack_ID, &attack, 30, 10, 0);
- printf("Gracz 2 - ATAKUJE");
- fflush(stdout);
- }
- }
- }
- /**
- Nasłuchuje czy klient nie chce rekrutować jednostki
- */
- void listenForRecruitment() {
- // Klient 1 - Przychodzące
- if (fork()) {
- while (1) {
- struct message recruit;
- struct message informClient;
- int recruit_ID = msgget(2001, 0606 | IPC_CREAT);
- int informClient_ID = msgget(3001, 0606 | IPC_CREAT);
- int sendForward_ID = msgget(6001, 0606 | IPC_CREAT); // Dodatkowa kolejka do obsługi rekrutacji
- msgrcv(recruit_ID, &recruit, 20, -6, 0);
- int unitCost;
- int unitsClientBehest;
- int totalCost;
- switch (recruit.mtype) {
- // Lekka Piechota
- case 3:
- unitCost = 100;
- unitsClientBehest = recruit.value;
- totalCost = unitCost * unitsClientBehest;
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- if (totalCost > atoi(C1_Surowce)) {
- informClient.mtype = 8;
- informClient.value = 0;
- msgsnd(informClient_ID, &informClient, 20, 0);
- } else {
- int materialsTogether = atoi(C1_Surowce);
- int materialsAfterRecruitment = materialsTogether - totalCost;
- char M_Text[10];
- sprintf(M_Text, "%d", materialsAfterRecruitment);
- strcpy(C1_Surowce, M_Text);
- msgsnd(sendForward_ID, &recruit, 20, 0);
- informClient.mtype = 8;
- informClient.value = 1;
- msgsnd(informClient_ID, &informClient, 20, 0);
- }
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- break;
- // Ciężka Piechota
- case 4:
- unitCost = 250;
- unitsClientBehest = recruit.value;
- totalCost = unitCost * unitsClientBehest;
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- if (totalCost > atoi(C1_Surowce)) {
- informClient.mtype = 8;
- informClient.value = 0;
- msgsnd(informClient_ID, &informClient, 20, 0);
- } else {
- int materialsTogether = atoi(C1_Surowce);
- int materialsAfterRecruitment = materialsTogether - totalCost;
- char M_Text[10];
- sprintf(M_Text, "%d", materialsAfterRecruitment);
- strcpy(C1_Surowce, M_Text);
- msgsnd(sendForward_ID, &recruit, 20, 0);
- informClient.mtype = 8;
- informClient.value = 1;
- msgsnd(informClient_ID, &informClient, 20, 0);
- }
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- break;
- // Jazda
- case 5:
- unitCost = 550;
- unitsClientBehest = recruit.value;
- totalCost = unitCost * unitsClientBehest;
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- if (totalCost > atoi(C1_Surowce)) {
- informClient.mtype = 8;
- informClient.value = 0;
- msgsnd(informClient_ID, &informClient, 20, 0);
- } else {
- int materialsTogether = atoi(C1_Surowce);
- int materialsAfterRecruitment = materialsTogether - totalCost;
- char M_Text[10];
- sprintf(M_Text, "%d", materialsAfterRecruitment);
- strcpy(C1_Surowce, M_Text);
- msgsnd(sendForward_ID, &recruit, 20, 0);
- informClient.mtype = 8;
- informClient.value = 1;
- msgsnd(informClient_ID, &informClient, 20, 0);
- }
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- break;
- // Robotnicy
- case 6:
- unitCost = 100;
- unitsClientBehest = recruit.value;
- totalCost = unitCost * unitsClientBehest;
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- if (totalCost > atoi(C1_Surowce)) {
- informClient.mtype = 8;
- informClient.value = 0;
- msgsnd(informClient_ID, &informClient, 20, 0);
- } else {
- int materialsTogether = atoi(C1_Surowce);
- int materialsAfterRecruitment = materialsTogether - totalCost;
- char M_Text[10];
- sprintf(M_Text, "%d", materialsAfterRecruitment);
- strcpy(C1_Surowce, M_Text);
- msgsnd(sendForward_ID, &recruit, 20, 0);
- informClient.mtype = 8;
- informClient.value = 1;
- msgsnd(informClient_ID, &informClient, 20, 0);
- }
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- break;
- }
- }
- // Klienta 2 - Przychodzące
- } else if (fork()) {
- while (1) {
- struct message recruit;
- struct message informClient;
- int recruit_ID = msgget(2002, 0606 | IPC_CREAT);
- int sendForward_ID = msgget(6002, 0606 | IPC_CREAT); // Dodatkowa kolejka do obsługi rekrutacji
- int informClient_ID = msgget(3002, 0606 | IPC_CREAT);
- msgrcv(recruit_ID, &recruit, 20, -6, 0);
- int unitCost;
- int unitsClientBehest;
- int totalCost;
- switch (recruit.mtype) {
- // Lekka Piechota
- case 3:
- unitCost = 100;
- unitsClientBehest = recruit.value;
- totalCost = unitCost * unitsClientBehest;
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- if (totalCost > atoi(C2_Surowce)) {
- informClient.mtype = 8;
- informClient.value = 0;
- msgsnd(informClient_ID, &informClient, 20, 0);
- } else {
- int materialsTogether = atoi(C2_Surowce);
- int materialsAfterRecruitment = materialsTogether - totalCost;
- char M_Text[10];
- sprintf(M_Text, "%d", materialsAfterRecruitment);
- strcpy(C2_Surowce, M_Text);
- msgsnd(sendForward_ID, &recruit, 20, 0);
- informClient.mtype = 8;
- informClient.value = 1;
- msgsnd(informClient_ID, &informClient, 20, 0);
- }
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- break;
- // Ciężka Piechota
- case 4:
- unitCost = 250;
- unitsClientBehest = recruit.value;
- totalCost = unitCost * unitsClientBehest;
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- if (totalCost > atoi(C2_Surowce)) {
- informClient.mtype = 8;
- informClient.value = 0;
- msgsnd(informClient_ID, &informClient, 20, 0);
- } else {
- int materialsTogether = atoi(C2_Surowce);
- int materialsAfterRecruitment = materialsTogether - totalCost;
- char M_Text[10];
- sprintf(M_Text, "%d", materialsAfterRecruitment);
- strcpy(C2_Surowce, M_Text);
- msgsnd(sendForward_ID, &recruit, 20, 0);
- informClient.mtype = 8;
- informClient.value = 1;
- msgsnd(informClient_ID, &informClient, 20, 0);
- }
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- break;
- // Jazda
- case 5:
- unitCost = 550;
- unitsClientBehest = recruit.value;
- totalCost = unitCost * unitsClientBehest;
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- if (totalCost > atoi(C2_Surowce)) {
- informClient.mtype = 8;
- informClient.value = 0;
- msgsnd(informClient_ID, &informClient, 20, 0);
- } else {
- int materialsTogether = atoi(C2_Surowce);
- int materialsAfterRecruitment = materialsTogether - totalCost;
- char M_Text[10];
- sprintf(M_Text, "%d", materialsAfterRecruitment);
- strcpy(C2_Surowce, M_Text);
- msgsnd(sendForward_ID, &recruit, 20, 0);
- informClient.mtype = 8;
- informClient.value = 1;
- msgsnd(informClient_ID, &informClient, 20, 0);
- }
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- break;
- // Robotnicy
- case 6:
- unitCost = 100;
- unitsClientBehest = recruit.value;
- totalCost = unitCost * unitsClientBehest;
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- if (totalCost > atoi(C2_Surowce)) {
- informClient.mtype = 8;
- informClient.value = 0;
- msgsnd(informClient_ID, &informClient, 20, 0);
- } else {
- int materialsTogether = atoi(C2_Surowce);
- int materialsAfterRecruitment = materialsTogether - totalCost;
- char M_Text[10];
- sprintf(M_Text, "%d", materialsAfterRecruitment);
- strcpy(C2_Surowce, M_Text);
- msgsnd(sendForward_ID, &recruit, 20, 0);
- informClient.mtype = 8;
- informClient.value = 1;
- msgsnd(informClient_ID, &informClient, 20, 0);
- }
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- break;
- }
- }
- // REKRUTACJA GRACZ 1
- } else if (fork()) {
- while (1) {
- struct message recruit;
- int recruit_ID = msgget(6001, 0606 | IPC_CREAT);
- msgrcv(recruit_ID, &recruit, 20, -6, 0);
- int unitProductionTime;
- int i;
- c1Recruiting.sem_num = 0;
- c1Recruiting.sem_flg = 0;
- c1Recruiting.sem_op = -1;
- semop(c1RecruitingSID, &c1Recruiting, 1);
- switch (recruit.mtype) {
- // Lekka piechota - rekrutacja
- case 3:
- unitProductionTime = 2;
- for(i = 0; i < recruit.value; i++) {
- sleep(unitProductionTime);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- int afterRecruitment = atoi(C1_LekkaPiechota) + 1;
- char AR_Text[10];
- sprintf(AR_Text, "%d", afterRecruitment);
- strcpy(C1_LekkaPiechota, AR_Text);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- }
- break;
- // Ciezka piechota - rekrutacja
- case 4:
- unitProductionTime = 3;
- for(i = 0; i < recruit.value; i++) {
- sleep(unitProductionTime);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- int afterRecruitment = atoi(C1_CiezkaPiechota) + 1;
- char AR_Text[10];
- sprintf(AR_Text, "%d", afterRecruitment);
- strcpy(C1_CiezkaPiechota, AR_Text);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- }
- break;
- // Jazda - rekrutacja
- case 5:
- unitProductionTime = 5;
- for(i = 0; i < recruit.value; i++) {
- sleep(unitProductionTime);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- int afterRecruitment = atoi(C1_Jazda) + 1;
- char AR_Text[10];
- sprintf(AR_Text, "%d", afterRecruitment);
- strcpy(C1_Jazda, AR_Text);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- }
- break;
- // Robotnicy - rekrutacja
- case 6:
- unitProductionTime = 2;
- for(i = 0; i < recruit.value; i++) {
- sleep(unitProductionTime);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- int afterRecruitment = atoi(C1_Robotnicy) + 1;
- char AR_Text[10];
- sprintf(AR_Text, "%d", afterRecruitment);
- strcpy(C1_Robotnicy, AR_Text);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- }
- break;
- }
- c1Recruiting.sem_num = 0;
- c1Recruiting.sem_flg = 0;
- c1Recruiting.sem_op = 1;
- semop(c1RecruitingSID, &c1Recruiting, 1);
- }
- // REKRUTACJA GRACZ 2
- } else {
- while (1) {
- struct message recruit;
- int recruit_ID = msgget(6002, 0606 | IPC_CREAT);
- msgrcv(recruit_ID, &recruit, 20, -6, 0);
- c2Recruiting.sem_num = 0;
- c2Recruiting.sem_flg = 0;
- c2Recruiting.sem_op = -1;
- semop(c2RecruitingSID, &c2Recruiting, 1);
- int unitCost;
- int unitProductionTime;
- int unitsClientBehest ;
- int totalCost;
- int i;
- switch (recruit.mtype) {
- // Lekka piechota - rekrutacja
- case 3:
- unitProductionTime = 2;
- for(i = 0; i < recruit.value; i++) {
- sleep(unitProductionTime);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- int afterRecruitment = atoi(C2_LekkaPiechota) + 1;
- char AR_Text[10];
- sprintf(AR_Text, "%d", afterRecruitment);
- strcpy(C2_LekkaPiechota, AR_Text);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- }
- break;
- // Ciezka piechota - rekrutacja
- case 4:
- unitProductionTime = 3;
- for(i = 0; i < recruit.value; i++) {
- sleep(unitProductionTime);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- int afterRecruitment = atoi(C2_CiezkaPiechota) + 1;
- char AR_Text[10];
- sprintf(AR_Text, "%d", afterRecruitment);
- strcpy(C2_CiezkaPiechota, AR_Text);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- }
- break;
- // Jazda - rekrutacja
- case 5:
- unitProductionTime = 5;
- for(i = 0; i < recruit.value; i++) {
- sleep(unitProductionTime);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- int afterRecruitment = atoi(C2_Jazda) + 1;
- char AR_Text[10];
- sprintf(AR_Text, "%d", afterRecruitment);
- strcpy(C2_Jazda, AR_Text);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- }
- break;
- // Robotnicy - rekrutacja
- case 6:
- unitProductionTime = 2;
- for(i = 0; i < recruit.value; i++) {
- sleep(unitProductionTime);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- int afterRecruitment = atoi(C2_Robotnicy) + 1;
- char AR_Text[10];
- sprintf(AR_Text, "%d", afterRecruitment);
- strcpy(C2_Robotnicy, AR_Text);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- }
- break;
- }
- c2Recruiting.sem_num = 0;
- c2Recruiting.sem_flg = 0;
- c2Recruiting.sem_op = 1;
- semop(c2RecruitingSID, &c2Recruiting, 1);
- }
- }
- }
- /**
- Zwiększa co jedną sekundę ilość posiadanych surowców
- */
- void increaseMaterials() {
- while (1) {
- int materialsIncome = 50 + ( 5 * atoi(C1_Robotnicy) );
- int materialsOverall = atoi(C1_Surowce);
- int materialsAfterIncrease = materialsOverall + materialsIncome;
- char MAI_Text[10];
- sprintf(MAI_Text, "%d", materialsAfterIncrease);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, -1);
- strcpy(C1_Surowce, MAI_Text);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- materialsIncome = 50 + ( 5 * atoi(C2_Robotnicy) );
- materialsOverall = atoi(C2_Surowce);
- materialsAfterIncrease = materialsOverall + materialsIncome;
- MAI_Text[10];
- sprintf(MAI_Text, "%d", materialsAfterIncrease);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, -1);
- strcpy(C2_Surowce, MAI_Text);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- sleep(1);
- }
- }
- /**
- Wysyła do klienta informacje o stanie jego wojsk i surowców co 200 ms
- */
- void clientStatus() {
- while (1) {
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = -1;
- semop(memoryMutexSID, &memoryMutex, 1);
- struct clientStatus cS;
- int cS_ID = msgget(3001, 0606 | IPC_CREAT);
- cS.mtype = 1;
- cS.surowce = atoi(C1_Surowce);
- cS.lekka_piechota = atoi(C1_LekkaPiechota);
- cS.ciezka_piechota = atoi(C1_CiezkaPiechota);
- cS.jazda = atoi(C1_Jazda);
- cS.robotnicy = atoi(C1_Robotnicy);
- cS.punkty = atoi(C1_Punkty);
- msgsnd(cS_ID, &cS, 50, 0);
- cS_ID = msgget(3002, 0606 | IPC_CREAT);
- cS.mtype = 1;
- cS.surowce = atoi(C2_Surowce);
- cS.lekka_piechota = atoi(C2_LekkaPiechota);
- cS.ciezka_piechota = atoi(C2_CiezkaPiechota);
- cS.jazda = atoi(C2_Jazda);
- cS.robotnicy = atoi(C2_Robotnicy);
- cS.punkty = atoi(C2_Punkty);
- msgsnd(cS_ID, &cS, 50, 0);
- memoryMutex.sem_num = 0;
- memoryMutex.sem_flg = 0;
- memoryMutex.sem_op = 1;
- semop(memoryMutexSID, &memoryMutex, 1);
- usleep(200 * 1000);
- }
- }
- /**
- Przygotowuje serwer do pracy
- */
- void prepareServer() {
- // SEMAFORY
- clientReadySID = semget(7001, 1, 0600 | IPC_CREAT);
- serverReadySID = semget(7002, 1, 0600 | IPC_CREAT);
- memoryMutexSID = semget(8000, 1, 0600 | IPC_CREAT);
- c1RecruitingSID = semget(8001, 1, 0600 | IPC_CREAT);
- c2RecruitingSID = semget(8002, 1, 0600 | IPC_CREAT);
- if(clientReadySID == -1 || serverReadySID == -1 || memoryMutexSID == -1 || c1RecruitingSID == -1 || c2RecruitingSID == -1) {
- exit(1);
- }
- semctl(clientReadySID, 0, SETVAL, 0);
- semctl(serverReadySID, 0, SETVAL, 0);
- semctl(memoryMutexSID, 0, SETVAL, 1);
- semctl(c1RecruitingSID, 0, SETVAL, 1);
- semctl(c2RecruitingSID, 0, SETVAL, 1);
- // KOLEJKI
- pC_ID = msgget(1000, 0606 | IPC_CREAT);
- // PAMIEC
- C1_LekkaPiechotaAdr = shmget(401, 20, 0606 | IPC_CREAT);
- C1_CiezkaPiechotaAdr = shmget(402, 20, 0606 | IPC_CREAT);
- C1_JazdaAdr = shmget(403, 20, 0606 | IPC_CREAT);
- C1_RobotnicyAdr = shmget(404, 20, 0606 | IPC_CREAT);
- C1_SurowceAdr = shmget(405, 20, 0606 | IPC_CREAT);
- C1_PunktyAdr = shmget(406, 20, 0606 | IPC_CREAT);
- C2_LekkaPiechotaAdr = shmget(501, 20, 0606 | IPC_CREAT);
- C2_CiezkaPiechotaAdr = shmget(502, 20, 0606 | IPC_CREAT);
- C2_JazdaAdr = shmget(503, 20, 0606 | IPC_CREAT);
- C2_RobotnicyAdr = shmget(504, 20, 0606 | IPC_CREAT);
- C2_SurowceAdr = shmget(505, 20, 0606 | IPC_CREAT);
- C2_PunktyAdr = shmget(506, 20, 0606 | IPC_CREAT);
- C1_LekkaPiechota = (char*)shmat(C1_LekkaPiechotaAdr, NULL, 0);
- C1_CiezkaPiechota = (char*)shmat(C1_CiezkaPiechotaAdr, NULL, 0);
- C1_Jazda = (char*)shmat(C1_JazdaAdr, NULL, 0);
- C1_Robotnicy = (char*)shmat(C1_RobotnicyAdr, NULL, 0);
- C1_Surowce = (char*)shmat(C1_SurowceAdr, NULL, 0);
- C1_Punkty = (char*)shmat(C1_PunktyAdr, NULL, 0);
- C2_LekkaPiechota = (char*)shmat(C2_LekkaPiechotaAdr, NULL, 0);
- C2_CiezkaPiechota = (char*)shmat(C2_CiezkaPiechotaAdr, NULL, 0);
- C2_Jazda = (char*)shmat(C2_JazdaAdr, NULL, 0);
- C2_Robotnicy = (char*)shmat(C2_RobotnicyAdr, NULL, 0);
- C2_Surowce = (char*)shmat(C2_SurowceAdr, NULL, 0);
- C2_Punkty = (char*)shmat(C2_PunktyAdr, NULL, 0);
- strcpy(C1_LekkaPiechota, "0");
- strcpy(C1_CiezkaPiechota, "0");
- strcpy(C1_Jazda, "0");
- strcpy(C1_Robotnicy, "0");
- strcpy(C1_Surowce, "300");
- strcpy(C1_Punkty, "0");
- strcpy(C2_LekkaPiechota, "0");
- strcpy(C2_CiezkaPiechota, "0");
- strcpy(C2_Jazda, "0");
- strcpy(C2_Robotnicy, "0");
- strcpy(C2_Surowce, "300");
- strcpy(C2_Punkty, "0");
- }
- int main() {
- #define client1_server 2001
- #define client2_server 2002
- #define server_client1 3001
- #define server_client2 3002
- prepareServer();
- clientReady.sem_num = 0;
- clientReady.sem_flg = 0;
- clientReady.sem_op = -1;
- semop(clientReadySID, &clientReady, 1); // POŁĄCZENIE PIERWSZEGO KLIENTA
- pC.mtype = 1;
- pC.CSQueueNumber = client1_server;
- pC.SCQueueNumber = server_client1;
- msgsnd(pC_ID, &pC, 10, 0);
- semop(clientReadySID, &clientReady, 1); // POŁĄCZENIE DRUGIEGO KLIENTA
- pC.CSQueueNumber = client2_server;
- pC.SCQueueNumber = server_client2;
- msgsnd(pC_ID, &pC, 10, 0);
- serverReady.sem_num = 0;
- serverReady.sem_flg = 0;
- serverReady.sem_op = 2;
- semop(serverReadySID, &serverReady, 1);
- semctl(clientReadySID, 0, SETVAL, 0);
- if (fork())
- clientStatus();
- else if (fork())
- increaseMaterials();
- else if (fork())
- listenForRecruitment();
- else if (fork())
- listenForFight();
- else {
- clientReady.sem_op = -2;
- semop(clientReadySID, &clientReady, 1);
- removeMQueues();
- kill(0, SIGKILL);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement