Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.c
- * Author: tiago
- *
- * Created on November 5, 2015, 4:25 PM
- */
- #include "sema.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/shm.h>
- #define LIFETIME 30 /* max. lifetime of a process */
- #define KEYBALLSOFPLAYERS 110
- #define KEYGOALS 111
- #define KEYREMATES 113
- #define KEYPOSSEBOLA 117
- #define KEYFALTAS 118
- #define KEYBOLAS 11
- int *bolasJogadores;
- int *golos;
- int *faltas;
- int *posseBola;
- int *bolas;
- int NPlayers, NBolas;
- int *numeroRemates;
- /*
- *
- */
- int main(int argc, char** argv) {
- int i, j, k, child_stat, cc;
- /*#########################################################################################################################################
- ############################################## TRATAMENTO DE VARIAVEIS NJOGADORES E NBOLAS#################################################
- ########################################################################################################################################### */
- printf("Insira o numero de jogadores: "); //output para informar que se pretende saber o numero de jogadores
- scanf("%d", &NPlayers); //input recebe o numero de jogadores
- printf("Insira o numero de bolas: "); //output para informar que se pretende saber o numero de bolas
- scanf("%d", &NBolas); //input recebe o numero de bolas
- /*#########################################################################################################################################
- ############################################## TRATAMENTO DE MEMORIA PARTILHADA############################################################
- ########################################################################################################################################### */
- int shmid = shmget(KEYBALLSOFPLAYERS, NPlayers, 0777 | IPC_CREAT);
- char *addr = (char*) shmat(shmid, 0, 0);
- bolasJogadores = (int*) addr;
- int shmid1 = shmget(KEYGOALS, NPlayers, 0777 | IPC_CREAT);
- char *addr1 = (char*) shmat(shmid1, 0, 0);
- golos = (int*) addr1;
- int shmid2 = shmget(KEYREMATES, NPlayers, 0777 | IPC_CREAT);
- char *addr2 = (char*) shmat(shmid2, 0, 0);
- numeroRemates = (int*) addr2;
- int shmid3 = shmget(KEYFALTAS, NPlayers, 0777 | IPC_CREAT);
- char *addr3 = (char*) shmat(shmid3, 0, 0);
- faltas = (int*) addr3;
- int shmid4 = shmget(KEYPOSSEBOLA, NPlayers, 0777 | IPC_CREAT);
- char *addr4 = (char*) shmat(shmid4, 0, 0);
- posseBola = (int*) addr4;
- int shmid5 = shmget(KEYBOLAS, NBolas, 0777 | IPC_CREAT);
- char *addr5 = (char*) shmat(shmid5, 0, 0);
- bolas = (int*) addr5;
- /*#########################################################################################################################################
- ############################################## ARRAY PARA OS FILHOS #######################################################################
- ########################################################################################################################################### */
- int child_pid [NBolas + 1], /* process ID's */
- wait_pid;
- /*#########################################################################################################################################
- ############################################## SEMAFOROS ##################################################################################
- ########################################################################################################################################### */
- //semaforo que controla os acessos á memoria
- semaphore control = init_sem(1);
- srand(time(NULL));
- /*#########################################################################################################################################
- ############################################## INICIO DOS PROCESSOS FILHOS ################################################################
- ########################################################################################################################################### */
- for (i = 0; i < NBolas + 1; i++) {
- child_pid [i] = fork(); //criar filhos
- /*#########################################################################################################################################
- ############################################## VERIFICAR QUAL É O PID #####################################################################
- ########################################################################################################################################### */
- switch (child_pid [i]) {
- case -1: /* error: no process created */
- perror("fork failed");
- exit(1);
- break;
- case 0://estamos no filho
- sleep(2);
- alarm(LIFETIME);
- while (1) {
- /*#########################################################################################################################################
- ############################################## PROCESSO EM QUE i=NPlayers É RESPONSAVEL POR CONTROLAR POSSE DE BOLA #######################
- ########################################################################################################################################### */
- if (i == NBolas) {
- int k;
- for (k = 0; k < NPlayers; k++) {
- if (*(bolasJogadores + k) > 0) {
- *(posseBola + k) = *(posseBola + k) + 100;
- }
- }
- usleep(100 * 1000);
- /*#########################################################################################################################################
- ######################### PROCESSO EM QUE i=0 ATÉ i=NPlayers-1 REPRESENTA O COMPORTAMENTO DOS JOGADORES####################################
- ########################################################################################################################################### */
- } else {
- int tempoRemate = rand() % 601;
- if (tempoRemate > 500) {
- usleep(500 * 1000);
- int l = *(bolas + i);
- P(control);
- *(faltas + l - 1) = *(faltas + l - 1) + 1;
- printf("###################Jogador %d cometeu uma falta ############################## \n", *(bolas + i));
- } else {
- if (tempoRemate < 100) {
- tempoRemate = 100;
- }
- usleep(tempoRemate * 1000);
- P(control);
- }
- rematar(i);
- int s;
- // printf("Placar");
- //for (s = 0; s < NPlayers; s++) {
- // printf("Jogador %d com %d golos num total de %d remates||", s + 1, *(golos + s), *(numeroRemates + s));
- //}
- printf("\n");
- V(control);
- }
- };
- break;
- default: /* parent process */
- /*#########################################################################################################################################
- ############################################## LIMPAR MEMORIA PARA PREVENIR ################################################################
- ########################################################################################################################################### */
- for (cc = 0; cc < NPlayers; cc++) {
- *(bolasJogadores + cc) = 0;
- *(posseBola + cc) = 0;
- *(faltas + cc) = 0;
- *(golos + cc) = 0;
- *(numeroRemates + cc) = 0;
- }
- for (cc = 0; cc < NBolas; cc++) {
- *(bolas + cc) = 0;
- }
- /*#########################################################################################################################################
- ############################################## DISTRIBUIR BOLAS ###########################################################################
- ########################################################################################################################################### */
- atribuirBolas(NPlayers, NBolas);
- /*#########################################################################################################################################
- ############################################## PROCESSO PAI################################################################################
- * Esperar que os processos filhos acabem
- * Responsavel por tratar da memoria
- ########################################################################################################################################### */
- if (i == (NBolas)) /* all childs created ? */ { /* yes -> wait for termination */
- for (j = 0; j < NBolas + 1; ++j) {
- wait_pid = wait(&child_stat);
- if (wait_pid == -1) {
- perror("wait failed");
- };
- };
- printf("All child processes have terminated.\n");
- //int lm;
- //for (lm = 0; lm < NPlayers; lm++) {
- // printf("Jogador %d com %d golos em %d remates tendo posse bola durante %1.2f minutos cometendo %d faltas \n", lm + 1, *(golos + lm), *(numeroRemates + lm)
- // , *(posseBola + lm) / ((float) 1000), *(faltas + lm));
- // }
- int podio [NPlayers][2];
- dataToArray(podio);
- selectionSort(podio, NPlayers);
- imprimir(podio, NPlayers);
- printf("verificando necesidade de prolongamento\n");
- for (cc = 0; cc < NPlayers; cc++) {
- *(bolasJogadores + cc) = 0;
- *(posseBola + cc) = 0;
- *(faltas + cc) = 0;
- *(golos + cc) = 0;
- *(numeroRemates + cc) = 0;
- }
- for (cc = 0; cc < NBolas; cc++) {
- *(bolas + cc) = 0;
- }
- int nJogadoresAProlongamento = prolongamento(podio);
- if (nJogadoresAProlongamento > 0) {
- int ii, jj;
- for (i = 0; i < NBolas + 1; i++) {
- child_pid [i] = fork(); //criar filhos
- /*#########################################################################################################################################
- ############################################## VERIFICAR QUAL É O PID #####################################################################
- ########################################################################################################################################### */
- switch (child_pid [i]) {
- case -1: /* error: no process created */
- perror("fork failed");
- exit(1);
- break;
- case 0://estamos no filho
- sleep(2);
- alarm(5);
- while (1) {
- /*#########################################################################################################################################
- ############################################## PROCESSO EM QUE i=NPlayers É RESPONSAVEL POR CONTROLAR POSSE DE BOLA #######################
- ########################################################################################################################################### */
- if (i == NBolas) {
- int k;
- for (k = 0; k < NPlayers; k++) {
- if (*(bolasJogadores + k) > 0) {
- *(posseBola + k) = *(posseBola + k) + 100;
- }
- }
- usleep(100 * 1000);
- /*#########################################################################################################################################
- ######################### PROCESSO EM QUE i=0 ATÉ i=NPlayers-1 REPRESENTA O COMPORTAMENTO DOS JOGADORES####################################
- ########################################################################################################################################### */
- } else {
- int tempoRemate = rand() % 601;
- if (tempoRemate > 500) {
- usleep(500 * 1000);
- int l = *(bolas + i);
- P(control);
- *(faltas + l - 1) = *(faltas + l - 1) + 1;
- printf("###################Jogador %d cometeu uma falta ############################## \n", *(bolas + i));
- } else {
- if (tempoRemate < 100) {
- tempoRemate = 100;
- }
- usleep(tempoRemate * 1000);
- P(control);
- }
- int JogadorAQuemRematar;
- do {
- JogadorAQuemRematar = podio[rand() % nJogadoresAProlongamento][1];
- } while (JogadorAQuemRematar == *(bolas + i));
- rematarProlongamento(i, JogadorAQuemRematar - 1);
- int s;
- printf("\n");
- V(control);
- }
- };
- break;
- default: /* parent process */
- for (ii = 0; ii < NBolas; ii++) {
- jj = rand() % nJogadoresAProlongamento;
- *(bolas + ii) = podio[jj][1];
- *(bolasJogadores + podio[jj][1] - 1) = *(bolasJogadores + podio[jj][1] - 1) + 1;
- }
- /*#########################################################################################################################################
- ############################################## PROCESSO PAI################################################################################
- * Esperar que os processos filhos acabem
- * Responsavel por tratar da memoria
- ########################################################################################################################################### */
- if (i == (NBolas)) /* all childs created ? */ { /* yes -> wait for termination */
- for (j = 0; j < NBolas + 1; ++j) {
- wait_pid = wait(&child_stat);
- if (wait_pid == -1) {
- perror("wait failed");
- };
- };
- int podio2[nJogadoresAProlongamento][2];
- dataToProlongamento(podio2, podio, nJogadoresAProlongamento);
- selectionSort(podio2, nJogadoresAProlongamento);
- imprimir(podio2, nJogadoresAProlongamento);
- };
- }; /* end switch */
- }; /* end for */
- rel_sem(control);
- sleep(2);
- shmdt(addr);
- shmctl(shmid, IPC_RMID, NULL);
- shmdt(addr1);
- shmctl(shmid1, IPC_RMID, NULL);
- shmdt(addr2);
- shmctl(shmid2, IPC_RMID, NULL);
- shmdt(addr3);
- shmctl(shmid3, IPC_RMID, NULL);
- shmdt(addr4);
- shmctl(shmid4, IPC_RMID, NULL);
- shmdt(addr5);
- shmctl(shmid5, IPC_RMID, NULL);
- }
- };
- }; /* end switch */
- }; /* end for */
- return (EXIT_SUCCESS);
- }
- int prolongamento(int array[][2]) {
- int maxGolo = 1, i;
- for (i = 1; i < NPlayers; i++) {
- if (array[i][0] == array[0][0]) {
- maxGolo++;
- }
- }
- if (maxGolo > 1) {
- printf("É necessario prolongamento\n");
- printf("vao %d jogadores a prolongamento", maxGolo);
- return maxGolo;
- } else {
- printf("FIM DE JOGO\n");
- }
- return 0;
- }
- imprimir(int array[][2], int tamanho) {
- int i;
- float precisao;
- printf("#########################################################################################################################################################\n");
- for (i = 0; i < tamanho; i++) {
- if (array[i][0] > 0) {
- precisao = array[i][0] / ((float) *(numeroRemates + array[i][1] - 1));
- } else {
- precisao = 0;
- }
- printf("%d lugar- Jogador %d com %d golos em %d remates com uma precisao de remate %1.2f %% tendo posse bola durante %1.2f minutos cometendo %d faltas \n", i + 1, array[i][1], array[i][0], *(numeroRemates + array[i][1] - 1), precisao * 100
- , *(posseBola + array[i][1] - 1) / ((float) 1000), *(faltas + array[i][1] - 1));
- sleep(0.2);
- };
- printf("#########################################################################################################################################################\n");
- }
- int dataToArray(int array[][2]) {
- int i;
- for (i = 0; i < NPlayers; i++) {
- array[i][0] = *(golos + i); //os golos dele
- array[i][1] = i + 1; //numero do jogador
- };
- }
- int dataToProlongamento(int array[][2], int array2[][2], int tamanho) {
- int i;
- for (i = 0; i < tamanho; i++) {
- array[i][1] = array2[i][1]; //numero do jogador
- array[i][0] = *(golos + array[i][1] - 1); //os golos dele
- };
- }
- selectionSort(int num[][2], int tam) {
- int i, j, min, aux;
- for (i = 0; i < (tam - 1); i++) {
- min = i;
- for (j = (i + 1); j < tam; j++) {
- if (num[j][0] > num[min][0])
- min = j;
- }
- if (i != min) {
- aux = num[i][0];
- num[i][0] = num[min][0];
- num[min][0] = aux;
- aux = num[i][1];
- num[i][1] = num[min][1];
- num[min][1] = aux;
- }
- }
- }
- rematarProlongamento(int i, int baliza) {
- int k = *(bolas + i); //k é o numero do jogador que vai rematar
- printf("###################################################\n");
- if (rand() % 2 == 0) {//50% 50%
- printf("Jogador %d marcou golo\n", k);
- *(golos + k - 1) = *(golos + k - 1) + 1;
- } else {//else falhou
- printf("Jogador %d falhou \n", k);
- }
- *(bolas + i) = (baliza + 1);
- *(bolasJogadores + baliza) = *(bolasJogadores + baliza) + 1;
- printf("Jogador %d ganhou uma bola\n", baliza + 1);
- *(numeroRemates + k - 1) = *(numeroRemates + k - 1) + 1;
- *(bolasJogadores + k - 1) = *(bolasJogadores + k - 1) - 1;
- printf("###################################################\n");
- }
- rematar(int i) {
- int k = *(bolas + i); //k é o numero do jogador que vai rematar
- int baliza = getBalizaValida(k - 1); //obtem baliza que via rematar
- printf("###################################################\n");
- if (rand() % 2 == 0) {//50% 50%
- printf("Jogador %d marcou golo\n", k);
- *(golos + k - 1) = *(golos + k - 1) + 1;
- } else {//else falhou
- printf("Jogador %d falhou \n", k);
- }
- *(bolas + i) = (baliza + 1);
- *(bolasJogadores + baliza) = *(bolasJogadores + baliza) + 1;
- printf("Jogador %d ganhou uma bola\n", baliza + 1);
- *(numeroRemates + k - 1) = *(numeroRemates + k - 1) + 1;
- *(bolasJogadores + k - 1) = *(bolasJogadores + k - 1) - 1;
- printf("###################################################\n");
- }
- int getBalizaValida(int i) {
- int aux;
- do {
- aux = rand() % NPlayers;
- } while (aux == i);
- return aux;
- }
- atribuirBolas(int numeroJogadores, int numeroBolas) {
- int i, j;
- for (i = 0; i < numeroBolas; i++) {
- j = rand() % numeroJogadores;
- *(bolas + i) = j + 1;
- *(bolasJogadores + j) = *(bolasJogadores + j) + 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement